Example #1
0
    def bind(self, _class, **kwargs):
        key = Key(interface=_class, annotation=kwargs.get('annotated_with'))

        binding = Binding()
        binding.key = key
        binding.scope = kwargs.get('in_scope', scopes.NO_SCOPE)

        if key in self._binding_map:
            self.add_error('baseclass %r already bound' % _class)

        if 'to' in kwargs:
            if not isinstance(kwargs['to'], type):
                self.add_error('to requires a new-style class')

            binding.provider = providers.create_simple_provider(kwargs['to'])

        elif 'to_provider' in kwargs:
            #TODO: add some validation
            provider = kwargs['to_provider']
            binding.provider = provider

        elif 'to_instance' in kwargs:
            if not isinstance(kwargs['to_instance'], object):
                self.add_error('to_instance requires an instance of a '
                               'new-style class')

            provider = kwargs['to_instance']
            binding.provider = providers.create_instance_provider(provider)

        self._binding_map[key] = binding
Example #2
0
 def scope(self, key, provider):
     cached_provider = self._cached_provider_map.get(key)
     if not cached_provider:
         instance = provider.get()
         cached_provider = self._cached_provider_map[key] = \
                 providers.create_instance_provider(instance)
     return cached_provider
Example #3
0
    def bind(self, _class, **kwargs):
        key = Key(interface=_class, annotation=kwargs.get('annotated_with'))

        binding = Binding()
        binding.key = key
        binding.scope = kwargs.get('in_scope', scopes.NO_SCOPE)

        if key in self._binding_map:
            self.add_error('baseclass %r already bound' % _class)

        if 'to' in kwargs:
            if not isinstance(kwargs['to'], type):
                self.add_error('to requires a new-style class')

            binding.provider = providers.create_simple_provider(kwargs['to'])

        elif 'to_provider' in kwargs:
            #TODO: add some validation
            provider = kwargs['to_provider']
            binding.provider = provider

        elif 'to_instance' in kwargs:
            if not isinstance(kwargs['to_instance'], object):
                self.add_error('to_instance requires an instance of a '
                               'new-style class')

            provider = kwargs['to_instance']
            binding.provider = providers.create_instance_provider(provider)

        self._binding_map[key] = binding
Example #4
0
 def scope(self, key, provider):
     class SessionProvider(object):
         def get(self):
             value = cherrypy.session.get(key)
             if not value:
                 value = cherrypy.session[key] = provider.get()
             return value
     return create_instance_provider(SessionProvider())
Example #5
0
    def scope(self, key, provider):
        class SessionProvider(object):
            def get(self):
                if not hasattr(cherrypy.request, '__guicy__'):
                    cherrypy.request.__guicy__ = {}

                value = cherrypy.request.__guicy__.get(key)
                if not value:
                    value = cherrypy.request.__guicy__[key] = provider.get()
                return value
        return create_instance_provider(SessionProvider())
Example #6
0
 def _dsl_to_provider(self, to, to_provider, to_instance):
     if to:
         #TODO: add some validation
         return providers.create_simple_provider(to)
     elif to_provider:
         #TODO: add some validation
         return to_provider
     elif to_instance:
         #TODO: add some validation
         return providers.create_instance_provider(to_instance)
     else:
         raise MultiBindingError('incorrect arguments to %s.add_binding'
                 % self.__class__.__name__)
Example #7
0
    def bind(self, _class, **kwargs):
        key = Key(interface=_class, annotation=kwargs.get('annotated_with'))

        binding = Binding()
        binding.key = key
        scope = kwargs.get('in_scope')
        if scope is not None:
            if not self._scope_cache.has_key(scope):
                raise errors.BindingError("'scope' has not been bound to this Binder via bind_scope")
            scope = self._scope_cache[scope]
        else:
            scope = self._scope_cache[scopes.NO_SCOPE]

        binding.scope = scope

        if key in self._binding_map:
            raise errors.BindingError('baseclass %r already bound' % _class)

        if 'to' in kwargs:
            if not isinstance(kwargs['to'], type):
                raise errors.BindingError("'to' requires a new-style class")

            binding.provider = providers.create_simple_provider(kwargs['to'])

        elif 'to_provider' in kwargs:
            #TODO: add some validation
            provider = kwargs['to_provider']
            binding.provider = provider

        elif 'to_instance' in kwargs:
            if not isinstance(kwargs['to_instance'], object):
                raise errors.BindingError(
                    "'to_instance' requires an instance of a new-style class")

            provider = kwargs['to_instance']
            binding.provider = providers.create_instance_provider(provider)

        self._binding_map[key] = binding