Mercurial > hg > decoupage
annotate decoupage/templates.py @ 83:78139c3cecfa
CLI
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sun, 15 Dec 2013 12:38:24 -0800 |
parents | 16d41af2d8ef |
children | 4a9c5cf9fec9 |
rev | line source |
---|---|
3
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
2 |
83 | 3 """ |
4 functionality related to templates | |
5 """ | |
6 | |
3
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
7 import os |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
8 import sys |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
9 from optparse import OptionParser |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
10 from pkg_resources import iter_entry_points |
8 | 11 from pkg_resources import resource_filename |
3
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
12 |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
13 def template_dirs(): |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
14 template_dirs = set() |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
15 for formatter in iter_entry_points('decoupage.formatters'): |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
16 try: |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
17 formatter.load() |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
18 except: |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
19 continue |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
20 template_dir = resource_filename(formatter.module_name, 'templates') |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
21 if os.path.isdir(template_dir): |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
22 template_dirs.add(template_dir) |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
23 return template_dirs |
83 | 24 |
25 | |
8 | 26 def templates(): |
27 templates = [] | |
28 for directory in template_dirs(): | |
83 | 29 templates.extend([os.path.join(directory, filename) |
8 | 30 for filename in os.listdir(directory) |
31 if filename.endswith('.html')]) | |
32 return templates | |
33 | |
83 | 34 |
3
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
35 def main(args=sys.argv[1:]): |
83 | 36 |
37 # comman line option parser | |
38 description = 'list available templates' | |
39 parser = OptionParser(description=description) | |
40 options, args = parser.parse_args(args) | |
41 | |
42 # list templates | |
8 | 43 for template in templates(): |
44 print template | |
3
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
45 |
83 | 46 |
3
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
47 if __name__ == '__main__': |
ea5a5ef8ae2e
add stub for template printing; does not work yet
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
48 main() |