Mercurial > hg > MakeItSo
annotate makeitso/file2template.py @ 182:215a71ac0eff
TODO
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 23 Mar 2014 11:53:11 -0700 |
parents | 7ac8571788e2 |
children | 55b34a5a2e5f |
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 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
9 import os |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
10 import subprocess |
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 from argparse import ArgumentParser |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
13 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
14 here = os.path.dirname(os.path.realpath(__file__)) |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
15 string = (str, unicode) |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 template = '''#!/usr/bin/env python |
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 """ |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 template |
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 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 import argparse |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 import sys |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 variables = { |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
27 {variables} |
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 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 template = """{content}""" |
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 def main(args=sys.argv[1:]): |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
33 """CLI""" |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
35 # parse command line |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
36 parser = argparse.ArgumentParser(description=__doc__) |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
37 for variable in variables: |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
38 pass |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
39 options = parser.parse_args(args) |
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 if __name__ == '__main__': |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
42 main() |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
43 ''' |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
44 |
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 class File2TemplateParser(ArgumentParser): |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
47 """argument parser for `%(prog)s`""" |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
48 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
49 def __init__(self): |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
50 ArgumentParser.__init__(self, description=__doc__) |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
51 parser.add_argument('input', nargs='?', |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
52 type=argparse.FileType('r'), default=sys.stdin, |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
53 help='input file, or read from stdin if ommitted') |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
54 parser.add_argument('variables', nargs='*', |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
55 help="variables to use") |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
56 parser.add_argument('-o', '--output', dest='output', |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
57 type=argparse.FileType('r'), default=sys.stdout, |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
58 help="output file, or stdout if ommitted'") |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
59 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
60 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
61 def main(args=sys.argv[1:]): |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
62 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
63 parser = File2TemplateParser() |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
64 options = parser.parse_args(args) |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
65 |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
66 if __name__ == '__main__': |
7ac8571788e2
add script to make templates from files
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
67 main() |