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