def resourcedirectory(self, source, target): """Create resource directory and register in ZCML. """ egg = egg_source(source) eggname = egg.name targetdir = read_target_node(source, target.target) if 'resources' not in targetdir.keys(): targetdir['resources'] = Directory() path = targetdir.path path.append('browser.zcml') fullpath = os.path.join(*path) if 'browser.zcml' not in targetdir: zcml = ZCMLFile(fullpath) zcml.nsmap['browser'] = 'http://namespaces.zope.org/browser' targetdir['browser.zcml'] = zcml else: zcml = targetdir['browser.zcml'] addZcmlRef(targetdir, zcml) # add the resourceDirectory stmt zcautils.set_zcml_directive(targetdir, 'browser.zcml', 'include', 'package', "zope.browserresource", file="meta.zcml") if not zcml.filter( tag='browser:resourceDirectory', attr='name', value=eggname): directory = SimpleDirective(name='browser:resourceDirectory', parent=zcml) directory.attrs['name'] = eggname directory.attrs['directory'] = 'resources'
def addZcmlRef(directory, zcml): """Adds a reference to a zcml file and implicitly taks care for creating a configure.zcml""" # collect zcmls to determine whether to include configure.zcml. path = directory.path path.append(confname) # fullpath = os.path.join(*path) if confname not in directory.keys(): # conf = ZCMLFile(fullpath) conf = ZCMLFile() directory[confname] = conf else: conf = directory[confname] zcmlpath = relpath(directory,zcml) if zcmlpath != 'configure.zcml': if zcml.name == 'configure.zcml': packname = '.' + zcml.parent.name found_include = conf.filter( tag='include', attr='package', value=packname) # add include directive if necessary if not found_include: include = SimpleDirective(name='include', parent=conf) include.attrs['package'] = packname else: found_include = conf.filter(tag='include', attr='file', value=zcmlpath) # add include directive if necessary if not found_include: include = SimpleDirective(name='include', parent=conf) include.attrs['file'] = zcmlpath if directory not in token('pyeggs', False).directories: parentdir = directory.__parent__ addZcmlRef(parentdir, conf)
def resourcedirectory(self, source, target): """Create resource directory and register in ZCML. """ egg = egg_source(source) eggname = egg.name targetdir = read_target_node(source, target.target) if 'resources' not in targetdir.keys(): targetdir['resources'] = Directory() path = targetdir.path path.append('browser.zcml') fullpath = os.path.join(*path) if 'browser.zcml' not in targetdir: zcml = ZCMLFile(fullpath) zcml.nsmap['browser'] = 'http://namespaces.zope.org/browser' targetdir['browser.zcml'] = zcml else: zcml = targetdir['browser.zcml'] addZcmlRef(targetdir, zcml) # add the resourceDirectory stmt zcautils.set_zcml_directive(targetdir, 'browser.zcml', 'include', 'package', "zope.browserresource", file="meta.zcml") if not zcml.filter( tag='browser:resourceDirectory', attr='name', value=eggname): directory = SimpleDirective( name='browser:resourceDirectory', parent=zcml) directory.attrs['name'] = eggname directory.attrs['directory'] = 'resources'
def zcaadapts(self, source, target): adapter = source if adapter.stereotype('pyegg:function'): # XXX: <<function>> <<adapter>> on class return targetadapter = read_target_node(adapter, target.target) tok = token(str(adapter.uuid), True) pack = source.parent target = read_target_node(pack, target.target) targetclass = read_target_node(adapter, target) if isinstance(target, python.Module): targetdir = target.parent else: targetdir = target path = targetdir.path path.append('adapters.zcml') fullpath = os.path.join(*path) if 'adapters.zcml' not in targetdir.keys(): zcml = ZCMLFile(fullpath) targetdir['adapters.zcml'] = zcml else: zcml = targetdir['adapters.zcml'] addZcmlRef(targetdir, zcml) targettok = token(str(targetclass.uuid), True, realizes=[], provides=None) if not hasattr(tok, 'adapts'): msg = 'adapter class %s has no <<adapts>> dependency' \ % dotted_path(adapter) raise ValueError(msg) _for = [token(str(adaptee.uuid), False).fullpath for adaptee in tok.adapts] factory = class_full_name(targetadapter) tgv = TaggedValues(adapter) name = tgv.direct('name', 'zca:adapter') found_adapts = zcml.filter(tag='adapter', attr='factory', value=factory) if found_adapts: adapts = found_adapts[0] else: adapts = SimpleDirective(name='adapter', parent=zcml) adapts.attrs['for'] = _for if not name is UNSET: adapts.attrs['name'] = name adapts.attrs['factory'] = factory # write the provides which is collected in the zcarealize handler if len(targettok.realizes) == 1: provides = targettok.realizes[0] else: provides = targettok.provides if not provides: msg = 'adapter class %s has no interface realization' \ % dotted_path(adapter) raise ValueError(msg) adapts.attrs['provides'] = provides['path'] if hasattr(tok, 'permission'): adapts.attrs['permission'] = tok.permission
def createpermission(self, source, target): targetclass = read_target_node(source, target.target) module = targetclass.parent targetdir = module.parent path = class_base_name(targetclass) # prevent python class from being generated sts = [st.name for st in source.stereotypes] # only if no other steroetypes are attached if 'zca:permission' in sts and len(sts) == 1: # class also has to be deleted from __init__ init = targetdir['__init__.py'] for imp in init.imports(): if imp.fromimport == path and imp.names[0][0] == source.name: del init[imp.__name__] del module[targetclass.__name__] # and now write the permission definition into confure.zcml zcmlpath = targetdir.path zcmlpath.append('configure.zcml') fullpath = os.path.join(*zcmlpath) if 'configure.zcml' not in targetdir.keys(): zcml = ZCMLFile(fullpath) targetdir['configure.zcml'] = zcml else: zcml = targetdir['configure.zcml'] addZcmlRef(targetdir, zcml) id = TaggedValues(source).direct('id', 'zca:permission') if id is UNSET: permid = dotted_path(source) else: permid = id found_directives = zcml.filter(tag='permission', attr='id', value=permid) if found_directives: directive = found_directives[0] else: directive = SimpleDirective(name='permission', parent=zcml) directive.attrs['id'] = permid title = TaggedValues(source).direct('title', 'zca:permission') if not title is UNSET: directive.attrs['title'] = title description = TaggedValues(source).direct('description', 'zca:permission') if not description is UNSET: directive.attrs['description'] = description
def behavioradapter(self, source, target): schema = read_target_node(source, target.target) module = schema.parent adaptername = schema.classname[1:] if module.classes(adaptername): adapter = module.classes(adaptername)[0] else: adapter = python.Class() module[uuid.uuid4()] = adapter adapter.classname = adaptername implements = "implements(%s)" % schema.classname implements_exists = False for block in adapter.blocks(): for line in block.lines: if line == implements: implements_exists = True block = python.Block() block.__name__ = str(uuid.uuid4()) if not implements_exists: block.lines.append(implements) if block.lines: adapter.insertfirst(block) # ``__init__ only created once`` # XXX: check if signature changed and raise error if not adapter.functions('__init__'): init = python.Function(functionname='__init__') init.args.append('context') block = init[str(uuid.uuid4())] = python.Block() block.lines.append('self.context = context') adapter[str(uuid.uuid4())] = init imp = Imports(module) imp.set('zope.interface', [['implements', None]]) # read or create configure.zcml package = module.parent if 'configure.zcml' in package: configure = package['configure.zcml'] else: path = package.path path.append('configure.zcml') fullpath = os.path.join(*path) configure = ZCMLFile(fullpath) configure.nsmap['plone'] = 'http://namespaces.plone.org/plone' configure.nsmap['grok'] = 'http://namespaces.zope.org/grok' package['configure.zcml'] = configure provides = '.%s.%s' % (module.modulename, schema.classname) factory = '.%s.%s' % (module.modulename, adapter.classname) # XXX: maybe more filters if not configure.filter( tag='plone:behavior', attr='factory', value=factory): behavior = SimpleDirective(name='plone:behavior', parent=configure) behavior.attrs['title'] = adapter.classname # XXX: stereotype tgv behavior.attrs['description'] = adapter.classname behavior.attrs['provides'] = provides behavior.attrs['factory'] = factory
def zcaeventfor(self, source, target): adapter = source tok = token(str(adapter.uuid), True) if not hasattr(tok, 'fors'): msg = 'subscriber class %s has no <<for>> dependency' \ % dotted_path(adapter) raise ValueError(msg) pack = source.parent target = read_target_node(pack, target.target) if isinstance(target, python.Module): targetdir = target.parent else: targetdir = target path = targetdir.path path.append('subscribers.zcml') fullpath = os.path.join(*path) if 'subscribers.zcml' not in targetdir.keys(): zcml = ZCMLFile(fullpath) targetdir['subscribers.zcml'] = zcml else: zcml = targetdir['subscribers.zcml'] addZcmlRef(targetdir, zcml) targetclass = read_target_node(adapter, target) targettok = token(str(targetclass.uuid), True, realizes=[], provides=None) # make sure that the event is the second entry in the for list if tok.fors[0].stereotype('zca:event'): tok.fors.reverse() _for = [token(str(adaptee.uuid), False).fullpath for adaptee in tok.fors] factory = dotted_path(adapter) tgv = TaggedValues(adapter) name = tgv.direct('name', 'zca:for') isfunc = adapter.stereotype('pyegg:function') # functions are declared handler, classes factories if isfunc: attrname = 'handler' else: attrname = 'factory' found_adapts = zcml.filter(tag='subscriber', attr=attrname, value=factory) if found_adapts: adapts = found_adapts[0] else: adapts = SimpleDirective(name='subscriber', parent=zcml) adapts.attrs['for'] = _for if not name is UNSET: adapts.attrs['name'] = name adapts.attrs[attrname] = factory # write the provides which is collected in the zcarealize handler if len(targettok.realizes) == 1: provides = targettok.realizes[0] else: provides = targettok.provides if not isfunc: if provides: adapts.attrs['provides'] = provides['path'] if hasattr(tok, 'permission'): adapts.attrs['permission'] = tok.permission
def behavioradapter(self, source, target): schema = read_target_node(source, target.target) module = schema.parent adaptername = schema.classname[1:] if module.classes(adaptername): adapter = module.classes(adaptername)[0] else: adapter = python.Class() module[uuid.uuid4()] = adapter adapter.classname = adaptername implements = "implements(%s)" % schema.classname implements_exists = False for block in adapter.blocks(): for line in block.lines: if line == implements: implements_exists = True block = python.Block() block.__name__ = str(uuid.uuid4()) if not implements_exists: block.lines.append(implements) if block.lines: adapter.insertfirst(block) # ``__init__ only created once`` # XXX: check if signature changed and raise error if not adapter.functions('__init__'): init = python.Function(functionname='__init__') init.args.append('context') block = init[str(uuid.uuid4())] = python.Block() block.lines.append('self.context = context') adapter[str(uuid.uuid4())] = init imp = Imports(module) imp.set('zope.interface', [['implements', None]]) # read or create configure.zcml package = module.parent if 'configure.zcml' in package: configure = package['configure.zcml'] else: path = package.path path.append('configure.zcml') fullpath = os.path.join(*path) configure = ZCMLFile(fullpath) configure.nsmap['plone'] = 'http://namespaces.plone.org/plone' configure.nsmap['grok'] = 'http://namespaces.zope.org/grok' package['configure.zcml'] = configure provides = '.%s.%s' % (module.modulename, schema.classname) factory = '.%s.%s' % (module.modulename, adapter.classname) # XXX: maybe more filters if not configure.filter(tag='plone:behavior', attr='factory', value=factory): behavior = SimpleDirective(name='plone:behavior', parent=configure) behavior.attrs['title'] = adapter.classname # XXX: stereotype tgv behavior.attrs['description'] = adapter.classname behavior.attrs['provides'] = provides behavior.attrs['factory'] = factory
def plonebrowserview(self, source, target): view = source if view.stereotype('pyegg:function'): # XXX: <<function>> <<adapter>> on class return tok = token(str(view.uuid), True, browserpages=[]) pack = source.parent target = read_target_node(pack, target.target) targetclass = read_target_node(view, target) if isinstance(target, python.Module): targetdir = target.parent else: targetdir = target path = targetdir.path path.append('browser.zcml') fullpath = os.path.join(*path) if 'browser.zcml' not in targetdir: zcml = ZCMLFile(fullpath) zcml.nsmap['browser'] = 'http://namespaces.zope.org/browser' targetdir['browser.zcml'] = zcml else: zcml = targetdir['browser.zcml'] addZcmlRef(targetdir, zcml) targettok = token(str(targetclass.uuid), True, browserpages=[], provides=None) _for = [token(str(context.supplier.uuid), False).fullpath \ for context in tok.browserpages] or ['*'] classpath = class_full_name(targetclass) tgv = TaggedValues(view) # create the templates dir if 'templates' not in targetdir.keys(): targetdir['templates'] = Directory('templates') templates = targetdir['templates'] templates.factories['.pt'] = XMLTemplate #create the browser:page entries for bp in tok.browserpages or [None]: #name of view: if it should have a constant name, change the last param viewname = tgv.direct('name', 'plone:view', None) or \ tgv.direct('name', 'plone:dynamic_view', view.xminame.lower()) name_raw = tgv.direct('name', 'plone:view', None) or \ tgv.direct('name', 'plone:vdynamic_view', None) name = name_raw or view.xminame.lower() template_name_raw = tgv.direct('template_name', 'plone:view', None) or \ tgv.direct('template_name', 'plone:dynamic_view', None) template_name = template_name_raw or name + '.pt' permission = tgv.direct('permission', 'plone:view', None) or \ tgv.direct('permission', 'plone:dynamic_view', None) layer = tgv.direct('layer', 'plone:view', None) or \ tgv.direct('layer', 'plone:dynamic_view', None) if bp: bptgv = TaggedValues(bp) bptok = token(str(bp.supplier.uuid), False) _for = bptok.fullpath print 'xminame:', bp, bp.xminame # consider uuid as an unset name if bp.xminame is None or re.match( '[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}', bp.xminame): bpname = None else: bpname = bp.xminame.lower() if bp.xminame: viewname = bp.xminame viewname = bptgv.direct('name', 'plone:view', None) or \ bptgv.direct('name', 'plone:dynamic_view', viewname) name = bptgv.direct('name', 'plone:view', None) or \ bptgv.direct('name', 'plone:dynamic_view', bpname or name) # override template name template_name = bptgv.direct( 'template_name', 'plone:view', None) or \ bptgv.direct( 'template_name', 'plone:dynamic_view', None) or \ template_name_raw or name + '.pt' permission = bptgv.direct('permission', 'plone:view', None) or \ bptgv.direct('permission', 'plone:dynamic_view', permission) layer = bptgv.direct('layer', 'plone:view', None) or \ bptgv.direct('layer', 'plone:dynamic_view', layer) else: _for = '*' found_browserpages = zcml.filter(tag='browser:page', attr='name', value=viewname) browser = None templatepath = 'templates/' + template_name if found_browserpages: for br in found_browserpages: #check if it really matches if br.attrs.get('class') == classpath and \ _for == br.attrs['for']: browser = br break if not browser: browser = SimpleDirective(name='browser:page', parent=zcml) browser.attrs['for'] = _for if viewname and not viewname is UNSET: browser.attrs['name'] = viewname browser.attrs['class'] = classpath browser.attrs['template'] = templatepath browser.attrs['permission'] = permission or 'zope2.View' if layer: browser.attrs['layer'] = layer # spit out the page vanilla template if template_name not in templates.keys(): pt = XMLTemplate() templates[template_name] = pt # set template for viewtemplate pt.template = 'agx.generator.plone:templates/viewtemplate.pt'
def gsprofilezcml(self, source, target): """Create configure.zcml if not exists yet, profiles.zcml and profile specific directives. """ package = target.anchor # read or create configure.zcml if 'configure.zcml' in package: configure = package['configure.zcml'] else: configure = package['configure.zcml'] = ZCMLFile() # if include profile.zcml missing, add it if not configure.filter(tag='include', attr='file', value='profiles.zcml'): include = SimpleDirective(name='include', parent=configure) include.attrs['file'] = 'profiles.zcml' # read or create profiles.zcml if 'profiles.zcml' in package: profiles = package['profiles.zcml'] else: snmap = { None: 'http://namespaces.zope.org/zope', 'genericsetup': 'http://namespaces.zope.org/genericsetup', } profiles = package['profiles.zcml'] = ZCMLFile(nsmap=snmap) # if include Products.GenericSetup missing, add it if not profiles.filter( tag='include', attr='package', value='Products.GenericSetup'): include = SimpleDirective(name='include', parent=profiles) include.attrs['package'] = 'Products.GenericSetup' include.attrs['file'] = 'meta.zcml' # read or create install profile directive if not profiles.filter( tag='genericsetup:registerProfile', attr='name', value='default'): install = SimpleDirective(name='genericsetup:registerProfile', parent=profiles) install.attrs['name'] = 'default' else: install = profiles.filter(tag='genericsetup:registerProfile', attr='name', value='default')[0] egg_name = egg_source(source).name # set default profile directive attributes install.attrs['title'] = '%s install' % egg_name install.attrs['description'] = 'Install %s in Plone' % egg_name install.attrs['directory'] = 'profiles/default' install.attrs['provides'] = 'Products.GenericSetup.interfaces.EXTENSION' # read or create uninstall profile directive if not profiles.filter(tag='genericsetup:registerProfile', attr='name', value='uninstall'): uninstall = SimpleDirective(name='genericsetup:registerProfile', parent=profiles) uninstall.attrs['name'] = 'uninstall' else: uninstall = profiles.filter(tag='genericsetup:registerProfile', attr='name', value='uninstall')[0] # set uninstall profile directive attributes uninstall.attrs['title'] = '%s uninstall' % egg_name uninstall.attrs['description'] = 'Uninstall %s in Plone' % egg_name uninstall.attrs['directory'] = 'profiles/uninstall' uninstall.attrs['provides'] = 'Products.GenericSetup.interfaces.EXTENSION'
def plonebrowserview(self, source, target): view = source if view.stereotype('pyegg:function'): # XXX: <<function>> <<adapter>> on class return tok = token(str(view.uuid), True, browserpages=[]) pack = source.parent target = read_target_node(pack, target.target) targetclass = read_target_node(view, target) if isinstance(target, python.Module): targetdir = target.parent else: targetdir = target path = targetdir.path path.append('browser.zcml') fullpath = os.path.join(*path) if 'browser.zcml' not in targetdir: zcml = ZCMLFile(fullpath) zcml.nsmap['browser'] = 'http://namespaces.zope.org/browser' targetdir['browser.zcml'] = zcml else: zcml = targetdir['browser.zcml'] addZcmlRef(targetdir, zcml) targettok = token( str(targetclass.uuid), True, browserpages=[], provides=None) _for = [token(str(context.supplier.uuid), False).fullpath \ for context in tok.browserpages] or ['*'] classpath = class_full_name(targetclass) tgv = TaggedValues(view) # create the templates dir if 'templates' not in targetdir.keys(): targetdir['templates'] = Directory('templates') templates = targetdir['templates'] templates.factories['.pt'] = XMLTemplate #create the browser:page entries for bp in tok.browserpages or [None]: #name of view: if it should have a constant name, change the last param viewname = tgv.direct('name', 'plone:view', None) or \ tgv.direct('name', 'plone:dynamic_view', view.xminame.lower()) name_raw = tgv.direct('name', 'plone:view', None) or \ tgv.direct('name', 'plone:vdynamic_view', None) name = name_raw or view.xminame.lower() template_name_raw = tgv.direct('template_name', 'plone:view', None) or \ tgv.direct('template_name', 'plone:dynamic_view', None) template_name = template_name_raw or name + '.pt' permission = tgv.direct('permission', 'plone:view', None) or \ tgv.direct('permission', 'plone:dynamic_view', None) layer = tgv.direct('layer', 'plone:view', None) or \ tgv.direct('layer', 'plone:dynamic_view', None) if bp: bptgv = TaggedValues(bp) bptok = token(str(bp.supplier.uuid), False) _for = bptok.fullpath print 'xminame:',bp,bp.xminame # consider uuid as an unset name if bp.xminame is None or re.match('[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}', bp.xminame): bpname = None else: bpname = bp.xminame.lower() if bp.xminame: viewname = bp.xminame viewname = bptgv.direct('name', 'plone:view', None) or \ bptgv.direct('name', 'plone:dynamic_view', viewname) name = bptgv.direct('name', 'plone:view', None) or \ bptgv.direct('name', 'plone:dynamic_view', bpname or name) # override template name template_name = bptgv.direct( 'template_name', 'plone:view', None) or \ bptgv.direct( 'template_name', 'plone:dynamic_view', None) or \ template_name_raw or name + '.pt' permission = bptgv.direct('permission', 'plone:view', None) or \ bptgv.direct('permission', 'plone:dynamic_view', permission) layer = bptgv.direct('layer', 'plone:view', None) or \ bptgv.direct('layer', 'plone:dynamic_view', layer) else: _for = '*' found_browserpages = zcml.filter( tag='browser:page', attr='name', value=viewname) browser = None templatepath = 'templates/' + template_name if found_browserpages: for br in found_browserpages: #check if it really matches if br.attrs.get('class') == classpath and \ _for == br.attrs['for']: browser = br break if not browser: browser = SimpleDirective(name='browser:page', parent=zcml) browser.attrs['for'] = _for if viewname and not viewname is UNSET: browser.attrs['name'] = viewname browser.attrs['class'] = classpath browser.attrs['template'] = templatepath browser.attrs['permission'] = permission or 'zope2.View' if layer: browser.attrs['layer'] = layer # spit out the page vanilla template if template_name not in templates.keys(): pt = XMLTemplate() templates[template_name] = pt # set template for viewtemplate pt.template = 'agx.generator.plone:templates/viewtemplate.pt'