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_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 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'))