示例#1
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 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
示例#3
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'
示例#4
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)
示例#5
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'