Example #1
0
 def get(self, directive, component, get_default):
     result = directive.store.get(directive, component, _USE_DEFAULT)
     if result is not _USE_DEFAULT:
         return result
     # We may be really dealing with an instance instead of a class.
     if not util.isclass(component):
         component = component.__class__
     return _default(inspect.getmro(component), get_default)
Example #2
0
def component_includes(component, *resources):
    if isclass(component):
        if not IResourcesIncluder.implementedBy(component):
            classImplements(component, IResourcesIncluder)
    else:
        if not IResourcesIncluder.providedBy(component):
            alsoProvides(component, IResourcesIncluder)

    include.set(component, resources)
Example #3
0
    def get(self, directive, component, get_default):
        # look up class-level directive on this class or its bases
        # we don't need to loop through the __mro__ here as Python will
        # do it for us
        result = directive.store.get(directive, component, _USE_DEFAULT)
        if result is not _USE_DEFAULT:
            return result

        # we may be really dealing with an instance or a module here
        if not util.isclass(component):
            return get_default(component, component)

        # now we need to loop through the mro, potentially twice
        mro = inspect.getmro(component)
        # look up module-level directive for this class or its bases
        for base in mro:
            module_of_base = scan.resolve(base.__module__)
            result = directive.store.get(directive, module_of_base,
                                         _USE_DEFAULT)
            if result is not _USE_DEFAULT:
                return result
        # look up default rule for this class or its bases
        return _default(mro, get_default)
 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
    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
Example #6
0
def validateClass(directive, value):
    if not util.isclass(value):
        raise GrokImportError("The '%s' directive can only be called with "
                              "a class." % directive.name)
Example #7
0
def validateInterfaceOrClass(directive, value):
    if not (IInterface.providedBy(value) or util.isclass(value)):
        raise GrokImportError("The '%s' directive can only be called with "
                              "a class or an interface." % directive.name)