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