Пример #1
0
    def get(self, instance=None):
        try:
            return self.cache[instance]
        except AttributeError:
            hashable = False
        except KeyError:
            hashable = True

        identity = None
        token = None

        assembly = Assembly.current()
        if instance:
            identity = '%s/%s' % (instance.__identity__, self.attr)
            if identity in assembly.configuration:
                token = identity

        if not token:
            if self.token:
                token = self.token
                #if token not in assembly.configuration and self.configuration_required:
                #    raise ConfigurationError(token)
                if identity and not assembly.should_isolate(identity):
                    identity = None
            else:
                token = identity

        key = (token, identity, self.unit)
        dependency = assembly.acquire(key, self.instantiate,
                                      (assembly, token, identity, instance))

        if hashable:
            self.cache[instance] = dependency
        return dependency
Пример #2
0
    def get(self, instance=None):
        try:
            return self.cache[instance]
        except AttributeError:
            hashable = False
        except KeyError:
            hashable = True

        identity = None
        token = None

        assembly = Assembly.current()
        if instance:
            identity = '%s/%s' % (instance.__identity__, self.attr)
            if identity in assembly.configuration:
                token = identity

        if not token:
            if self.token:
                token = self.token
                #if token not in assembly.configuration and self.configuration_required:
                #    raise ConfigurationError(token)
                if identity and not assembly.should_isolate(identity):
                    identity = None
            else:
                token = identity

        key = (token, identity, self.unit)
        dependency = assembly.acquire(key, self.instantiate, (assembly, token, identity, instance))

        if hashable:
            self.cache[instance] = dependency
        return dependency
Пример #3
0
    def get(self, instance=None):
        try:
            return self.cache[instance]
        except KeyError:
            pass

        identity = None
        token = None

        assembly = Assembly.current()
        if instance:
            identity = '%s/%s' % (instance.__identity__, self.attr)
            if identity in assembly.configuration:
                token = identity

        if not token:
            if self.token:
                token = self.token
                if token not in assembly.configuration and self.configuration_required:
                    raise ConfigurationError(token)
                if identity and not assembly.should_isolate(identity):
                    identity = None
            else:
                token = identity

        key = (token, identity, self.unit)
        self.cache[instance] = assembly.acquire(key, self.instantiate, (assembly, token, identity, instance))
        return self.cache[instance]
Пример #4
0
    def __call__(cls, *args, **params):
        assembly = params.pop("__assembly__", None)
        if not assembly:
            assembly = Assembly.current()

        identity = params.pop("__identity__", None)
        token = params.pop("__token__", None)

        if not token:
            token = identity = cls.identity

        signature = get_constructor_args(cls)
        if args:
            for i, argument in enumerate(args):
                try:
                    name = signature[i]
                    if name not in params:
                        params[name] = argument
                    else:
                        raise TypeError("duplicate arguments")
                except IndexError:
                    raise TypeError("too many arguments")

        unit = cls.__new__(cls)
        unit.__assembly__ = assembly
        unit.__identity__ = identity
        unit.__token__ = token

        # log('debug', 'instantiating unit %s: token=%r, identity=%r',
        #    cls.identity, token, identity)

        if cls.configuration:
            configuration = cls.configuration.get(unit)
            if params:
                for name, value in params.items():
                    configuration[name] = value
                    if name not in signature:
                        del params[name]

            for name in signature:
                if name in configuration:
                    params[name] = configuration[name]

        for dependency in cls.dependencies.itervalues():
            if not dependency.deferred:
                dependency.get(unit)

        unit.__init__(**params)

        return unit
Пример #5
0
    def __call__(cls, *args, **params):
        assembly = params.pop('__assembly__', None)
        if not assembly:
            assembly = Assembly.current()

        identity = params.pop('__identity__', None)
        token = params.pop('__token__', None)

        if not token:
            token = identity = cls.identity

        signature = get_constructor_args(cls)
        if args:
            for i, argument in enumerate(args):
                try:
                    name = signature[i]
                    if name not in params:
                        params[name] = argument
                    else:
                        raise TypeError('duplicate arguments')
                except IndexError:
                    raise TypeError('too many arguments')

        unit = cls.__new__(cls)
        unit.__assembly__ = assembly
        unit.__identity__ = identity
        unit.__token__ = token

        #log('debug', 'instantiating unit %s: token=%r, identity=%r',
        #    cls.identity, token, identity)

        if cls.configuration:
            configuration = cls.configuration.get(unit)
            if params:
                for name, value in params.items():
                    configuration[name] = value
                    if name not in signature:
                        del params[name]

            for name in signature:
                if name in configuration:
                    params[name] = configuration[name]

        for dependency in cls.dependencies.itervalues():
            if not dependency.deferred:
                dependency.get(unit)

        unit.__init__(**params)

        return unit