Mercurial > hg > decoupage
comparison decoupage/templates.py @ 91:4a9c5cf9fec9
STUB: decoupage/templates.py decoupage/index.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sat, 29 Mar 2014 17:17:21 -0700 |
parents | 78139c3cecfa |
children | 6b79c13bb42b |
comparison
equal
deleted
inserted
replaced
90:55719bcdc2c2 | 91:4a9c5cf9fec9 |
---|---|
2 | 2 |
3 """ | 3 """ |
4 functionality related to templates | 4 functionality related to templates |
5 """ | 5 """ |
6 | 6 |
7 import argparse | |
7 import os | 8 import os |
8 import sys | 9 import sys |
9 from optparse import OptionParser | 10 from .index import index |
10 from pkg_resources import iter_entry_points | 11 from pkg_resources import iter_entry_points |
11 from pkg_resources import resource_filename | 12 from pkg_resources import resource_filename |
12 | 13 |
13 def template_dirs(): | 14 def template_dirs(): |
15 """registered template directories""" | |
16 | |
14 template_dirs = set() | 17 template_dirs = set() |
15 for formatter in iter_entry_points('decoupage.formatters'): | 18 for formatter in iter_entry_points('decoupage.formatters'): |
16 try: | 19 try: |
17 formatter.load() | 20 formatter.load() |
18 except: | 21 except: |
22 template_dirs.add(template_dir) | 25 template_dirs.add(template_dir) |
23 return template_dirs | 26 return template_dirs |
24 | 27 |
25 | 28 |
26 def templates(): | 29 def templates(): |
30 """templates""" | |
27 templates = [] | 31 templates = [] |
28 for directory in template_dirs(): | 32 for directory in template_dirs(): |
29 templates.extend([os.path.join(directory, filename) | 33 templates.extend([os.path.join(directory, filename) |
30 for filename in os.listdir(directory) | 34 for filename in os.listdir(directory) |
31 if filename.endswith('.html')]) | 35 if filename.endswith('.html')]) |
32 return templates | 36 return templates |
33 | 37 |
38 def template_dict(): | |
39 """return a dict of templates""" | |
40 return {os.path.basename(template):template for template in templates()} | |
34 | 41 |
35 def main(args=sys.argv[1:]): | 42 def main(args=sys.argv[1:]): |
43 """CLI""" | |
36 | 44 |
37 # comman line option parser | 45 # parse command line |
38 description = 'list available templates' | 46 description = 'list and output available templates' |
39 parser = OptionParser(description=description) | 47 parser = argparse.ArgumentParser(description=description) |
40 options, args = parser.parse_args(args) | 48 parser.add_argument('template', nargs='?', |
49 help="output this template") | |
50 parser.add_argument('-o', '--output', dest='output', | |
51 help="output to file or directory or stdout") | |
52 # TODO | |
53 parser.add_argument('--cwd', dest='cwd', | |
54 help="output to current working directory") | |
55 options = parser.parse_args(args) | |
41 | 56 |
42 # list templates | 57 # retrieve templates |
43 for template in templates(): | 58 _templates = template_dict() |
44 print template | 59 |
60 template = options.template | |
61 if template: | |
62 | |
63 # look up template | |
64 if not template.endswith('.html'): | |
65 template = template + '.html' | |
66 filename = _templates.get(template) | |
67 if filename is None: | |
68 parser.error("Template '{}' not in {}".format(template, ', '.join(sorted(_templates.keys())))) | |
69 content = open(filename, 'r').read() | |
70 | |
71 # divine output | |
72 output = options.output | |
73 if output: | |
74 if os.path.isdir(output): | |
75 output = os.path.join(output, 'index.html') | |
76 with open(output, 'w') as f: | |
77 f.write(content) | |
78 | |
79 directory = os.path.dirname(os.path.abspath(output)) | |
80 ini = os.path.join(directory, 'index.ini') | |
81 if not os.path.exists(ini): | |
82 pass | |
83 # TODO: output directory contents to ini | |
84 # if specified | |
85 | |
86 else: | |
87 print (content) | |
88 | |
89 else: | |
90 # list templates | |
91 for template in templates(): | |
92 print (template) | |
45 | 93 |
46 | 94 |
47 if __name__ == '__main__': | 95 if __name__ == '__main__': |
48 main() | 96 main() |