def create_site_root(self, source, target): targetclass = read_target_node(source, target.target) module = targetclass.parent if not module.functions('bootstrap'): func = Function('bootstrap') func.insertlast(Block(bootstrap_templ % source.name)) module.insertafter(func, targetclass) else: func = module.functions('bootstrap')[0] # mark the class because it has to be referenced by main token('site_root', True, site_root=source, klass=read_target_node(source, target.target), bootstrap=func)
def sqlcontentclass_engine_created_handler(self, source, target): """create and register the handler for the IEngineCreatedEvent. """ if source.stereotype('pyegg:stub'): return targetpack=read_target_node(source,target.target) targetpack['saconfig.py']=Module() module = targetpack['saconfig.py'] imps = Imports(module) # check if one of the parent packages has the z3c_saconfig stereotype z3c_saconfig=get_z3c_saconfig(source) # add engine-created handler if z3c_saconfig: eggtgv=TaggedValues(z3c_saconfig) engine_name = eggtgv.direct( 'engine_name', 'sql:z3c_saconfig', 'default') globalfuncs = [f for f in module.filtereditems(IFunction)] globalfuncnames = [f.functionname for f in globalfuncs] if 'engineCreatedHandler' not in globalfuncnames: imps.set('z3c.saconfig.interfaces', [['IEngineCreatedEvent', None]]) imps.set('zope', [['component', None]]) imps.set('z3c.saconfig.interfaces',[['IEngineFactory',None]]) imps.set('sqlalchemy.ext.declarative','declarative_base') #att = [att for att in module.filtereditems(IAttribute) \ # if att.targets == ['Base']][0] bases=module.attributes('Base') if not bases: base=Attribute('Base','declarative_base()') module.insertafterimports(base) else: base=bases[0] ff = Function('engineCreatedHandler') # 'engineCreatedHandler' ff.__name__ = ff.uuid ff.args = ('event',) dec = Decorator('component.adapter') dec.__name__ = dec.uuid dec.args = ('IEngineCreatedEvent',) ff.insertfirst(dec) block_lines = [ "fact = component.queryUtility(IEngineFactory, '%s')" \ % engine_name, "if fact and fact._args == event.engine_args:", " Base.metadata.create_all(event.engine)", ] block = Block('\n'.join(block_lines)) block.__name__ = block.uuid ff.insertlast(block) module.insertafter(ff, base) prov = Block('component.provideHandler(engineCreatedHandler)') prov.__name__ = prov.uuid module.insertafter(prov, ff)
def create_register_func(self, source, target): """Creates the register function. """ if not token(str(source.uuid),True,is_generator_egg=False).is_generator_egg: return init = read_target_node(source, target.target)['__init__.py'] fname = 'register' path = dotted_path(source) if fname not in [f.functionname for f in init.functions()]: f = Function() f.functionname = fname f.__name__ = str(f.uuid) bl = Block() bl.__name__ = str(bl.uuid) bl.lines.append('"""register this generator"""') bl.lines.append("import %s" % path) bl.lines.append("from agx.core.config import register_generator") bl.lines.append("register_generator(%s)" % path) f.insertfirst(bl) init[f.name] = f
def generatescopeclass(self, source, target): """Generates scope classes. """ if source.stereotype('pyegg:stub'): return targetclass = read_target_node(source, target.target) module = targetclass.parent methods = [m for m in targetclass.functions()] methnames = [m.functionname for m in methods] methname = '__call__' tgv = TaggedValues(source) stereotypes = tgv.direct('stereotypes', 'generator:class_scope', None) or \ tgv.direct('stereotypes', 'generator:simple_scope', None) or [] transform = tgv.direct('transform', 'generator:class_scope', None) or \ tgv.direct('transform', 'generator:simple_scope', None) or \ 'uml2fs' if stereotypes: for st in stereotypes: if ':' not in st: msg = 'you have to specify the stereotype in the form ' + \ '"namespace:stereotype", but forgot the namespace ' + \ '(scope %s, stereotype %s)' % (source.name, st) raise ValueError(msg) #if not stereotypes: # msg = 'scope %s must have a stereotype attribute!!' % source.name # raise ValueError(msg) if 'Scope' not in targetclass.bases: targetclass.bases.append('Scope') if methname not in methnames: f = Function() f.functionname = methname f.args = ['self', 'node'] f.__name__ = str(f.uuid) targetclass.insertfirst(f) bl = Block() bl.__name__ = str(bl.uuid) conds = [ "node.stereotype('%s') is not None" % st for st in stereotypes ] cond = ' or '.join(conds) bl.lines.append("return %s" % cond) f.insertfirst(bl)
def generatescopeclass(self, source, target): """Generates scope classes. """ if source.stereotype('pyegg:stub'): return targetclass = read_target_node(source, target.target) module = targetclass.parent methods = [m for m in targetclass.functions()] methnames = [m.functionname for m in methods] methname = '__call__' tgv = TaggedValues(source) stereotypes = tgv.direct('stereotypes', 'generator:class_scope', None) or \ tgv.direct('stereotypes', 'generator:simple_scope', None) or [] transform = tgv.direct('transform', 'generator:class_scope', None) or \ tgv.direct('transform', 'generator:simple_scope', None) or \ 'uml2fs' if stereotypes: for st in stereotypes: if ':' not in st: msg = 'you have to specify the stereotype in the form ' + \ '"namespace:stereotype", but forgot the namespace ' + \ '(scope %s, stereotype %s)' % (source.name, st) raise ValueError(msg) #if not stereotypes: # msg = 'scope %s must have a stereotype attribute!!' % source.name # raise ValueError(msg) if 'Scope' not in targetclass.bases: targetclass.bases.append('Scope') if methname not in methnames: f = Function() f.functionname = methname f.args = ['self', 'node'] f.__name__ = str(f.uuid) targetclass.insertfirst(f) bl = Block() bl.__name__ = str(bl.uuid) conds = ["node.stereotype('%s') is not None" % st for st in stereotypes] cond = ' or '.join(conds) bl.lines.append("return %s" % cond) f.insertfirst(bl)
def create_register_func(self, source, target): """Creates the register function. """ if not token(str(source.uuid), True, is_generator_egg=False).is_generator_egg: return init = read_target_node(source, target.target)['__init__.py'] fname = 'register' path = dotted_path(source) if fname not in [f.functionname for f in init.functions()]: f = Function() f.functionname = fname f.__name__ = str(f.uuid) bl = Block() bl.__name__ = str(bl.uuid) bl.lines.append('"""register this generator"""') bl.lines.append("import %s" % path) bl.lines.append("from agx.core.config import register_generator") bl.lines.append("register_generator(%s)" % path) f.insertfirst(bl) init[f.name] = f
def generate_configuration(self, source, target): tgt = read_target_node(source, target.target) if IModule.providedBy(tgt): # target is a module, then its ok module = tgt else: # fetch __init__.py module = tgt['__init__.py'] tok = token('pyramid_configuration', True, imports=[], static_views=[], scans=[]) imps = Imports(module) imps.set('wsgiref.simple_server', 'make_server') imps.set('pyramid.config', 'Configurator') # create a function main if not present if 'main' not in [f.functionname for f in module.functions()]: func = Function('main') func.args = ['global_config', '**settings'] module.insertafterimports(func) main = module.functions(name='main')[0] try: srtok = token('site_root', False) bootstrap = srtok.bootstrap except ComponentLookupError: bootstrap = None if not main.blocks('Configurator'): if bootstrap: import_if_necessary(module, bootstrap) main.insertfirst(Block('config = Configurator(root_factory=bootstrap)')) else: main.insertfirst(Block('config = Configurator()')) # do the configurator stuff mainblock = main.blocks('Configurator')[0] # importmes imptok = token('pyramid_importmes', True, packages=[]) for pack in imptok.packages: mainblock.insertlineafter( "config.include('%s')" % pack,'Configurator', ifnotpresent=True) # static views for sv in tok.static_views: mainblock.insertlineafter('''config.add_static_view('%s', '%s/',%s)''' % \ (sv[0], sv[1], sv[2]), 'Configurator', ifnotpresent=True) # scans for scan in tok.scans: if scan: mainblock.insertlineafter("config.scan('%s')" % scan, 'add_static_view', ifnotpresent=True) else: mainblock.insertlineafter("config.scan()" , 'add_static_view', ifnotpresent=True) # insert app stuff at end of block mainblock.appendline("app = config.make_wsgi_app()", ifnotpresent=True) mainblock.appendline("return app", ifnotpresent=True) if not module.blocks('__main__'): module.insertlast(Block('''if __name__ == '__main__': app = main() server = make_server('0.0.0.0', 8080, app) server.serve_forever()'''))
def generate_view_class(self, source, target): targetklass = read_target_node(source, target.target) try: module = targetklass.parent except AttributeError: # if there is no parent, this handler is not responsible return tdir = module.parent try: tok = token(str(source.uuid), False) has_view_methods = tok.has_view_methods except: has_view_methods = False view_class = source.stereotype('pyramid:view_class') # create init method if view_class or has_view_methods: inits = targetklass.functions('__init__') if inits: init = inits[0] else: init = Function('__init__') targetklass.insertfirst(init) # import pdb;pdb.set_trace() init.args = ['context', 'request'] if not init.blocks('self.request'): init.insertfirst(Block('self.request = request')) if not init.blocks('self.context'): init.insertfirst(Block('self.context = context')) # if its a view_class create the global_template if view_class: gtemplate = view_class.taggedvalue('global_template').value from_gtemplate = view_class.taggedvalue('from_global_template').value create_template_file(tdir, gtemplate, from_gtemplate) imps = Imports(module) imps.set('pyramid.renderers', 'get_renderer') if not init.blocks('global_template'): init.insertlast(Block('renderer = get_renderer("%s")' % gtemplate)) init.insertlast(Block('self.global_template = renderer.implementation()')) init.insertlast(Block('self.macros = self.global_template.macros'))