comparison makeitso/makeitso.py @ 268:64979cfff465 default tip

some py3 fixes
author Jeff Hammel <k0scist@gmail.com>
date Tue, 29 May 2018 15:28:41 -0700
parents 7e3a32f2794a
children
comparison
equal deleted inserted replaced
267:7e3a32f2794a 268:64979cfff465
14 import sys 14 import sys
15 import tempfile 15 import tempfile
16 import urllib 16 import urllib
17 # TODO: may have to use urllib2.urlopen to get reasonable timeouts 17 # TODO: may have to use urllib2.urlopen to get reasonable timeouts
18 18
19 from ConfigParser import RawConfigParser 19 try:
20 # python 2
21 from ConfigParser import RawConfigParser
22 except ImportError:
23 # python 3
24 from configparser import RawConfigParser
25
26 try:
27 # python 2
28 string = (unicode, str)
29 except NameError:
30 # python 3
31 string = (str,)
32
33
20 from optparse import OptionParser 34 from optparse import OptionParser
21 35
22 # URL of -this file- 36 # URL of -this file-
23 location = 'http://k0s.org/mozilla/hg/MakeItSo/raw-file/tip/makeitso/makeitso.py' 37 location = 'http://k0s.org/mozilla/hg/MakeItSo/raw-file/tip/makeitso/makeitso.py'
24 38
212 # interpolate 226 # interpolate
213 content = ContentTemplate.substitute(self, **variables) 227 content = ContentTemplate.substitute(self, **variables)
214 228
215 # write output 229 # write output
216 output = output or sys.stdout 230 output = output or sys.stdout
217 if isinstance(output, basestring): 231 if isinstance(output, string):
218 path = output 232 path = output
219 if os.path.isdir(output): 233 if os.path.isdir(output):
220 path = os.path.join(path, basename(self.name)) 234 path = os.path.join(path, basename(self.name))
221 f = file(path, 'w') 235 f = file(path, 'w')
222 print >> f, content 236 print >> f, content
327 def __init__(self, templates, interactive=True, variables=None): 341 def __init__(self, templates, interactive=True, variables=None):
328 self.interactive = interactive 342 self.interactive = interactive
329 self.templates = [] 343 self.templates = []
330 entry_points = get_entry_points() 344 entry_points = get_entry_points()
331 for template in templates: 345 for template in templates:
332 if isinstance(template, basestring): 346 if isinstance(template, string):
333 # TODO: check if the template is a [e.g] PasteScript.template entry point 347 # TODO: check if the template is a [e.g] PasteScript.template entry point
334 if os.path.isdir(template): 348 if os.path.isdir(template):
335 self.templates.append(DirectoryTemplate(template, interactive=self.interactive, variables=variables)) 349 self.templates.append(DirectoryTemplate(template, interactive=self.interactive, variables=variables))
336 elif not os.path.exists(template) and template in entry_points: 350 elif not os.path.exists(template) and template in entry_points:
337 self.templates.append(entry_points[template](interactive=interactive, variables=variables)) 351 self.templates.append(entry_points[template](interactive=interactive, variables=variables))
355 missing.update(missed) 369 missing.update(missed)
356 vars.update(dict([(i, '') for i in missed])) 370 vars.update(dict([(i, '') for i in missed]))
357 return missing 371 return missing
358 372
359 def check_output(self, output): 373 def check_output(self, output):
360 if output and isinstance(output, basestring) and os.path.exists(output) and len(self.templates) > 1: 374 if output and isinstance(output, string) and os.path.exists(output) and len(self.templates) > 1:
361 assert os.path.isdir(output), "Must specify a directory for multiple templates" 375 assert os.path.isdir(output), "Must specify a directory for multiple templates"
362 for template in self.templates: 376 for template in self.templates:
363 if hasattr(template, 'check_output'): 377 if hasattr(template, 'check_output'):
364 template.check_output(output) 378 template.check_output(output)
365 379