changeset 58:112bf081148c

make a full CLI class for a single API template
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 06 Jan 2011 15:54:55 -0800
parents 074a32920f7c
children 93f2b2c7f838
files makeitso/cli.py makeitso/template.py
diffstat 2 files changed, 59 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/makeitso/cli.py	Thu Jan 06 14:27:09 2011 -0800
+++ b/makeitso/cli.py	Thu Jan 06 15:54:55 2011 -0800
@@ -4,12 +4,57 @@
 
 from optparse import OptionParser
 
-def parser(template):
-  """
-  return a command line parser for the template
-  """
-  usage = '%prog [options]'
-  description = getattr(template, 'description', None)
-  parser = OptionParser(usage=usage, description=description)
-  return parser
+class MakeItSoCLI(object):
+  """command line interface to a makeitso template"""
+  
+  def __init__(self, template_class):
+    self.template_class = template_class
+
+  def parser(self):
+    """
+    return a command line parser for the template
+    """
+    usage = '%prog [options] output [var1=value1] [var2=value2] [...]'
+    description = getattr(template, 'description', None)
+    parser = OptionParser(usage=usage, description=description)
+    parser.add_options('--variables', dest='variables', 
+                       action='store_true', default=False,
+                       help="display variables in the template")
+    return parser
+
+  def parse(self):
+    parser = self.parser()
+    options, args = parser.parse_args()
+
+    # print the variables for the templates
+    if options.variables:
 
+        # makes no sense without a template
+        if not args:
+            parser.print_usage()
+            parser.exit()
+
+        # find all variables
+        template = self.template()
+        variables = template.variables()
+
+        # print them
+        for variable in sorted(variables):
+            print variable
+        return
+
+    # template variables
+    variables = {}
+    output = []
+    for arg in args:
+        if '=' in arg:
+            key, value = arg.split('=')
+            variables[key] = value
+        else:
+            output.append(arg)
+    if len(output) != 1:
+      parser.error("Please specify one output")
+    
+    template = self.template_class(output=output[0], variables=variables)
+
+    return template
--- a/makeitso/template.py	Thu Jan 06 14:27:09 2011 -0800
+++ b/makeitso/template.py	Thu Jan 06 15:54:55 2011 -0800
@@ -14,7 +14,7 @@
 class Variable(object):
     """variable object for MakeItSo templates"""
     
-    def __init__(self, name, default=Undefined, description=None,
+    def __init__(self, name, description=None, default=Undefined,
                  cast=None):
         self.name = name
         self.default = default
@@ -63,7 +63,8 @@
     # inspect the templates for more variables
     look = False
 
-    def __init__(self, output=None, interactive=True, usedefaults=False, **variables):
+    def __init__(self, output=None, interactive=True, usedefaults=True,
+                 variables=None):
         """
         - output : output file or directory
         - interactive : whether tointeractively get variables
@@ -73,10 +74,10 @@
         assert self.templates
         self.output = output
         self.interactive = interactive
-        self.location = os.path.dirnme(os.path.abspath(__file__))
-        self.defaults = variables.copy
+        self.location = os.path.dirname(os.path.abspath(__file__))
+        self.defaults = variables.copy()
 
-        # make a dictionary of the variables
+        # make a dictionary of the variables for lookup convenience
         self.vardict = {}
         for i in self.vars:
             self.vardict[i.name] = i