def behaviorDirective(_context, title, provides, description=None, marker=None, factory=None, for_=None): if marker is None and factory is None: marker = provides if factory is None and marker is not None and marker is not provides: raise ConfigurationError( u"You cannot specify a different 'marker' and 'provides' if " u"there is no adapter factory for the provided interface." ) # Instantiate the real factory if it's the schema-aware type. We do # this here so that the for_ interface may take this into account. if factory is not None and ISchemaAwareFactory.providedBy(factory): factory = factory(provides) # Attempt to guess the factory's adapted interface and use it as the for_ if for_ is None and factory is not None: adapts = getattr(factory, '__component_adapts__', None) if adapts: if len(adapts) != 1: raise ConfigurationError( u"The factory cannot be declared a multi-adapter." ) for_ = adapts[0] else: for_ = Interface elif for_ is None: for_ = Interface registration = BehaviorRegistration( title=title, description=description, interface=provides, marker=marker, factory=factory ) adapter_factory = BehaviorAdapterFactory(registration) utility( _context, provides=IBehavior, name=provides.__identifier__, component=registration ) if factory is not None: adapter( _context, factory=(adapter_factory,), provides=provides, for_=(for_,) )
def behaviorDirective(_context, title, provides, description=None, marker=None, factory=None, for_=None): if marker is None and factory is None: marker = provides if factory is None and marker is not None and marker is not provides: raise ConfigurationError( u"You cannot specify a different 'marker' and 'provides' if " u"there is no adapter factory for the provided interface.") # Instantiate the real factory if it's the schema-aware type. We do # this here so that the for_ interface may take this into account. if factory is not None and ISchemaAwareFactory.providedBy(factory): factory = factory(provides) # Attempt to guess the factory's adapted interface and use it as the for_ if for_ is None and factory is not None: adapts = getattr(factory, '__component_adapts__', None) if adapts: if len(adapts) != 1: raise ConfigurationError( u"The factory cannot be declared a multi-adapter.") for_ = adapts[0] else: for_ = Interface elif for_ is None: for_ = Interface registration = BehaviorRegistration(title=title, description=description, interface=provides, marker=marker, factory=factory) adapter_factory = BehaviorAdapterFactory(registration) utility(_context, provides=IBehavior, name=provides.__identifier__, component=registration) if factory is not None: adapter(_context, factory=(adapter_factory, ), provides=provides, for_=(for_, ))
def behaviorDirective(_context, title, provides, name=None, description=None, marker=None, factory=None, for_=None): if marker is None and factory is None: marker = provides if marker is not None and factory is None and marker is not provides: raise ConfigurationError( u"You cannot specify a different 'marker' and 'provides' if " u"there is no adapter factory for the provided interface." ) # Instantiate the real factory if it's the schema-aware type. We do # this here so that the for_ interface may take this into account. if factory is not None and ISchemaAwareFactory.providedBy(factory): factory = factory(provides) registration = BehaviorRegistration( title=title, description=description, interface=provides, marker=marker, factory=factory, name=name ) # behavior registration by provides interface identifier utility(_context, provides=IBehavior, name=provides.__identifier__, component=registration) if name is not None: # for convinience we register with a given name utility(_context, provides=IBehavior, name=name, component=registration) if factory is None: if for_ is not None: logger.warn( u"Specifying 'for' in behavior '{0}' if no 'factory' is given " u"has no effect and is superfluous.".format(title) ) # w/o factory we're done here return if for_ is None: # Attempt to guess the factory's adapted interface and use it as # the 'for_'. # Fallback to '*' (=Interface). adapts = getattr(factory, "__component_adapts__", None) or [Interface] if len(adapts) != 1: raise ConfigurationError(u"The factory can not be declared as multi-adapter.") for_ = adapts[0] adapter_factory = BehaviorAdapterFactory(registration) adapter(_context, factory=(adapter_factory,), provides=provides, for_=(for_,))
def behaviorDirective(_context, title, provides, name=None, description=None, marker=None, factory=None, for_=None): if marker is None and factory is None: marker = provides if marker is not None and factory is None and marker is not provides: raise ConfigurationError( u"You cannot specify a different 'marker' and 'provides' if " u"there is no adapter factory for the provided interface.") # Instantiate the real factory if it's the schema-aware type. We do # this here so that the for_ interface may take this into account. if factory is not None and ISchemaAwareFactory.providedBy(factory): factory = factory(provides) registration = BehaviorRegistration( title=title, description=description, interface=provides, marker=marker, factory=factory, name=name, ) # behavior registration by provides interface identifier utility(_context, provides=IBehavior, name=provides.__identifier__, component=registration) if name is not None: # for convinience we register with a given name utility(_context, provides=IBehavior, name=name, component=registration) if factory is None: if for_ is not None: logger.warn( u"Specifying 'for' in behavior '{0}' if no 'factory' is given " u"has no effect and is superfluous.".format(title)) # w/o factory we're done here return if for_ is None: # Attempt to guess the factory's adapted interface and use it as # the 'for_'. # Fallback to '*' (=Interface). adapts = getattr(factory, '__component_adapts__', None) or [Interface] if len(adapts) != 1: raise ConfigurationError( u"The factory can not be declared as multi-adapter.") for_ = adapts[0] adapter_factory = BehaviorAdapterFactory(registration) adapter(_context, factory=(adapter_factory, ), provides=provides, for_=(for_, ))
def behaviorDirective(_context, title, provides, name=None, description=None, marker=None, factory=None, for_=None, name_only=False): if marker is None and factory is None: # a schema only behavior means usually direct attribute settings on the # object itself, so the object itself provides the interface. # so we mark with the provides. marker = provides if marker is not None and factory is None and marker is not provides: raise ConfigurationError( u'You cannot specify a different \'marker\' and \'provides\' if ' u'there is no adapter factory for the provided interface.') if name_only and name is None: raise ConfigurationError( u'If you decide to only register by \'name\', a name must ' u'be given.') # Instantiate the real factory if it's the schema-aware type. We do # this here so that the for_ interface may take this into account. if factory is not None and ISchemaAwareFactory.providedBy(factory): factory = factory(provides) # the behavior registration hold all information about the behavior. registration = BehaviorRegistration( title=title, description=description, interface=provides, marker=marker, factory=factory, name=name, ) # the behavior registration can be looked up as a named utility. # the name of the utility is either the full dotted path of the interface # it provides... if not name_only: # behavior registration by provides interface identifier utility(_context, provides=IBehavior, name=provides.__identifier__, component=registration) if name is not None: # .. or if given, we register with a given (short) name. # Advantage is, we can have more than one implementations of a # behavior on one object (if a factory is used). # This is handy for certain use cases. utility(_context, provides=IBehavior, name=name, component=registration) if factory is None: if for_ is not None: logger.warn( u'Specifying \'for\' in behavior \'{0}\' if no \'factory\' is ' u'given has no effect and is superfluous.'.format(title)) # w/o factory we're done here: schema only behavior return if for_ is None: for_ = _detect_for(factory, marker) adapter_factory = BehaviorAdapterFactory(registration) adapter(_context, factory=(adapter_factory, ), provides=provides, for_=(for_, ))