changeset 95:e74baa8e6df4

fix CLI interface a bit....write a test for it
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 11 Jan 2011 09:06:25 -0800
parents b6a46332cced
children cc17537254d2
files examples/doctest.txt makeitso/cli.py makeitso/python.py makeitso/python_package/{{package}}/template.py
diffstat 4 files changed, 21 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/examples/doctest.txt	Tue Jan 11 07:33:59 2011 -0800
+++ b/examples/doctest.txt	Tue Jan 11 09:06:25 2011 -0800
@@ -102,6 +102,14 @@
     'Hello bar'
     >>> os.remove(buffer)
 
+Test CLI handler:
+
+    >>> from makeitso.cli import MakeItSoCLI
+    >>> cli = MakeItSoCLI(MyTemplate)
+    >>> buffer = tempfile.mktemp()
+    >>> cli(buffer)
+    >>> os.remove(buffer)
+
 Test to make sure permissions are preserved. This won't work on windows::
 
     >>> buffer = tempfile.mktemp()
--- a/makeitso/cli.py	Tue Jan 11 07:33:59 2011 -0800
+++ b/makeitso/cli.py	Tue Jan 11 09:06:25 2011 -0800
@@ -29,12 +29,12 @@
                         help=description)
     return parser
 
-  def parse(self, parser=None, options=None, args=None):
+  def parse(self, args=None, parser=None, options=None):
 
     # parse the command line
-    if not parser or not options or not args:
+    if not parser or not options:
       parser = self.parser()
-      options, args = parser.parse_args()
+      options, args = parser.parse_args(args=args)
 
     # ensure output is given
     if len(args) != 1:
@@ -44,8 +44,11 @@
     variables = dict([(key, value)
                       for key, value in options.__dict__.items()
                       if not key.startswith('_')])
+
+    #
+    return variables, args[0]
     
-    # instantiate the template
-    template = self.template_class(output=args[0], variables=variables)
-
-    return template
+  def __call__(self, *args):
+    variables, output = self.parse(list(args))
+    template = self.template_class(variables=variables)
+    template.substitute({}, output=output)
--- a/makeitso/python.py	Tue Jan 11 07:33:59 2011 -0800
+++ b/makeitso/python.py	Tue Jan 11 09:06:25 2011 -0800
@@ -39,6 +39,7 @@
   def __init__(self, **kw):
     MakeItSoTemplate.__init__(self, **kw)
 
+
 def main(args=sys.argv[:]):
   usage = '%prog [options]'
   
--- a/makeitso/python_package/{{package}}/template.py	Tue Jan 11 07:33:59 2011 -0800
+++ b/makeitso/python_package/{{package}}/template.py	Tue Jan 11 09:06:25 2011 -0800
@@ -24,9 +24,8 @@
 
 def main(args=sys.argv[:]):
   cli = TemplateCLI()
-  template = cli.parse()
-  template.substitue()
-
+  cli(args)
+  
 if __name__ == '__main__':
   main()