def load_service(_context, service): # prevent circular import from guillotina.security.utils import protect_view service_conf = service["config"] factory = resolve_dotted_name(service["klass"]) permission = service_conf.get("permission", app_settings.get("default_permission", None)) protect_view(factory, permission) method = service_conf.get("method", "GET") default_layer = resolve_dotted_name( app_settings.get("default_layer", IDefaultLayer)) layer = service_conf.get("layer", default_layer) name = service_conf.get("name", "") content = service_conf.get("context", Interface) logger.debug("Defining adapter for " # noqa "{0:s} {1:s} {2:s} to {3:s} name {4:s}".format( content.__identifier__, app_settings["http_methods"][method].__identifier__, layer.__identifier__, str(factory), name, )) if not getattr(factory, "__route__", None): factory.__route__ = routes.Route(name) else: factory.__route__.service_configuration = service_conf service_conf["route"] = [str(rp) for rp in factory.__route__.route_parts] component.adapter( _context, factory=(factory, ), provides=app_settings["http_methods"][method], for_=(content, layer), name=factory.__route__.view_name, ) api = app_settings["api_definition"] ct_name = content.__identifier__ if ct_name not in api: api[ct_name] = OrderedDict() ct_api = api[ct_name] if name: if "endpoints" not in ct_api: ct_api["endpoints"] = OrderedDict() if name not in ct_api["endpoints"]: ct_api["endpoints"][name] = OrderedDict() ct_api["endpoints"][name][method] = OrderedDict(service_conf) else: ct_api[method] = OrderedDict(service_conf)
def load_adapter(_context, adapter): conf = adapter['config'] klass = resolve_dotted_name(adapter['klass']) factory = conf.pop('factory', None) or klass _component_conf(conf) if 'provides' in conf and isinstance(klass, type): # not sure if this is what we want or not for sure but # we are automatically applying the provides interface to # registered class objects classImplements(klass, conf['provides']) component.adapter(_context, factory=(factory, ), **conf)
def load_service(_context, service): # prevent circular import from guillotina.security.utils import protect_view service_conf = service['config'] factory = resolve_dotted_name(service['klass']) permission = service_conf.get( 'permission', app_settings.get('default_permission', None)) protect_view(factory, permission) method = service_conf.get('method', 'GET') default_layer = resolve_dotted_name( app_settings.get('default_layer', IDefaultLayer)) layer = service_conf.get('layer', default_layer) name = service_conf.get('name', '') content = service_conf.get('context', Interface) logger.debug('Defining adapter for ' # noqa '{0:s} {1:s} {2:s} to {3:s} name {4:s}'.format( content.__identifier__, app_settings['http_methods'][method].__identifier__, layer.__identifier__, str(factory), name)) if not getattr(factory, '__route__', None): factory.__route__ = routes.Route(name) else: factory.__route__.service_configuration = service_conf component.adapter( _context, factory=(factory,), provides=app_settings['http_methods'][method], for_=(content, layer), name=factory.__route__.view_name ) api = app_settings['api_definition'] ct_name = content.__identifier__ if ct_name not in api: api[ct_name] = OrderedDict() ct_api = api[ct_name] if name: if 'endpoints' not in ct_api: ct_api['endpoints'] = OrderedDict() if name not in ct_api['endpoints']: ct_api['endpoints'][name] = OrderedDict() ct_api['endpoints'][name][method] = OrderedDict(service_conf) else: ct_api[method] = OrderedDict(service_conf)
def load_adapter(_context, adapter): conf = adapter['config'] klass = resolve_dotted_name(adapter['klass']) factory = conf.pop('factory', None) or klass _component_conf(conf) if 'provides' in conf and isinstance(klass, type): # not sure if this is what we want or not for sure but # we are automatically applying the provides interface to # registered class objects classImplements(klass, conf['provides']) component.adapter( _context, factory=(factory,), **conf )
def test_configuration_machine_allows_overriding(): from guillotina.configure.config import ConfigurationMachine from guillotina.configure import component from guillotina.component import adapter, named, getAdapter from zope.interface import implementer, Interface class IFoo(Interface): pass @implementer(IFoo) class Foo(object): pass class IBar(Interface): pass cm = ConfigurationMachine() @adapter(IFoo) @implementer(IBar) @named('bar') class _Factory(object): def __init__(self, context): self.context = context class _FactoryOverride(_Factory): pass component.adapter(cm, (_Factory, )) cm.execute_actions() cm.commit() component.adapter(cm, (_FactoryOverride, )) cm.execute_actions() adapter = getAdapter(Foo(), name='bar') assert isinstance(adapter, _FactoryOverride)
def test_configuration_machine_allows_overriding(): from guillotina.configure.config import ConfigurationMachine from guillotina.configure import component from guillotina.component import adapter, get_adapter from zope.interface import implementer, Interface, named class IFoo(Interface): pass @implementer(IFoo) class Foo(object): pass class IBar(Interface): pass cm = ConfigurationMachine() @adapter(IFoo) @implementer(IBar) @named('bar') class _Factory(object): def __init__(self, context): self.context = context class _FactoryOverride(_Factory): pass component.adapter(cm, (_Factory,)) cm.execute_actions() cm.commit() component.adapter(cm, (_FactoryOverride,)) cm.execute_actions() adapter = get_adapter(Foo(), name='bar') assert isinstance(adapter, _FactoryOverride)
def load_behavior(_context, behavior): conf = behavior['config'] klass = resolve_dotted_name(behavior['klass']) factory = conf.get('factory') or klass real_factory = resolve_dotted_name(factory) if IInterface.providedBy(real_factory): # create concret class to register for behavior schema = real_factory from guillotina.behaviors.instance import AnnotationBehavior class real_factory(AnnotationBehavior): __annotations_data_key__ = conf.get('data_key', 'default') auto_serialize = conf.get('auto_serialize', True) else: schema = resolve_dotted_name(conf['provides']) classImplements(real_factory, schema) name = conf.get('name') name_only = conf.get('name_only', False) title = conf.get('title', '') for_ = resolve_dotted_name(conf.get('for_')) marker = resolve_dotted_name(conf.get('marker')) if marker is None and real_factory is None: marker = schema if marker is not None and real_factory is None and marker is not schema: 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 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 IBehaviorSchemaAwareFactory.providedBy(factory): factory = factory(schema) registration = BehaviorRegistration( title=conf.get('title', ''), description=conf.get('description', ''), interface=schema, marker=marker, factory=real_factory, name=name, for_=for_ ) if not name_only: # behavior registration by provides interface identifier component.utility( _context, provides=IBehavior, name=schema.__identifier__, component=registration ) if name is not None: # for convinience we register with a given name component.utility( _context, provides=IBehavior, name=name, component=registration ) if factory is None: if for_ is not None: logger.warning( 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) component.adapter( _context, factory=(adapter_factory,), provides=schema, for_=(for_,) )
def _callFUT(self, *args, **kw): from guillotina.configure.component import adapter return adapter(*args, **kw)
def _callFUT(self, *args, **kw): # noqa: N802 from guillotina.configure.component import adapter return adapter(*args, **kw)