comparison makeitso/makeitso.py @ 26:044ac9e0b29c

add a non-working example with an include clause
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 13 Dec 2010 21:03:39 -0800
parents fbf19940fa97
children ac44c36da885
comparison
equal deleted inserted replaced
25:fbf19940fa97 26:044ac9e0b29c
19 19
20 # URL of tempita 20 # URL of tempita
21 tempita_location = 'http://bitbucket.org/ianb/tempita/raw-file/tip/tempita/' 21 tempita_location = 'http://bitbucket.org/ianb/tempita/raw-file/tip/tempita/'
22 22
23 def cleanup(): 23 def cleanup():
24
25 # remove temporary net module directory 24 # remove temporary net module directory
26 if 'tempdir' in globals(): 25 if 'tempdir' in globals():
27 shutil.remove(tempdir) 26 shutil.remove(tempdir)
28 27
29 try: 28 try:
78 cmdstr = command 77 cmdstr = command
79 else: 78 else:
80 cmdstr = ' '.join(command) 79 cmdstr = ' '.join(command)
81 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code)) 80 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code))
82 81
82 def base_uri(uri):
83 if '://' in uri:
84 return 'uri'.rsplit('/', 1)[0]
85 return os.path.dirname(os.path.abspath('me'))
86
87 def include(uri):
88 f, headers = urllib.urlretrieve(uri)
89 return file(f).read()
90
91 ### functions for variables
92
93 defaults = {'include': include}
94
83 def get_missing(name_error): 95 def get_missing(name_error):
84 """ 96 """
85 This is a horrible hack because python doesn't do the proper thing 97 This is a horrible hack because python doesn't do the proper thing
86 via eval and return the name of the variable; instead, it just gives 98 via eval and return the name of the variable; instead, it just gives
87 you a message: 99 you a message:
124 136
125 # remove makeitso shebang if it has one 137 # remove makeitso shebang if it has one
126 if shebang_re.match(content): 138 if shebang_re.match(content):
127 content = os.linesep.join(content.splitlines()[1:]) 139 content = os.linesep.join(content.splitlines()[1:])
128 140
129 variables = variables or {} 141 variables = variables or defaults.copy()
130 template = tempita.Template(content) 142 template = tempita.Template(content)
131 missing = missing_variables(template, variables) 143 missing = missing_variables(template, variables)
132 if missing: 144 if missing:
133 # TODO: add a switch for interactive or not 145 # TODO: add a switch for interactive or not
134 variables.update(read_variables(missing)) 146 variables.update(read_variables(missing))
178 for variable in variables: 190 for variable in variables:
179 print variable 191 print variable
180 return 192 return
181 193
182 # template variables 194 # template variables
183 variables = {} 195 variables = defaults.copy()
184 _vars = [] 196 _vars = []
185 _args = [] 197 _args = []
186 for arg in args: 198 for arg in args:
187 if '=' in arg: 199 if '=' in arg:
188 key, value = arg.split('=') 200 key, value = arg.split('=')
195 if options.commandline: 207 if options.commandline:
196 if args: 208 if args:
197 for arg in args: 209 for arg in args:
198 print invocation(arg, **variables) 210 print invocation(arg, **variables)
199 else: 211 else:
200 print invocation('[URL]', **variables) 212 print invocation('[URI]', **variables)
201 return 213 return
202 214
203 # get the content 215 # get the content
204 if args: 216 if args:
205 for arg in args: 217 for arg in args:
206 f, headers = urllib.urlretrieve(arg) 218 var_copy = variables.copy()
207 content = file(f).read() 219 var_copy['here'] = base_uri(arg)
208 print substitute(content, variables=variables) 220 content = include(arg)
221 print substitute(content, variables=var_copy)
209 else: 222 else:
210 content = sys.stdin.read() 223 content = sys.stdin.read()
211 print substitute(content, variables=variables) 224 print substitute(content, variables=variables)
212 225
213 # cleanup 226 # cleanup