def __call__(self, econtext):
        name = StringExpr.__call__(self, econtext)
        context = econtext.vars['context']
        request = econtext.vars['request']
        view = econtext.vars['view']

        # Try to look up the provider.
        provider = zope.component.queryMultiAdapter(
            (context, request, view), interfaces.IContentProvider, name)

        # Provide a useful error message, if the provider was not found.
        if provider is None:
            raise interfaces.ContentProviderLookupError(name)

        if getattr(provider, '__of__', None) is not None:
            provider = provider.__of__(context)

        # Insert the data gotten from the context
        addTALNamespaceData(provider, econtext)

        # Stage 1: Do the state update.
        provider.update()

        # Stage 2: Render the HTML content.
        return provider.render()
예제 #2
0
    def __call__(self, econtext):
        name = super(ProviderExpression, self).__call__(econtext)
        view = econtext.vars['view']
        context = view.context
        request = view.request

        # Try to look up the provider.
        provider = zope.component.queryMultiAdapter(
            (context, request, view), interfaces.IContentProvider, name)

        # Provide a useful error message, if the provider was not found.
        if provider is None:
            raise interfaces.ContentProviderLookupError(name)

        # add the __name__ attribute if it implements ILocation
        if ILocation.providedBy(provider):
            provider.__name__ = name

        # Insert the data gotten from the context
        addTALNamespaceData(provider, econtext)

        # Stage 1: Do the state update.
        zope.event.notify(interfaces.BeforeUpdateEvent(provider, request))
        provider.update()

        # Stage 2: Render the HTML content.
        return provider.render()
예제 #3
0
    def render_content_provider(econtext, name):
        name = name.strip()

        context = econtext.get('context')
        request = econtext.get('request')
        view = econtext.get('view')

        cp = queryMultiAdapter((context, request, view),
                               IContentProvider,
                               name=name)

        # provide a useful error message, if the provider was not found.
        if cp is None:
            raise ContentProviderLookupError(name)

        # add the __name__ attribute if it implements ILocation
        if ILocation.providedBy(cp):
            cp.__name__ = name

        # Insert the data gotten from the context
        addTALNamespaceData(cp, econtext)

        # BBB: This is where we're different:
        if getattr(cp, '__of__', None) is not None:
            cp = cp.__of__(context)

        # Stage 1: Do the state update.
        if BeforeUpdateEvent is not None:
            notify(BeforeUpdateEvent(cp, request))
        cp.update()

        # Stage 2: Render the HTML content.
        return cp.render()
예제 #4
0
파일: expressions.py 프로젝트: jean/z3c.pt
def render_content_provider(econtext, name):
    name = name.strip()

    context = econtext.get('context')
    request = econtext.get('request')
    view = econtext.get('view')

    cp = zope.component.queryMultiAdapter(
        (context, request, view), IContentProvider, name=name)

    # provide a useful error message, if the provider was not found.
    if cp is None:
        raise ContentProviderLookupError(name)

    # add the __name__ attribute if it implements ILocation
    if ILocation.providedBy(cp):
        cp.__name__ = name

    # Insert the data gotten from the context
    addTALNamespaceData(cp, econtext)

    # Stage 1: Do the state update.
    if BeforeUpdateEvent is not None:
        zope.event.notify(BeforeUpdateEvent(cp, request))
    cp.update()

    # Stage 2: Render the HTML content.
    return cp.render()
예제 #5
0
    def __call__(self, econtext):
        name = super(Z2ProviderExpression, self).__call__(econtext)
        context = econtext.vars['context']
        request = econtext.vars['request']
        view = econtext.vars['view']

        # Try to look up the provider.
        provider = zope.component.queryMultiAdapter(
            (context, request, view), cp_interfaces.IContentProvider, name)

        # Provide a useful error message, if the provider was not found.
        if provider is None:
            raise cp_interfaces.ContentProviderLookupError(name)

        # XXX We can either wrap this in the context and have three test
        # failures in directives.txt or wrap it in the view (aka our
        # __parent__) and have one test failure in provider.txt which also
        # happens when we don't wrap this at all anymore :(
        # Removing all the AQ-wrapping is probably the way to go here.
        if getattr(provider, '__of__', None) is not None:
            provider = provider.__of__(context)

        # Insert the data gotten from the context
        addTALNamespaceData(provider, econtext)

        # Stage 1: Do the state update.
        provider.update()

        # Stage 2: Render the HTML content.
        return provider.render()
예제 #6
0
    def __call__(self, econtext):
        name = StringExpr.__call__(self, econtext)
        context = econtext.vars['context']
        request = econtext.vars['request']
        view = econtext.vars['view']

        # Try to look up the provider.
        provider = zope.component.queryMultiAdapter(
            (context, request, view), interfaces.IContentProvider, name)

        # Provide a useful error message, if the provider was not found.
        if provider is None:
            raise interfaces.ContentProviderLookupError(name)

        if getattr(provider, '__of__', None) is not None:
            provider = provider.__of__(context)

        # Insert the data gotten from the context
        addTALNamespaceData(provider, econtext)

        # Stage 1: Do the state update.
        provider.update()

        # Stage 2: Render the HTML content.
        return provider.render()
예제 #7
0
def render_content_provider(econtext, name):
    name = name.strip()

    context = econtext.get("context")
    request = econtext.get("request")
    view = econtext.get("view")

    cp = zope.component.queryMultiAdapter((context, request, view),
                                          IContentProvider,
                                          name=name)

    # provide a useful error message, if the provider was not found.
    # Be sure to provide the objects in addition to the name so
    # debugging ZCML registrations is possible
    if cp is None:
        raise ContentProviderLookupError(name, (context, request, view))

    # add the __name__ attribute if it implements ILocation
    if ILocation.providedBy(cp):
        cp.__name__ = name

    # Insert the data gotten from the context
    addTALNamespaceData(cp, econtext)

    # Stage 1: Do the state update.
    zope.event.notify(BeforeUpdateEvent(cp, request))
    cp.update()

    # Stage 2: Render the HTML content.
    return cp.render()
예제 #8
0
    def __call__(self, econtext):
        name = super(Z2ProviderExpression, self).__call__(econtext)
        context = econtext.vars['context']
        request = econtext.vars['request']
        view = econtext.vars['view']

        # Try to look up the provider.
        provider = zope.component.queryMultiAdapter(
            (context, request, view), cp_interfaces.IContentProvider, name)

        # Provide a useful error message, if the provider was not found.
        if provider is None:
            raise cp_interfaces.ContentProviderLookupError(name)

        # XXX We can either wrap this in the context and have three test
        # failures in directives.txt or wrap it in the view (aka our
        # __parent__) and have one test failure in provider.txt which also
        # happens when we don't wrap this at all anymore :(
        # Removing all the AQ-wrapping is probably the way to go here.
        if getattr(provider, '__of__', None) is not None:
            provider = provider.__of__(context)

        # Insert the data gotten from the context
        addTALNamespaceData(provider, econtext)

        # Stage 1: Do the state update.
        provider.update()

        # Stage 2: Render the HTML content.
        return provider.render()
예제 #9
0
def render_content_provider(econtext, name):
    name = name.strip()

    context = econtext.get("context")
    request = econtext.get("request")
    view = econtext.get("view")

    cp = zope.component.queryMultiAdapter(
        (context, request, view), IContentProvider, name=name
    )

    # provide a useful error message, if the provider was not found.
    # Be sure to provide the objects in addition to the name so
    # debugging ZCML registrations is possible
    if cp is None:
        raise ContentProviderLookupError(name, (context, request, view))

    # add the __name__ attribute if it implements ILocation
    if ILocation.providedBy(cp):
        cp.__name__ = name

    # Insert the data gotten from the context
    addTALNamespaceData(cp, econtext)

    # Stage 1: Do the state update.
    zope.event.notify(BeforeUpdateEvent(cp, request))
    cp.update()

    # Stage 2: Render the HTML content.
    return cp.render()
예제 #10
0
 def renderBlockProvider(self, context):
     name = u'zug.simplelayout.block'
     context = context
     request = self.request
     view = self
     provider = zope.component.queryMultiAdapter(
         (context, request, view), cp_interfaces.IContentProvider, name)
     addTALNamespaceData(provider, context)
     provider.update()
     return provider.render()
    def blockRenderer(self,name="simplelayout.portlet.listing"):

        manager = queryMultiAdapter((self.context, self.request, self),
                                    IViewletManager, name)
        viewlet_adapters = getAdapters(
            (manager.context, manager.request, manager.__parent__, manager),
            IViewlet)

        if manager is None:
            return ''
        addTALNamespaceData(manager, self.context)
        manager.update()
        return manager.render()
예제 #12
0
    def renderBlockProvider(self, context):
        #logger.info('sl viewlet renderer not cached')
        view = self
        block = context
        request = self.request

        blockconf = IBlockConfig(context)
        name = blockconf.viewlet_manager

        #Paragraph is our example, this content type has no ViewletManager
        #so it should use simplelayout.base.block
        default = 'block'
        prefix = 'simplelayout.base'
        #first time we have to generate the viewletManager name.
        #so we have the possibility to change the manager later.
        if name is None:

            #now build the viewletManager name from given prefix and
            #block.__class__.__name__ (ClassName)
            name = '%s.%s' % (prefix, block.__class__.__name__)
            blockconf.viewlet_manager = name

        provider = None
        counter = 0
        while provider is None and counter < 10:
            provider = zope.component.queryMultiAdapter(
                (block, request, view), cp_interfaces.IContentProvider, name)
            counter += 1
            if provider is None:
                name = '%s.%s' % (prefix, default)

        #if provider is still None
        if provider is None:
            return 'Something is wrong - pls debug'

        addTALNamespaceData(provider, block)
        provider.update()
        return provider.render()
예제 #13
0
파일: tales.py 프로젝트: Py-AMS/pyams-utils
def render_extension(econtext, name):
    """TALES extension renderer

    See :ref:`tales` for complete description.

    The requested extension can be called with our without arguments, like in
    ${structure:tales:my_expression} or ${structure:tales:my_expression(arg1, arg2)}.
    In the second form, arguments will be passed to the "render" method; arguments can be
    static (like strings or integers), or can be variables defined into current template
    context; other Python expressions including computations or functions calls are actually
    not supported, but dotted syntax is supported to access inner attributes of variables.
    """

    def get_value(econtext, arg):
        """Extract argument value from context

        Extension expression language is quite simple. Values can be given as
        positioned strings, integers or named arguments of the same types.
        """
        arg = arg.strip()
        if arg.startswith('"') or arg.startswith("'"):
            # may be a quoted string...
            return arg[1:-1]
        if '=' in arg:
            key, value = arg.split('=', 1)
            value = get_value(econtext, value)
            return {key.strip(): value}
        try:
            arg = int(arg)  # check integer value
        except ValueError:
            args = arg.split('.')
            result = econtext.get(args.pop(0))
            for arg in args:  # pylint: disable=redefined-argument-from-local
                result = getattr(result, arg)
            return result
        else:
            return arg

    name = name.strip()
    context = econtext.get('context')
    request = econtext.get('request')
    view = econtext.get('view')

    args, kwargs = [], {}
    func_match = FUNCTION_EXPRESSION.match(name)
    if func_match:
        name, arguments = func_match.groups()
        for arg in map(lambda x: get_value(econtext, x), ARGUMENTS_EXPRESSION.findall(arguments)):
            if isinstance(arg, dict):
                kwargs.update(arg)
            else:
                args.append(arg)

    extension = queryMultiAdapter((context, request, view), ITALESExtension, name=name)
    if extension is None:
        extension = queryMultiAdapter((context, request), ITALESExtension, name=name)
    if extension is None:
        extension = queryAdapter(context, ITALESExtension, name=name)

    # return an empty string if the extension was not found.
    if extension is None:
        return ''

    # Insert the data gotten from the context
    addTALNamespaceData(extension, econtext)

    return extension.render(*args, **kwargs)
예제 #14
0
def render_content_provider(econtext, name):  # pylint: disable=too-many-locals,too-many-statements
    """TALES provider: content provider

    This TALES expression is used to render a registered "content provider", which
    is an adapter providing IContentProvider interface; adapter lookup is based on
    current context, request and view.

    The requested provider can be called with our without arguments, like in
    ${structure:provider:my_provider} or ${structure:provider:my_provider(arg1, arg2)}.
    In the second form, arguments will be passed to the "update" method; arguments can be
    static (like strings or integers), or can be variables defined into current template
    context; other Python expressions including computations or functions calls are actually
    not supported, but dotted syntax is supported to access inner attributes of variables.

    Provider arguments can be passed by position but can also be passed by name, using classic
    syntax as in ${structure:provider:my_provider(arg1, arg3=var3)}
    """

    def get_provider(name):
        # we first look into request annotations to check if a provider implementation has
        # already been provided during traversal; if not, a simple adapter lookup is done
        provider = get_request_data(request, 'provider:{}:factory'.format(name))

        # if using request annotations, provider can be given as a "direct" factory or as a
        # dict; if a dict is provided, it's keys are interfaces or classes that the current
        # context class have to provide or inherit from, and it's matching values are the
        # provider factories.
        # if provider is given as a dict, it should be ordered using an OrderedDict so thet
        # more specific interfaces are provided first!
        if isinstance(provider, dict):
            for intf, factory in provider.items():
                if (is_interface(intf) and intf.providedBy(context)) or \
                        (inspect.isclass(intf) and isinstance(context, intf)):
                    provider = factory
                    break
            else:
                provider = None

        # if provider is a callable, we call it!
        if callable(provider):
            provider = provider(context, request, view)

        if provider is None:
            provider = registry.queryMultiAdapter((context, request, view), IContentProvider,
                                                  name=name)
        if provider is not None:
            econtext['provider'] = provider
        return provider

    def get_value(arg):
        """Extract argument value from context

        Extension expression language is quite simple. Values can be given as
        positioned strings, integers or named arguments of the same types.
        """
        arg = arg.strip()
        if arg.startswith('"') or arg.startswith("'"):
            # may be a quoted string...
            return arg[1:-1]
        if '=' in arg:
            key, value = arg.split('=', 1)
            value = get_value(value)
            return {key.strip(): value}
        try:
            arg = int(arg)  # check integer value
        except ValueError:
            args = arg.split('.')
            result = econtext.get(args.pop(0))
            for arg in args:  # pylint: disable=redefined-argument-from-local
                result = getattr(result, arg)
            return result
        else:
            return arg

    def get_context_arg(arg):
        """Extract a value if present in kwargs, otherwise look into context"""
        if arg in kwargs:
            return kwargs.pop(arg)
        return econtext.get(arg)

    name = name.strip()

    args, kwargs = [], {}
    func_match = FUNCTION_EXPRESSION.match(name)
    if func_match:
        name, arguments = func_match.groups()
        for arg in map(get_value, ARGUMENTS_EXPRESSION.findall(arguments)):
            if isinstance(arg, dict):
                kwargs.update(arg)
            else:
                args.append(arg)
    else:
        match = CONTENT_PROVIDER_NAME.match(name)
        if match:
            name = match.groups()[0]
        else:
            raise ContentProviderLookupError(name)

    context = get_context_arg('context')
    request = get_context_arg('request')
    view = get_context_arg('view')

    registry = request.registry
    provider = get_provider(name)

    # raise an exception if the provider was not found.
    if provider is None:
        raise ContentProviderLookupError(name)

    # add the __name__ attribute if it implements ILocation
    if ILocation.providedBy(provider):
        provider.__name__ = name

    # Insert the data gotten from the context
    addTALNamespaceData(provider, econtext)

    # Stage 1: Do the state update
    registry.notify(BeforeUpdateEvent(provider, request))
    provider.update(*args, **kwargs)

    # Stage 2: Render the HTML content
    return provider.render()
예제 #15
0
 def traverse(self, name, furtherPath):
     provider = ContentProviders.traverse(self, name, furtherPath)
     if self.engine is not None:
         unproxied = removeSecurityProxy(provider)
         addTALNamespaceData(unproxied, self.engine)
     return provider
예제 #16
0
 def traverse(self, name, furtherPath):
     provider = ContentProviders.traverse(self, name, furtherPath)
     if self.engine is not None:
         unproxied = removeSecurityProxy(provider)
         addTALNamespaceData(unproxied, self.engine)
     return provider