Beispiel #1
0
    def image_fields(self):
        """ read interface
        """
        fields = []

        for field in self.context.Schema().fields():
            if IBlobImageField in providedBy(field).interfaces() or \
               IImageField in providedBy(field).interfaces() and \
               field.get_size(self.context) > 0:
                fields.append(field)

        return fields
    def image_fields(self):
        """ read interface
        """
        fields = []

        for field in self.context.Schema().fields():
            if IBlobImageField in providedBy(field).interfaces() or \
               IImageField in providedBy(field).interfaces() and \
               field.get_size(self.context) > 0:
                fields.append(field)

        return fields
Beispiel #3
0
    def __get__(self, inst, cls=None):
        # We're looking at a class - fall back on default
        if inst is None:
            return getObjectSpecification(cls)

        # Find the cached value.
        cache = getattr(inst, "_v__providedBy__", None)

        # Find the data we need to know if our cache needs to be invalidated
        provided = link_provides = getattr(inst._context, "__provides__", None)

        # See if we have a valid cache, and if so return it
        if cache is not None:
            cached_mtime, cached_provides, cached_provided = cache

            if inst._p_mtime == cached_mtime and link_provides is cached_provides:
                return cached_provided

        # If the instance doesn't have a __provides__ attribute, get the
        # interfaces implied by the class as a starting point.
        if provided is None:
            provided = implementedBy(cls)

        # Add the interfaces provided by the target, but ensure that some problematic
        # interfaces are removed
        provided += providedBy(
            inst._context) - IIterateAware - IVersioningSupport
        provided += ISymlinkMarker

        inst._v__providedBy__ = inst._p_mtime, link_provides, provided
        return provided
Beispiel #4
0
    def providedBy(self, ob):
        """Is the interface implemented by an object

          >>> from zope.interface import *
          >>> class I1(Interface):
          ...     pass
          >>> class C(object):
          ...     implements(I1)
          >>> c = C()
          >>> class X(object):
          ...     pass
          >>> x = X()
          >>> I1.providedBy(x)
          False
          >>> I1.providedBy(C)
          False
          >>> I1.providedBy(c)
          True
          >>> directlyProvides(x, I1)
          >>> I1.providedBy(x)
          True
          >>> directlyProvides(C, I1)
          >>> I1.providedBy(C)
          True

        """
        spec = providedBy(ob)
        return self in spec._implied
Beispiel #5
0
def _getUtilityProvided(component):
    provided = list(providedBy(component))
    if len(provided) == 1:
        return provided[0]
    raise TypeError(
        "The utility doesn't provide a single interface "
        "and no provided interface was specified.")
Beispiel #6
0
def _getUtilityProvided(component):
    provided = list(providedBy(component))
    if len(provided) == 1:
        return provided[0]
    raise TypeError(
        "The utility doesn't provide a single interface "
        "and no provided interface was specified.")
Beispiel #7
0
def getAdapter(obj, interfaceClass, default=_Nothing,
               adapterClassLocator=None, persist=None):
    """DEPRECATED. Return an object that implements the given interface.

    The result will be a wrapper around the object passed as a parameter, or
    the parameter itself if it already implements the interface. If no
    adapter can be found, the 'default' parameter will be returned.

    The recommended way of replacing uses of this function is to use
    IFoo(o), since getAdapter is tied to a specific Twisted registry
    and thus won't interoperate well.
    """
    warnings.warn("components.getAdapter() is deprecated.", ComponentsDeprecationWarning, stacklevel=2)
    if hasattr(obj, '__class__'):
        fixClassImplements(obj.__class__)
    self = globalRegistry
    if interfaceClass.providedBy(obj):
        return obj

    if persist != False:
        pkey = (id(obj), interfaceClass)
        if _adapterPersistence.has_key(pkey):
            return _adapterPersistence[pkey]

    factory = self.lookup1(declarations.providedBy(obj), interfaceClass)
    if factory != None:
        return factory(obj)

    if default == _Nothing:
        raise NotImplementedError
    else:
        return default
    def providedBy(self, ob):
        """Is the interface implemented by an object

          >>> from zope.interface import *
          >>> class I1(Interface):
          ...     pass
          >>> class C(object):
          ...     implements(I1)
          >>> c = C()
          >>> class X(object):
          ...     pass
          >>> x = X()
          >>> I1.providedBy(x)
          False
          >>> I1.providedBy(C)
          False
          >>> I1.providedBy(c)
          True
          >>> directlyProvides(x, I1)
          >>> I1.providedBy(x)
          True
          >>> directlyProvides(C, I1)
          >>> I1.providedBy(C)
          True
        
        """
        spec = providedBy(ob)
        return self in spec._implied
Beispiel #9
0
    def test_impl(self):
        myTask = MyTask()
#         print CabbageTask.implementedBy(MyTask)
#         print directlyProvides(myTask,Task)
        print type(MyTask)
        print list(providedBy(myTask))[0]
        print list(implementedBy(MyTask))
        myTask.run()
        pass
Beispiel #10
0
        def image_fields(self):
            """ read interface
            """
            fields = []

            for field in self.context.getTypeInfo().lookupSchema():
                img_field = getattr(self.context, field, None)
                if img_field and IImage in providedBy(img_field).interfaces():
                    fields.append(img_field)

            return fields
Beispiel #11
0
    def adapter_hook(self, interface, object, name='', default=None):
        """Hook function used when calling interfaces.

        When called from Interface.__adapt__, only the interface and
        object parameters will be passed.
        
        """
        factory = self.lookup1(providedBy(object), interface, name)
        if factory is not None:
            return factory(object)

        return default
Beispiel #12
0
def getInterfaces(klass):
    """DEPRECATED. Return list of all interfaces the class implements. Or the object provides.
    This is horrible and stupid. Please use zope.interface.providedBy() or implementedBy().
    """
    warnings.warn("getInterfaces should not be used, use providedBy() or implementedBy()", ComponentsDeprecationWarning, stacklevel=2)
    if isinstance(klass, (type, types.ClassType)):
        fixClassImplements(klass)
        l = list(declarations.implementedBy(klass))
    else:
        fixClassImplements(klass.__class__)
        l = list(declarations.providedBy(klass))
    r = []
    for i in l:
        r.extend(superInterfaces(i))
    return util.uniquify(r)
Beispiel #13
0
    def adapter_hook(self, interface, object, name='', default=None):
        """Hook function used when calling interfaces.

        When called from Interface.__adapt__, only the interface and
        object parameters will be passed.

        If the factory produces `None`, then the default is returned. This
        allows us to prevent adaptation (if desired) and make the factory
        decide whether an adapter will be available.
        """
        factory = self.lookup1(providedBy(object), interface, name)
        if factory is not None:
            adapter = factory(object)
            if adapter is not None:
                return adapter
        return default
Beispiel #14
0
    def is_allowed_to_view(request, view_name):
        """
        Check if the current user have the right to the view
        """
        try:
            reg = request.registry
        except AttributeError:
            reg = get_current_registry()

        request_iface = reg.queryUtility(IRouteRequest, name=view_name)
        provides = [IViewClassifier,
                    request_iface,
                    providedBy(request.context)]
        view = reg.adapters.lookup(provides, ISecuredView, name='')

        assert view is not None
        return view.__permitted__(request.context, request)
Beispiel #15
0
def getInterfaces(klass):
    """DEPRECATED. Return list of all interfaces the class implements. Or the object provides.

    This is horrible and stupid. Please use zope.interface.providedBy() or implementedBy().
    """
    warnings.warn("getInterfaces should not be used, use providedBy() or implementedBy()", ComponentsDeprecationWarning, stacklevel=2)
    # try to support both classes and instances, giving different behaviour
    # which is HORRIBLE :(
    if isinstance(klass, (type, types.ClassType)):
        fixClassImplements(klass)
        l = list(declarations.implementedBy(klass))
    else:
        fixClassImplements(klass.__class__)
        l = list(declarations.providedBy(klass))
    r = []
    for i in l:
        r.extend(superInterfaces(i))
    return util.uniquify(r)
Beispiel #16
0
    def addComponent(self, component, ignoreClass=0):
        """
        Add a component to me, for all appropriate interfaces.

        In order to determine which interfaces are appropriate, the component's
        provided interfaces will be scanned.

        If the argument 'ignoreClass' is True, then all interfaces are
        considered appropriate.

        Otherwise, an 'appropriate' interface is one for which its class has
        been registered as an adapter for my class according to the rules of
        getComponent.
        """
        for iface in declarations.providedBy(component):
            if (ignoreClass or (self.locateAdapterClass(
                    self.__class__, iface, None) == component.__class__)):
                self._adapterCache[reflect.qual(iface)] = component
Beispiel #17
0
 def addComponent(self, component, ignoreClass=0, registry=None):
     """
     Add a component to me, for all appropriate interfaces.
     In order to determine which interfaces are appropriate, the component's
     provided interfaces will be scanned.
     If the argument 'ignoreClass' is True, then all interfaces are
     considered appropriate.
     Otherwise, an 'appropriate' interface is one for which its class has
     been registered as an adapter for my class according to the rules of
     getComponent.
     @return: the list of appropriate interfaces
     """
     if hasattr(component, "__class__"):
         fixClassImplements(component.__class__)
     for iface in declarations.providedBy(component):
         if (ignoreClass or
             (self.locateAdapterClass(self.__class__, iface, None, registry)
              == component.__class__)):
             self._adapterCache[reflect.qual(iface)] = component
    def __get__(self, inst, cls=None):

        # We're looking at a class - fall back on default
        if inst is None:
            return getObjectSpecification(cls)

        # Find the cached value.
        cache = getattr(inst, '_v__providedBy__', None)

        # Find the data we need to know if our cache needs to be invalidated
        provided = alias_provides = getattr(inst, '__provides__', None)

        # See if we have a valid cache, and if so return it
        if cache is not None:
            cached_mtime, cached_provides, cached_provided = cache

            if (
                inst._p_mtime == cached_mtime and
                alias_provides is cached_provides
            ):
                return cached_provided

        # If the instance doesn't have a __provides__ attribute, get the
        # interfaces implied by the class as a starting point.
        if provided is None:
            assert cls == Alias # XXX: remove
            provided = implementedBy(cls)

        # Add the interfaces provided by the target
        target = aq_base(inst._target)
        if target is None:
            return provided # don't cache yet!

        # Add the interfaces provided by the target, but take away
        # IHasAlias if set
        provided += providedBy(target) - IHasAlias - IIterateAware

        if ITranslatable:
            provided -= ITranslatable

        inst._v__providedBy__ = inst._p_mtime, alias_provides, provided
        return provided
Beispiel #19
0
    def __get__(self, inst, cls=None):

        # We're looking at a class - fall back on default
        if inst is None:
            return getObjectSpecification(cls)

        # Find the cached value.
        cache = getattr(inst, '_v__providedBy__', None)

        # Find the data we need to know if our cache needs to be invalidated
        provided = alias_provides = getattr(inst, '__provides__', None)

        # See if we have a valid cache, and if so return it
        if cache is not None:
            cached_mtime, cached_provides, cached_provided = cache

            if (inst._p_mtime == cached_mtime
                    and alias_provides is cached_provides):
                return cached_provided

        # If the instance doesn't have a __provides__ attribute, get the
        # interfaces implied by the class as a starting point.
        if provided is None:
            assert cls == Alias  # XXX: remove
            provided = implementedBy(cls)

        # Add the interfaces provided by the target
        target = aq_base(inst._target)
        if target is None:
            return provided  # don't cache yet!

        # Add the interfaces provided by the target, but take away
        # IHasAlias if set
        provided += providedBy(target) - IHasAlias - IIterateAware

        if ITranslatable:
            provided -= ITranslatable

        inst._v__providedBy__ = inst._p_mtime, alias_provides, provided
        return provided
Beispiel #20
0
def _hook(iface, ob, lookup=_vcoRegistry.lookup1):
    factory = lookup(declarations.providedBy(ob), iface)
    if factory is None:
        return None
    else:
        return factory(ob)
Beispiel #21
0
 def _hook(iface, ob):
     factory = lookup(declarations.providedBy(ob), iface)
     if factory is None:
         return None
     else:
         return factory(ob)
Beispiel #22
0
 def providedBy(self, ob):
     """Is the interface implemented by an object
     """
     spec = providedBy(ob)
     return self in spec._implied
Beispiel #23
0
 def _adapter_hook(self, iface, ob):
     factory = self._adapters.lookup1(declarations.providedBy(ob), iface)
     return factory and factory(ob)
 def providedBy(self, ob):
     """Is the interface implemented by an object
     """
     spec = providedBy(ob)
     return self in spec._implied
Beispiel #25
0
def getFlattener(original):
    """Get a flattener function with signature (ctx, original) for the object original.
    """
    return tpc.globalRegistry.lookup1(declarations.providedBy(original),
                                      ISerializable, 'nevow.flat')
def _lookup_adapter_hook(iface, ob):
    factory = registry.lookup1(declarations.providedBy(ob), iface)
    return factory and factory(ob)
def _lookup_adapter_hook(iface, ob):
    factory = registry.lookup1(declarations.providedBy(ob), iface)
    return factory and factory(ob)
 def _hook(iface, ob):
     factory = lookup(declarations.providedBy(ob), iface)
     if factory is None:
         return None
     else:
         return factory(ob)
Beispiel #29
0
def _hook(iface, ob, lookup=_vcoRegistry.lookup1):
    factory = lookup(declarations.providedBy(ob), iface)
    if factory is None:
        return None
    else:
        return factory(ob)
Beispiel #30
0
def queryType(object, interface):
    """Returns the object's interface which implements interface.

    >>> from zope.interface import Interface
    >>> class IContentType(Interface):
    ...    pass
    >>> from zope.interface import Interface, implements, directlyProvides
    >>> class I(Interface):
    ...     pass
    >>> class J(Interface):
    ...     pass
    >>> directlyProvides(I, IContentType)
    >>> class C(object):
    ...     implements(I)
    >>> class D(object):
    ...     implements(J,I)
    >>> obj = C()
    >>> c1_ctype = queryType(obj, IContentType)
    >>> c1_ctype.__name__
    'I'
    >>> class I1(I):
    ...     pass
    >>> class I2(I1):
    ...     pass
    >>> class I3(Interface):
    ...     pass
    >>> class C1(object):
    ...     implements(I1)
    >>> obj1 = C1()
    >>> c1_ctype = queryType(obj1, IContentType)
    >>> c1_ctype.__name__
    'I'
    >>> class C2(object):
    ...     implements(I2)
    >>> obj2 = C2()
    >>> c2_ctype = queryType(obj2, IContentType)
    >>> c2_ctype.__name__
    'I'

    >>> class C3(object):
    ...     implements(I3)
    >>> obj3 = C3()

    If Interface doesn't provide `IContentType`, `queryType` returns ``None``.

    >>> c3_ctype = queryType(obj3, IContentType)
    >>> c3_ctype
    >>> c3_ctype is None
    True
    >>> class I4(I):
    ...     pass
    >>> directlyProvides(I4, IContentType)
    >>> class C4(object):
    ...     implements(I4)
    >>> obj4 = C4()
    >>> c4_ctype = queryType(obj4, IContentType)
    >>> c4_ctype.__name__
    'I4'

    """
    # Remove the security proxy, so that we can introspect the type of the
    # object's interfaces.
    naked = removeSecurityProxy(object)
    object_iro = providedBy(naked).__iro__
    for iface in object_iro:
        if interface.providedBy(iface):
            return iface

    return None
Beispiel #31
0
def getFlattener(original):
    """Get a flattener function with signature (ctx, original) for the object original.
    """
    return tpc.globalRegistry.lookup1(declarations.providedBy(original), ISerializable, 'nevow.flat')