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 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 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 generatescopereg(self, source, target): if source.stereotype('pyegg:stub'): return targetclass = read_target_node(source, target.target) module = targetclass.parent blocks = module.blocks() tgv = TaggedValues(source) transform = tgv.direct('transform', 'generator:class_scope', None) or \ tgv.direct('transform', 'generator:simple_scope', None) or \ 'uml2fs' interfaces = tgv.direct('interfaces', 'generator:class_scope', None) or \ tgv.direct('interfaces', 'generator:simple_scope', None) scopename = tgv.direct('scopename', 'generator:class_scope', None) or \ tgv.direct('scopename', 'generator:simple_scope', None) or \ source.name.lower() #do some common imports imps = Imports(module) imps.set('node.ext.uml.interfaces', [ ['IOperation', None], ['IClass', None], ['IPackage', None], ['IInterface', None], ['IInterfaceRealization', None], ['IDependency', None], ['IProperty', None], ['IAssociation', None], ]) imps.set('agx.core', [ ['handler', None], ['Scope', None], ['registerScope', None], ['token', None], ]) # make the register statement if interfaces: ifstring = "[%s]" % ','.join(interfaces) else: ifstring = None if source.stereotype('generator:class_scope'): classname = source.name else: classname = 'Scope' reg = "registerScope('%s', '%s', %s, %s)" % \ (scopename, transform, ifstring, classname) # look if the reg stmt already exists regsearch = "registerScope('%s'" % scopename blockfound = None for b in blocks: for i in range(len(b.lines)): lcode = b.lines[i].strip().replace(' ', '') if lcode.startswith(regsearch): # replace the line b.lines[i] = reg return # else make a new block after the class declaration bl = Block() bl.__name__ = str(bl.uuid) bl.lines.append(reg) classes = [c for c in module.classes() if c.classname == source.name] if classes: klass = classes[0] module.insertafter(bl, klass) else: module.insertlast(bl)