예제 #1
0
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)
예제 #2
0
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'
예제 #4
0
def zcml_include_package(directory):
    if not 'configure.zcml' in directory.parent:
        directory.parent['configure.zcml'] = ZCMLFile()
    zcml = directory.parent['configure.zcml']
    package = '.%s' % directory.name
    if not zcml.filter(tag='include', attr='package', value=package):
        include = SimpleDirective(name='include', parent=zcml)
        include.attrs['package'] = package
def make_generators(self, source, target):
    if source.stereotype('pyegg:stub'):
        return
    egg = egg_source(source)
    eggtarget = read_target_node(egg, target.target)
    zcml = get_zcml(eggtarget, 'configure.zcml',
                    nsmap={None: 'http://namespaces.zope.org/zope', 
                           'agx': 'http://namespaces.zope.org/agx'})
    tgv = TaggedValues(source)

    # if transform isnt specified as tgv, get it from dependency relations to
    # other generators
    transform = tgv.direct('transform', 'generator:generator', None)
    if not transform:
        transforms = token(str(source.uuid), True, transforms=[]).transforms
        if len(transforms) > 1:
            msg = 'Currently only one transform per generator allowed (%s)'
            msg = msg % source.name
            raise ValueError(msg)
        elif len(transforms) == 1:
            transform = transforms[0].name

    if not transform:
        transform = 'uml2fs'

    # if depends isnt specified as tgv, get it from dependency relations to
    # transforms
    depend = tgv.direct('depends', 'generator:generator', None)
    if not depend:
        depends = token(str(source.uuid), True, depends_on=[]).depends_on
        if len(depends) > 1:
            msg = 'Currently only one depends per generator allowed (%s)'
            msg = msg % source.name
            raise ValueError(msg)
        elif len(depends) == 1:
            depend = depends[0].name

    if not depend:
        depend = 'NO'

    directives = zcml.filter(tag='agx:generator', attr='name')

    directive = None
    for d in directives:
        if d.attrs['name'] == source.name:
            directive = d
            break

    if not directive:
        directive = SimpleDirective(name='agx:generator', parent=zcml)

    directive.attrs['name'] = source.name
    directive.attrs['transform'] = transform
    directive.attrs['depends'] = depend

    set_zcml_directive(eggtarget, 'configure.zcml', 'agx:generator',
                       'name', source.name, overwrite=True)
예제 #6
0
def set_zcml_directive(directory, zcmlname, tag, attr, value,
                       overwrite=False, **kw):
    zcml = get_zcml(directory, zcmlname)
    directives = zcml.filter(tag=tag, attr=attr, value=value)
    if directives:
        directive = directives[0]
    else:
        directive = SimpleDirective(name=tag, parent=zcml)
    directive.attrs[attr]=value
    for k in kw:
        if k not in directive.attrs or overwrite:
            directive.attrs[k] = kw[k]
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 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 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 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'
예제 #11
0
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
예제 #13
0
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'
예제 #14
0
def make_generators(self, source, target):
    if source.stereotype('pyegg:stub'):
        return
    egg = egg_source(source)
    eggtarget = read_target_node(egg, target.target)
    zcml = get_zcml(eggtarget,
                    'configure.zcml',
                    nsmap={
                        None: 'http://namespaces.zope.org/zope',
                        'agx': 'http://namespaces.zope.org/agx'
                    })
    tgv = TaggedValues(source)

    # if transform isnt specified as tgv, get it from dependency relations to
    # other generators
    transform = tgv.direct('transform', 'generator:generator', None)
    if not transform:
        transforms = token(str(source.uuid), True, transforms=[]).transforms
        if len(transforms) > 1:
            msg = 'Currently only one transform per generator allowed (%s)'
            msg = msg % source.name
            raise ValueError(msg)
        elif len(transforms) == 1:
            transform = transforms[0].name

    if not transform:
        transform = 'uml2fs'

    # if depends isnt specified as tgv, get it from dependency relations to
    # transforms
    depend = tgv.direct('depends', 'generator:generator', None)
    if not depend:
        depends = token(str(source.uuid), True, depends_on=[]).depends_on
        if len(depends) > 1:
            msg = 'Currently only one depends per generator allowed (%s)'
            msg = msg % source.name
            raise ValueError(msg)
        elif len(depends) == 1:
            depend = depends[0].name

    if not depend:
        depend = 'NO'

    directives = zcml.filter(tag='agx:generator', attr='name')

    directive = None
    for d in directives:
        if d.attrs['name'] == source.name:
            directive = d
            break

    if not directive:
        directive = SimpleDirective(name='agx:generator', parent=zcml)

    directive.attrs['name'] = source.name
    directive.attrs['transform'] = transform
    directive.attrs['depends'] = depend

    set_zcml_directive(eggtarget,
                       'configure.zcml',
                       'agx:generator',
                       'name',
                       source.name,
                       overwrite=True)
예제 #15
0
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'