Mercurial > hg > MakeItSo
annotate makeitso/file2template.py @ 185:0fd0cc154153
STUB: makeitso/makeitso.py setup.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 26 Mar 2014 21:28:47 -0700 |
parents | be33152d7d0c |
children |
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 | 7 # TODO: shell script extension |
181
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
8 |
183 | 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 | 25 variables = dict({variables}) |
181
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 |
183 | 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 | 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 | 47 argparse.ArgumentParser.__init__(self, description=__doc__) |
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 | 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 | 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 | 59 """CLI""" |
181
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
60 |
183 | 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 | 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 | |
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() |