changeset 103:6b79c13bb42b

untangle `decoupage-templates` console script + minor cleanup + version bump
author Jeff Hammel <k0scist@gmail.com>
date Wed, 29 Mar 2017 09:28:21 -0700
parents 2b38f9a43f84
children ae8df6457f05
files decoupage/templates.py setup.py
diffstat 2 files changed, 42 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/decoupage/templates.py	Wed Mar 29 09:08:57 2017 -0700
+++ b/decoupage/templates.py	Wed Mar 29 09:28:21 2017 -0700
@@ -11,8 +11,12 @@
 from pkg_resources import iter_entry_points
 from pkg_resources import resource_filename
 
+
 def template_dirs():
-    """registered template directories"""
+    """
+    returns set of registered template directories
+    from `decoupage.formatters` setuptools entrypoint
+    """
 
     template_dirs = set()
     for formatter in iter_entry_points('decoupage.formatters'):
@@ -27,7 +31,8 @@
 
 
 def templates():
-    """templates"""
+    """return all registered templates"""
+
     templates = []
     for directory in template_dirs():
         templates.extend([os.path.join(directory, filename)
@@ -35,61 +40,62 @@
                           if filename.endswith('.html')])
     return templates
 
+
 def template_dict():
     """return a dict of templates"""
     return {os.path.basename(template):template for template in templates()}
 
+
 def main(args=sys.argv[1:]):
     """CLI"""
 
     # parse command line
-    description = 'list and output available templates'
+    description = """list and output available templates.
+    If no argument is given list all full paths to templates.
+    If `template` is provided, output its contents.
+    """
     parser = argparse.ArgumentParser(description=description)
     parser.add_argument('template', nargs='?',
                         help="output this template")
     parser.add_argument('-o', '--output', dest='output',
                         help="output to file or directory or stdout")
-    # TODO
     parser.add_argument('--cwd', dest='cwd',
                         help="output to current working directory")
     options = parser.parse_args(args)
 
+    # validate options
+    if options.cwd:
+        if options.output:
+            parser.error("Overspecified: `--cwd` cannot be used with `--output`")
+        options.output = os.getcwd()
+
     # retrieve templates
     _templates = template_dict()
 
     template = options.template
-    if template:
-
-        # look up template
-        if not template.endswith('.html'):
-            template = template + '.html'
-        filename = _templates.get(template)
-        if filename is None:
-            parser.error("Template '{}' not in {}".format(template, ', '.join(sorted(_templates.keys()))))
-        content = open(filename, 'r').read()
-
-        # divine output
-        output = options.output
-        if output:
-            if os.path.isdir(output):
-                output = os.path.join(output, 'index.html')
-            with open(output, 'w') as f:
-                f.write(content)
-
-            directory = os.path.dirname(os.path.abspath(output))
-            ini = os.path.join(directory, 'index.ini')
-            if not os.path.exists(ini):
-                pass
-                # TODO: output directory contents to ini
-                # if specified
-
-        else:
-            print (content)
-
-    else:
-        # list templates
+    if not template:
+        # list templates and return
         for template in templates():
             print (template)
+        return
+
+    # look up template
+    if not template.endswith('.html'):
+        template += '.html'
+    filename = _templates.get(template)
+    if not filename:
+        parser.error("Template '{}' not in {}".format(template, ', '.join(sorted(_templates.keys()))))
+    content = open(filename, 'r').read()
+
+    # divine output
+    output = options.output
+    if output:
+        if os.path.isdir(output):
+            output = os.path.join(output, 'index.html')
+        with open(output, 'w') as f:
+            f.write(content)
+    else:
+        print (content)
 
 
 if __name__ == '__main__':
--- a/setup.py	Wed Mar 29 09:08:57 2017 -0700
+++ b/setup.py	Wed Mar 29 09:28:21 2017 -0700
@@ -6,13 +6,13 @@
 except IOError:
     description = ''
 
-version = '0.14.1'
+version = '0.14.2'
 
 setup(name='decoupage',
       version=version,
       description="Decoupage is the art of decorating an object by gluing colored paper cutouts onto it in combination with special paint effects ... The software decoupage lets you stitch together index pages from filesystem content",
       long_description=description,
-      classifiers=[], # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers
+      classifiers=[],
       author='Jeff Hammel',
       author_email='k0scist@gmail.com',
       url='http://k0s.org/hg/decoupage',