comparison makeitso/makeitso.py @ 107:2142ad247eb6

we can has entry points
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 14 Jan 2011 07:29:23 -0800
parents 1295df1700a4
children 32893f67f85d
comparison
equal deleted inserted replaced
106:1295df1700a4 107:2142ad247eb6
323 323
324 class PolyTemplate(ContentTemplate): 324 class PolyTemplate(ContentTemplate):
325 """template for several files/directories""" 325 """template for several files/directories"""
326 326
327 def __init__(self, templates, interactive=True, variables=None): 327 def __init__(self, templates, interactive=True, variables=None):
328
329 self.interactive = interactive 328 self.interactive = interactive
330 self.templates = [] 329 self.templates = []
330 entry_points = get_entry_points()
331 for template in templates: 331 for template in templates:
332 if isinstance(template, basestring): 332 if isinstance(template, basestring):
333 # TODO: check if the template is a [e.g] PasteScript.template entry point 333 # TODO: check if the template is a [e.g] PasteScript.template entry point
334 if os.path.isdir(template): 334 if os.path.isdir(template):
335 self.templates.append(DirectoryTemplate(template, interactive=self.interactive, variables=variables)) 335 self.templates.append(DirectoryTemplate(template, interactive=self.interactive, variables=variables))
336 elif not os.path.exists(template) and template in entry_points:
337 self.templates.append(entry_points[template](interactive=interactive, variables=variables))
336 else: 338 else:
337 self.templates.append(URITemplate(template, interactive=self.interactive, variables=variables)) 339 self.templates.append(URITemplate(template, interactive=self.interactive, variables=variables))
338 else: 340 else:
339 # assume the template is an object that conforms to the API 341 # assume the template is an object that conforms to the API
340 self.templates.append(template) 342 self.templates.append(template)
510 512
511 513
512 # get templates from pkg_resources 514 # get templates from pkg_resources
513 # (MakeItSo! and [TODO] pastescript templates) 515 # (MakeItSo! and [TODO] pastescript templates)
514 # this should go last to ensure the module is wholly loaded 516 # this should go last to ensure the module is wholly loaded
515 def get_entry_points(name): 517 def get_entry_points():
518 retval = {}
516 try: 519 try:
517 from pkg_resources import iter_entry_points 520 from pkg_resources import iter_entry_points
518 for i in iter_entry_points('makeitso.templates'): 521 for i in iter_entry_points('makeitso.templates'):
519 pass # :( 522 try:
523 retval[i.name] = i.load()
524 except:
525 continue
520 526
521 except ImportError: 527 except ImportError:
522 return None 528 return retval
529 return retval