annotate makeitso/file2template.py @ 222:d07bd979592b

one more time
author Jeff Hammel <k0scist@gmail.com>
date Mon, 14 Dec 2015 13:43:08 -0800
parents be33152d7d0c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 convert a file to a template
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
182
Jeff Hammel <k0scist@gmail.com>
parents: 181
diff changeset
7 # TODO: shell script extension
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8
183
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
9 import argparse
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import os
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import sys
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 here = os.path.dirname(os.path.realpath(__file__))
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 string = (str, unicode)
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 template = '''#!/usr/bin/env python
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 """
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 template
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 """
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 import argparse
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 import sys
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
183
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
25 variables = dict({variables})
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
183
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
27 template = """{template}"""
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 def main(args=sys.argv[1:]):
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 """CLI"""
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 # parse command line
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 parser = argparse.ArgumentParser(description=__doc__)
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 for variable in variables:
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 pass
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 options = parser.parse_args(args)
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 if __name__ == '__main__':
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 main()
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 '''
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42
183
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
43 class File2TemplateParser(argparse.ArgumentParser):
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 """argument parser for `%(prog)s`"""
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 def __init__(self):
183
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
47 argparse.ArgumentParser.__init__(self, description=__doc__)
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
48 self.add_argument('input', nargs='?',
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 type=argparse.FileType('r'), default=sys.stdin,
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 help='input file, or read from stdin if ommitted')
183
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
51 self.add_argument('variables', nargs='*',
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 help="variables to use")
183
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
53 self.add_argument('-o', '--output', dest='output',
184
be33152d7d0c STUB: makeitso/file2template.py
Jeff Hammel <k0scist@gmail.com>
parents: 183
diff changeset
54 type=argparse.FileType('w'), default=sys.stdout,
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 help="output file, or stdout if ommitted'")
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 def main(args=sys.argv[1:]):
183
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
59 """CLI"""
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60
183
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
61 # parse command line
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 parser = File2TemplateParser()
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 options = parser.parse_args(args)
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64
183
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
65 # get variable values
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
66 lines = []
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
67 for v in options.variables:
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
68 if '=' in v:
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
69 key, value = v.split('=', 1)
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
70 else:
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
71 key = v
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
72 value = 'None'
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
73 lines.append('{}: {},'.format(repr(key), value))
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
74 varstring = '\n'.join(lines)
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
75
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
76 # read the content
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
77
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
78 # interpolate the template
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
79 output = template.format(variables=varstring, template='')
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
80 options.output.write(output)
55b34a5a2e5f basic form now works
Jeff Hammel <k0scist@gmail.com>
parents: 182
diff changeset
81
181
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82 if __name__ == '__main__':
7ac8571788e2 add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83 main()