Mercurial > hg > MakeItSo
comparison makeitso/main.py @ 3:8931be6d2c8a
stub for getting missing variables
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 04 Nov 2010 19:12:56 -0700 |
parents | 825922315ce6 |
children |
comparison
equal
deleted
inserted
replaced
2:825922315ce6 | 3:8931be6d2c8a |
---|---|
17 cmdstr = command | 17 cmdstr = command |
18 else: | 18 else: |
19 cmdstr = ' '.join(command) | 19 cmdstr = ' '.join(command) |
20 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code)) | 20 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code)) |
21 | 21 |
22 def get_missing(name_error): | |
23 """ | |
24 This is a horrible hack because python doesn't do the proper thing | |
25 via eval and return the name of the variable; instead, it just gives | |
26 you a message: | |
27 >>> try: | |
28 ... eval('2*foo') | |
29 ... except Exception, e: | |
30 ... pass | |
31 """ | |
32 message = name_error.args[0] | |
33 varname = message.split("'")[1] | |
34 return varname | |
35 | |
36 def missing_variables(template, variables): | |
37 """return additional variables needed""" | |
38 vars = variables.copy() | |
39 missing = set([]) | |
40 while True: | |
41 try: | |
42 template.substitute(**missing) | |
43 return missing | |
44 except NameError, e: | |
45 missed = get_missing(e) | |
46 missing.add(missed) | |
47 vars[missed] = '' | |
48 | |
22 def template_variables(template): | 49 def template_variables(template): |
23 """return the variables needed for a template""" | 50 """return the variables needed for a template""" |
24 raise NotImplementedError | 51 return missing_variables(template, {}) |
25 | 52 |
26 def main(args=sys.argv[1:]): | 53 def main(args=sys.argv[1:]): |
27 | 54 |
28 # create option parser | 55 # create option parser |
29 usage = '%prog [options]' | 56 usage = '%prog [options]' |
30 parser = OptionParser(usage, description=__doc__) | 57 parser = OptionParser(usage, description=__doc__) |
31 parser.add_option('--variables', dest='variables', | 58 parser.add_option('--variables', dest='variables', action='store_true', |
32 help='print the variables in a template') | 59 help='print the variables in a template') |
33 options, args = parser.parse_args(args) | 60 options, args = parser.parse_args(args) |
61 | |
62 if options.variables: | |
63 variables = template_variables() # TODO: pass template | |
64 return | |
34 | 65 |
35 # template variables | 66 # template variables |
36 variables = {} | 67 variables = {} |
37 _vars = [] | 68 _vars = [] |
38 _args = [] | 69 _args = [] |
53 else: | 84 else: |
54 content = sys.stdin.read() | 85 content = sys.stdin.read() |
55 template = Template(content) | 86 template = Template(content) |
56 print template.substitute(**variables) | 87 print template.substitute(**variables) |
57 | 88 |
58 | |
59 if __name__ == '__main__': | 89 if __name__ == '__main__': |
60 main() | 90 main() |