def nodelevel(self): level = -1 for parent in LocationIterator(self): if IModule.providedBy(parent): break level += 1 return level
def __call__(self, source): super(PackageSyncer, self).__call__(source) if (IClass.providedBy(source) or IInterface.providedBy(source)) \ and IDirectory.providedBy(self.anchor) \ and (source.parent.stereotype('pyegg:pypackage') is not None \ or source.parent.stereotype('pyegg:pyegg') is not None): modulename = '%s.py' % IModuleNameChooser(source)() self.anchor[modulename] = Module() self.anchor = self.anchor[modulename] elif (IClass.providedBy(source) or IInterface.providedBy(source)) \ and IModule.providedBy(self.anchor) \ and (source.parent.stereotype('pyegg:pypackage') is not None \ or source.parent.stereotype('pyegg:pyegg') is not None): modulename = '%s.py' % IModuleNameChooser(source)() container = self.anchor.parent container[modulename] = Module() self.anchor = container[modulename] else: if source.parent is None: return target_node = read_target_node(source.parent, self.target) if not target_node: super(PackageSyncer, self).__call__(source) return if len(target_node.path) < len(self.anchor.path): self.anchor = target_node
def autoimport(self, source,target): """Takes classes with 'api' stereotype and imports them into the pyegg's __init__.py. """ targetob = read_target_node(source, target.target) if IModule.providedBy(targetob) or IDirectory.providedBy(targetob): init = targetob.parent['__init__.py'] else: init = targetob.parent.parent['__init__.py'] imps = Imports(init) imps.set(None, [[source.name, None]])
def generate_sqlalchemy_config(self, source, target): tgt = read_target_node(source, target.target) if IModule.providedBy(tgt): # target is a module, then its ok module = tgt target_dir = module.parent else: # fetch __init__.py module = tgt['__init__.py'] target_dir = tgt # first lets create sqla_config.py that does all the config situps fname = 'sqla_config.py' package_name = implicit_dotted_path(source) templ = JinjaTemplate() target_dir.factories[fname] = JinjaTemplate templ.template = templatepath(fname) + '.jinja' target_dir[fname] = templ target_dir[fname].params = {'engine_name':'default', 'package_name':dotted_path(source)} # now lets call config_db from the __init__.py imps = Imports(module) imps.set('sqla_config', 'config_db') main = module.functions('main')[0] term = 'config.make_wsgi_app' make_app = main.blocks(term)[0] lines = make_app.lines # find occurrence of line in block index = -1 found = False for i in range(len(lines)): if term in lines[i]: index = i if 'config_db' in lines[i]: found = True tok = token('config', True, main=main, module=module) if index != -1 and not found: lines.insert(index, 'config_db(config)')
def typeview(self, source, target): schema = getschemaclass(source, target) klass = read_target_node(source, target.target) module = schema.parent if IModule.providedBy(module): directory = module.parent else: directory = module nsmap = { None: 'http://namespaces.zope.org/zope', 'plone': 'http://namespaces.plone.org/plone', 'grok': 'http://namespaces.zope.org/grok', } zcmlfile = get_zcml(directory, 'configure.zcml', nsmap=nsmap) # include grok:grok directive if not set yet set_zcml_directive(directory, 'configure.zcml', 'grok:grok', 'package', '.') classname = '%sView' % klass.classname if module.classes(classname): view = module.classes(classname)[0] else: view = python.Class() module[uuid.uuid4()] = view view.classname = classname if not 'dexterity.DisplayForm' in view.bases: view.bases.append('dexterity.DisplayForm') context = "grok.context(%s)" % schema.classname require = "grok.require('zope2.View')" context_exists = False require_exists = False for block in view.blocks(): for line in block.lines: if line == context: context_exists = True if line == require: require_exists = True block = python.Block() block.__name__ = str(uuid.uuid4()) if not context_exists: block.lines.append(context) if not require_exists: block.lines.append(require) if block.lines: view.insertfirst(block) template = False for attr in view.attributes(): if 'template' in attr.targets: template = attr break if not template: template = python.Attribute() template.targets = ['template'] view[str(uuid.uuid4())] = template template.value = "PageTemplate('templates/%s.pt')" \ % klass.classname.lower() imp = Imports(module) imp.set('plone.directives', [['dexterity', None]]) imp.set('five', [['grok', None]]) imp.set('grokcore.view.components', [['PageTemplate', None]]) directory = module.parent template_name = '%s.pt' % klass.classname.lower() template = 'templates/%s' % template_name if not 'templates' in directory: directory['templates'] = Directory() templates = directory['templates'] templates.factories['.pt'] = XMLTemplate if template_name not in templates: pt = templates[template_name] = XMLTemplate() pt.template = 'agx.generator.dexterity:templates/displayform.pt'
def __init__(self, context): if not IModule.providedBy(context): raise ValueError(u"Given context is not an IModule implementation") self.context = context
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 typeview(self, source, target): schema = getschemaclass(source,target) klass = read_target_node(source, target.target) module = schema.parent if IModule.providedBy(module): directory = module.parent else: directory = module nsmap = { None: 'http://namespaces.zope.org/zope', 'plone': 'http://namespaces.plone.org/plone', 'grok': 'http://namespaces.zope.org/grok', } zcmlfile = get_zcml(directory, 'configure.zcml', nsmap=nsmap) # include grok:grok directive if not set yet set_zcml_directive(directory, 'configure.zcml', 'grok:grok', 'package', '.') classname = '%sView' % klass.classname if module.classes(classname): view = module.classes(classname)[0] else: view = python.Class() module[uuid.uuid4()] = view view.classname = classname if not 'dexterity.DisplayForm' in view.bases: view.bases.append('dexterity.DisplayForm') context = "grok.context(%s)" % schema.classname require = "grok.require('zope2.View')" context_exists = False require_exists = False for block in view.blocks(): for line in block.lines: if line == context: context_exists = True if line == require: require_exists = True block = python.Block() block.__name__ = str(uuid.uuid4()) if not context_exists: block.lines.append(context) if not require_exists: block.lines.append(require) if block.lines: view.insertfirst(block) template = False for attr in view.attributes(): if 'template' in attr.targets: template = attr break if not template: template = python.Attribute() template.targets = ['template'] view[str(uuid.uuid4())] = template template.value = "PageTemplate('templates/%s.pt')" \ % klass.classname.lower() imp = Imports(module) imp.set('plone.directives', [['dexterity', None]]) imp.set('five', [['grok', None]]) imp.set('grokcore.view.components', [['PageTemplate', None]]) directory = module.parent template_name = '%s.pt' % klass.classname.lower() template = 'templates/%s' % template_name if not 'templates' in directory: directory['templates'] = Directory() templates = directory['templates'] templates.factories['.pt'] = XMLTemplate if template_name not in templates: pt = templates[template_name] = XMLTemplate() pt.template = 'agx.generator.dexterity:templates/displayform.pt'