예제 #1
0
def _getAdapterRequired(factory, required):
    if required is None:
        try:
            required = factory.__component_adapts__
        except AttributeError:
            raise TypeError(
                "The adapter factory doesn't have a __component_adapts__ "
                "attribute and no required specifications were specified"
                )
    elif ISpecification.providedBy(required):
        raise TypeError("the required argument should be a list of "
                        "interfaces, not a single interface")

    result = []
    for r in required:
        if r is None:
            r = Interface
        elif not ISpecification.providedBy(r):
            if isinstance(r, class_types):
                r = implementedBy(r)
            else:
                raise TypeError("Required specification must be a "
                                "specification or class."
                                )
        result.append(r)
    return tuple(result)
예제 #2
0
def _getAdapterRequired(factory, required):
    if required is None:
        try:
            required = factory.__component_adapts__
        except AttributeError:
            raise TypeError(
                "The adapter factory doesn't have a __component_adapts__ "
                "attribute and no required specifications were specified"
                )
    elif ISpecification.providedBy(required):
        raise TypeError("the required argument should be a list of "
                        "interfaces, not a single interface")

    result = []
    for r in required:
        if r is None:
            r = Interface
        elif not ISpecification.providedBy(r):
            if isinstance(r, CLASS_TYPES):
                r = implementedBy(r)
            else:
                raise TypeError("Required specification must be a "
                                "specification or class."
                                )
        result.append(r)
    return tuple(result)
예제 #3
0
 def unregister(self, for_=None, provided=Interface, name=u""):
     if for_ is None:
         for_ = tuple()
     else:
         assert isinstance(for_, tuple) or ISpecification.providedBy(for_)
     specs = readSpecification(for_, specificationOfClass)
     self.components.unregister(specs, provided, name)
예제 #4
0
 def get_interface((name, listing)):
     identifier = grok.name.bind().get(listing)
     if not ISpecification.providedBy(listing.interface):
         return map(
             lambda (name, interface): (name, interface, identifier),
             listing.interface)
     return [(str(name), listing.interface, identifier)]
예제 #5
0
def get_specification(spec, force=False):
    """Get the specification of the given object.

    If the given object is already a specification acceptable to the component
    architecture, it is simply returned. This is true for classes
    and specification objects (which includes interfaces).

    In case of instances, an interface is generated on the fly and tagged onto
    the object. Then the interface is returned as the specification.
    """
    # If the specification is an instance, then we do some magic.
    if (force or (spec is not None and not ISpecification.providedBy(spec)
                  and not isinstance(spec, class_types))):

        # Step 1: Calculate an interface name
        iface_name = 'IGeneratedForObject_%i' % id(spec)

        # Step 2: Find out if we already have such an interface
        existing_interfaces = [
            i for i in directlyProvidedBy(spec) if i.__name__ == iface_name
        ]

        # Step 3a: Return an existing interface if there is one
        if len(existing_interfaces) > 0:
            spec = existing_interfaces[0]
        # Step 3b: Create a new interface if not
        else:
            iface = InterfaceClass(iface_name)
            alsoProvides(spec, iface)
            spec = iface
    return spec
예제 #6
0
파일: registry.py 프로젝트: Cromlech/crom
def interfaces(requirements):
    ifaces = []
    for requirement in requirements:
        if ISpecification.providedBy(requirement):
            ifaces.append(requirement)
            continue
        if isinstance(requirement, CLASS_TYPES):
            ifaces.append(implementedBy(requirement))
        else:
            raise TypeError("Sources must either be " "an interface or a class.")
    return ifaces
예제 #7
0
def interfaces(requirements):
    ifaces = []
    for requirement in requirements:
        if ISpecification.providedBy(requirement):
            ifaces.append(requirement)
            continue
        if isinstance(requirement, CLASS_TYPES):
            ifaces.append(implementedBy(requirement))
        else:
            raise TypeError("Sources must either be "
                            "an interface or a class.")
    return ifaces
예제 #8
0
파일: registry.py 프로젝트: faassen/crom
 def register(self, sources, target, name, component):
     iface_sources = []
     for source in sources:
         if ISpecification.providedBy(source):
             iface_sources.append(source)
             continue
         if isinstance(source, CLASS_TYPES):
             iface_sources.append(implementedBy(source))
         else:
             raise TypeError("Sources must either be "
                             "an interface or a class.")
     self.registry.register(iface_sources, target, name, component)
예제 #9
0
def verify_context(cls, obj, implements=False):
    require = None
    if isinstance(cls, (types.ClassType, type)):
        require = specification = grok.context.bind(default=None).get(cls)
        if require is None:
            return True, require
        if not ISpecification.providedBy(specification):
            specification = implementedBy(specification)
        if implements:
            if not specification.implementedBy(obj):
                return False, require
        else:
            if not specification.providedBy(obj):
                return False, require
    return True, require
예제 #10
0
 def execute(self, factory, config, provides, name, **kw):
     specs = adaptedBy(factory)
     context = grokcore.component.context.bind(get_default=lambda *args, **kwargs: None).get(factory)
     validated_specs = []
     if specs is None:
         if context is not None:
             validated_specs = [context]
     else:
         default = context is not None and context or Interface
         for value in specs:
             if value is None:
                 validated_specs.append(default)
             elif ISpecification.providedBy(value) or isclass(value):
                 validated_specs.append(value)
             else:
                 raise GrokError(u"Invalid adaption argument %r for %r" % (value, factory))
     validated_specs = tuple(validated_specs)
     config.action(
         discriminator=("component", validated_specs, provides, name),
         callable=getSite().register,
         args=(factory, validated_specs, provides, name),
     )
     return True
예제 #11
0
    def grok(self, name, module, module_info, config, **kw):
        components = module_info.getAnnotation("zeam.components", [])

        for factory, specs, options in components:
            if set(options.keys()).difference(OPTIONS):
                raise GrokError(u"There are unknown options for %s" % factory)
            name = options.get("name", u"")
            provides = options.get("provides", Interface)
            validated_specs = []
            for value in specs:
                if value is None:
                    validated_specs.append(Interface)
                elif ISpecification.providedBy(value) or isclass(value):
                    validated_specs.append(value)
                else:
                    raise GrokError(u"Invalid adaption argument %r for %r" % (value, factory))
            validated_specs = tuple(validated_specs)
            config.action(
                discriminator=("component", validated_specs, provides, name),
                callable=getSite().register,
                args=(factory, validated_specs, provides, name),
            )
        return len(components) != 0
예제 #12
0
def specificationOfInstance(obj):
    if ISpecification.providedBy(obj):
        return obj
    return providedBy(obj)
예제 #13
0
def specificationOfClass(obj):
    if ISpecification.providedBy(obj):
        return obj
    return implementedBy(obj)
예제 #14
0
파일: core.py 프로젝트: Cromlech/dawnlight
def _get_interface(class_or_interface):
    if ISpecification.providedBy(class_or_interface):
        return class_or_interface
    return implementedBy(class_or_interface)
예제 #15
0
def _get_interface(class_or_interface):
    if ISpecification.providedBy(class_or_interface):
        return class_or_interface
    return implementedBy(class_or_interface)