Ejemplo n.º 1
0
def describe_compositing_GuiceData_instances():

    class GrandDaddy(object): pass
    g_data = GuiceData.from_class(GrandDaddy)
    g_data.init = object()
    g_data.methods = {'g_method': object(),'method': object()}

    class Daddy(GrandDaddy): pass
    d_data = GuiceData.from_class(Daddy)
    d_data.init = object()
    d_data.methods = {'d_method': object(),'method': object()}

    class Son(Daddy): pass
    s_data = GuiceData.from_class(Son)
    s_data.init = object()
    s_data.methods = {'s_method': object()}

    data = GuiceData.composite_from_class(Son)

    def should_have_create_a_new_GuiceData_instance():
        assert isinstance(data, GuiceData)
        assert data not in (g_data, d_data, s_data)

    def should_have_all_unique_methods():
        assert data.methods['g_method'] == g_data.methods['g_method']
        assert data.methods['d_method'] == d_data.methods['d_method']
        assert data.methods['s_method'] == s_data.methods['s_method']

    def should_use_the_last_method_defined():
        assert data.methods['method'] == d_data.methods['method']
Ejemplo n.º 2
0
    def describe_with_instance_attached():
        existing_data = GuiceData()
        class Dummy(object):
            __guice__ = existing_data

        data = GuiceData.from_class(Dummy)

        def should_return_existing_instance():
            assert existing_data is data
Ejemplo n.º 3
0
    def describe_when_using_builtin_types():
        data0 = GuiceData.from_class(object)
        data1 = GuiceData.from_class(object)

        def should_have_gotten_GuiceData_instances():
            assert isinstance(data0, GuiceData)
            assert isinstance(data1, GuiceData)

        def should_return_new_instance_each_time():
            assert data0 is not data1

        def should_not_be_storing_instance():
            assert not hasattr(object, '__guice__')
Ejemplo n.º 4
0
 def get(self):
     kwargs = {}
     method_name = method.__name__
     guice_data = _GuiceData.from_class(module.__class__)
     injectable_args = guice_data.methods.get(method_name, {})
     for name, guicearg in injectable_args.items():
         kwargs[name] = self._injector.get_instance(
                 guicearg.datatype, guicearg.annotation)
     return method(**kwargs)
Ejemplo n.º 5
0
 def get(self):
     kwargs = {}
     method_name = method.__name__
     guice_data = _GuiceData.from_class(module.__class__)
     injectable_args = guice_data.methods.get(method_name, {})
     for name, guicearg in injectable_args.items():
         kwargs[name] = self._injector.get_instance(
                 guicearg.datatype, guicearg.annotation)
     return method(**kwargs)
Ejemplo n.º 6
0
    def describe_without_instance_attached():
        class Dummy(object): pass

        data = GuiceData.from_class(Dummy)

        def should_return_a_GuiceData_instance():
            assert isinstance(data, GuiceData)

        def should_save_create_class_attribute():
            assert Dummy.__guice__ is data
Ejemplo n.º 7
0
    def inject_members(self, instance):
        # this may be a little slow; done twice
        guice_data = _GuiceData.composite_from_class(instance.__class__)

        for name, gm in guice_data.methods.items():
            kwargs = {}
            for param, guicearg in gm.items():
                kwargs[param] = self.get_instance(guicearg.datatype,
                                                  guicearg.annotation)
            getattr(instance, name)(**kwargs)
        return instance
Ejemplo n.º 8
0
    def inject_members(self, instance):
        # this may be a little slow; done twice
        guice_data = _GuiceData.composite_from_class(instance.__class__)

        for name, gm in guice_data.methods.items():
            kwargs = {}
            for param, guicearg in gm.items():
                kwargs[param] = self.get_instance(guicearg.datatype,
                                                  guicearg.annotation)
            getattr(instance, name)(**kwargs)
        return instance
Ejemplo n.º 9
0
    def _assisted_inject(func):
        if func.__name__ != '__init__':
            raise AssistError('assisted_inject can only be used on __init__s')

        class_locals = enclosing_frame().f_locals

        guice_data = GuiceData.from_class_dict(class_locals)
        guice_data.assisted = True  # TODO: I don't like this, but it works for now

        annotations = getattr(func, '__guice_annotations__', {})

        guice_data.init = dict(
            (k, GuiceArg(v, annotations.get(k))) for k, v in kwargs.items())

        return func
Ejemplo n.º 10
0
def AssistProvider(cls):
    guice_data = GuiceData.from_class(cls)
    if not getattr(guice_data, 'assisted', False):
        raise AssistError('AssistProvider can only by used on '
                          '@assisted_inject-ed classes')

    class _AssistProvider(object):
        @inject(injector=Injector)
        def __init__(self, injector):
            self._injector = injector

        def get(self):
            return build_factory(self._injector, cls)

    return _AssistProvider
Ejemplo n.º 11
0
    def _assisted_inject(func):
        if func.__name__ != '__init__':
            raise AssistError('assisted_inject can only be used on __init__s')

        class_locals = enclosing_frame().f_locals

        guice_data = GuiceData.from_class_dict(class_locals)
        guice_data.assisted = True # TODO: I don't like this, but it works for now

        annotations = getattr(func, '__guice_annotations__', {})

        guice_data.init = dict((k, GuiceArg(v, annotations.get(k)))
                                for k, v in kwargs.items())

        return func
Ejemplo n.º 12
0
def AssistProvider(cls):
    guice_data = GuiceData.from_class(cls)
    if not getattr(guice_data, 'assisted', False):
        raise AssistError('AssistProvider can only by used on '
                '@assisted_inject-ed classes')

    class _AssistProvider(object):

        @inject(injector=Injector)
        def __init__(self, injector):
            self._injector = injector

        def get(self):
            return build_factory(self._injector, cls)

    return _AssistProvider
Ejemplo n.º 13
0
    def create_object(self, cls):
        if not hasattr(cls, '__mro__'):
            warnings.warn("can't create an instance of %r - no __mro__; "
                         "this legacy behavior will be removed in a future "
                          "version")
            return cls

        guice_data = _GuiceData.composite_from_class(cls)

        if not guice_data.init:
            instance = cls()
        else:
            kwargs = {}
            for name, guicearg in guice_data.init.items():
                kwargs[name] = self.get_instance(guicearg.datatype,
                                                 guicearg.annotation)
            instance = cls(**kwargs)

        return instance
Ejemplo n.º 14
0
    def create_object(self, cls):
        if not hasattr(cls, '__mro__'):
            warnings.warn("can't create an instance of %r - no __mro__; "
                         "this legacy behavior will be removed in a future "
                          "version")
            return cls

        guice_data = _GuiceData.composite_from_class(cls)

        if not guice_data.init:
            instance = cls()
        else:
            kwargs = {}
            for name, guicearg in guice_data.init.items():
                kwargs[name] = self.get_instance(guicearg.datatype,
                                                 guicearg.annotation)
            instance = cls(**kwargs)

        return instance
Ejemplo n.º 15
0
def build_factory(injector, cls):
    guice_data = GuiceData.from_class(cls)

    providers = {}
    for name, guicearg in guice_data.init.items():
        providers[name] = injector.get_provider(guicearg.datatype,
                                                guicearg.annotation)

    all_args = inspect.getargspec(cls.__init__).args[1:]
    needed_args = set(all_args) - set(providers.keys())

    class DynamicFactory(object):

        def create(self, **kwargs):
            if set(kwargs.keys()) - needed_args:
                raise TypeError('TODO: error message here about too many values')

            for name, provider in providers.items():
                kwargs[name] = provider.get()
            return cls(**kwargs)

    return DynamicFactory()
Ejemplo n.º 16
0
def build_factory(injector, cls):
    guice_data = GuiceData.from_class(cls)

    providers = {}
    for name, guicearg in guice_data.init.items():
        providers[name] = injector.get_provider(guicearg.datatype,
                                                guicearg.annotation)

    all_args = inspect.getargspec(cls.__init__).args[1:]
    needed_args = set(all_args) - set(providers.keys())

    class DynamicFactory(object):
        def create(self, **kwargs):
            if set(kwargs.keys()) - needed_args:
                raise TypeError(
                    'TODO: error message here about too many values')

            for name, provider in providers.items():
                kwargs[name] = provider.get()
            return cls(**kwargs)

    return DynamicFactory()
Ejemplo n.º 17
0
def describe_initializing_GuiceData_from_a_dict():
    d = dict()
    data = GuiceData.from_class_dict(d)

    def should_have_a_GuiceData_instance_in_dict():
        assert isinstance(d['__guice__'], GuiceData)
Ejemplo n.º 18
0
def describe_getting_GuiceData_from_a_dict():
    d = dict(__guice__=object())
    data = GuiceData.from_class_dict(d)

    def should_get_the_same_instance():
        assert d['__guice__'] is data