comparison makeitso/makeitso.py @ 16:1818cc524cde

change API making substitute return the substituted quantity
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 15 Nov 2010 18:28:10 -0800
parents edabb0bfe107
children c879b93c3f15
comparison
equal deleted inserted replaced
15:edabb0bfe107 16:1818cc524cde
8 import subprocess 8 import subprocess
9 import sys 9 import sys
10 import urllib 10 import urllib
11 11
12 from optparse import OptionParser 12 from optparse import OptionParser
13
14 13
15 try: 14 try:
16 import tempita 15 import tempita
17 except ImportError: 16 except ImportError:
18 raise NotImplementedError 17 raise NotImplementedError
74 for i in variables: 73 for i in variables:
75 print 'Enter %s: ' % i, 74 print 'Enter %s: ' % i,
76 retval[i] = raw_input() 75 retval[i] = raw_input()
77 return retval 76 return retval
78 77
79 def substitute(content, fp=sys.stdout, variables=None): 78 def substitute(content, variables=None):
79 """interactive (for now) substitution"""
80 80
81 # remove makeitso shebang if it has one 81 # remove makeitso shebang if it has one
82 if shebang_re.match(content): 82 if shebang_re.match(content):
83 content = os.linesep.join(content.splitlines()[1:]) 83 content = os.linesep.join(content.splitlines()[1:])
84 84
86 template = tempita.Template(content) 86 template = tempita.Template(content)
87 missing = missing_variables(template, variables) 87 missing = missing_variables(template, variables)
88 if missing: 88 if missing:
89 # TODO: add a switch for interactive or not 89 # TODO: add a switch for interactive or not
90 variables.update(read_variables(missing)) 90 variables.update(read_variables(missing))
91 print >> fp, template.substitute(**variables) 91 return template.substitute(**variables)
92 92
93 def invocation(url, **variables): 93 def invocation(url, **variables):
94 """returns a string appropriate for TTW invocation""" 94 """returns a string appropriate for TTW invocation"""
95 variables_string = ' '.join(['%s=%s' % (i,j) for i,j in variables.items()]) 95 variables_string = ' '.join(['%s=%s' % (i,j) for i,j in variables.items()])
96 return 'python <(curl %s) %s %s' % (location, url, variables_string) 96 return 'python <(curl %s) %s %s' % (location, url, variables_string)
159 # get the content 159 # get the content
160 if args: 160 if args:
161 for arg in args: 161 for arg in args:
162 f, headers = urllib.urlretrieve(arg) 162 f, headers = urllib.urlretrieve(arg)
163 content = file(f).read() 163 content = file(f).read()
164 substitute(content, variables=variables) 164 print substitute(content, variables=variables)
165 else: 165 else:
166 content = sys.stdin.read() 166 content = sys.stdin.read()
167 substitute(content, variables=variables) 167 print substitute(content, variables=variables)
168 168
169 if __name__ == '__main__': 169 if __name__ == '__main__':
170 main() 170 main()