Skip to content

Commit

Permalink
updated readme, changes, comments, bumped version
Browse files Browse the repository at this point in the history
  • Loading branch information
glorpen committed Feb 21, 2017
1 parent 7135b29 commit ca23a7d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
v1.3
----

- [BC break] Service.configurator no longer accepts `callable_args` and `methods_args` parameters,
you can now just inject required params.
61 changes: 59 additions & 2 deletions doc/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,65 @@ Running snippet will print:
container parameter: value from container
provided value: defined value

Arguments
---------

In cases when you want to use inject parameter already used by internal methods, eg. :meth:`glorpen.di.container.Service.call`,
you can pass args with :class:`glorpen.di.container.Kwargs` helper class.

.. code-block:: python
svc.configurator(callable=configurator, kwargs=Kwargs(router__svc=Router), other_param="param")
svc.call("some_method", kwargs=Kwargs(router__svc=Router), other_param="param")
Arguments defined by :class:`glorpen.di.container.Kwargs` are overriding ones provided by `**kwargs` notation.

Configurators
-------------

Configurators are services or callables used to configure main service.
Provided callables are given main service instance as first argument.

.. code-block:: python
def configurator(obj, some_service):
obj.some_thing = some_service()
svc.configurator(callable=configurator, some_service__svc=MyClass)
svc.configurator(service=ConfiguratorClass, method="some_method", some_service__svc=MyClass)
Factories
---------

Services that create other objects. It is possible to provide parameters/other services from Container to given callables.

.. code-block:: python
def factory(some_service):
return some_service("arg1")
svc1.factory(callable=factory, some_service__svc=MyClass)
class FactoryClass(object):
def create_new(self, some_service):
return some_service("arg1")
svc2.factory(service=FactoryClass, method="create_new", some_service__svc=MyClass)
Calling methods and settings properties
---------------------------------------

To call method on service creation:

.. code-block:: python
svc.call("some_method", some_service__svc=MyClass)
To set properties on service creation:

.. code-block:: python
svc.set(my_prop__svc=MyClass)
Using type hints for auto injection
-----------------------------------
***********************************

Sometimes it is easier to just auto-fill function arguments, when using Python3 it can be done by arguments type hinting (see :mod:`typing` for more information).

Expand Down Expand Up @@ -75,7 +132,7 @@ Snippet will create following output:


Adding custom scope
-------------------
*******************

You can define new scope by extending :class:`glorpen.di.scopes.ScopeBase`
and using :meth:`glorpen.di.container.Container.set_scope_hierarchy`.
Expand Down
6 changes: 3 additions & 3 deletions src/glorpen/di/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'''

from glorpen.di.container import Container
from glorpen.di.container import Container, Kwargs

__version__ = "1.2"
__all__ = ['Container', "__version__"]
__version__ = "1.3"
__all__ = ['Container', 'Kwargs', '__version__']
4 changes: 4 additions & 0 deletions src/glorpen/di/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,16 @@ def resolve(self, getter, param_getter):
raise Exception()

class Kwargs(object):
"""Simply wraps given kwargs for later use."""
def __init__(self, **kwargs):
super(Kwargs, self).__init__()
self.kwargs = kwargs

@classmethod
def merge(cls, *args):
"""Merges iterable arguments, can be `dict` or :class:`.Kwargs` instance.
@return: Resulting dict
"""
ret = {}
for arg in args:
if arg is None:
Expand Down
2 changes: 1 addition & 1 deletion src/glorpen/di/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, svc_name):
super(ServiceAlreadyCreated, self).__init__("Service %r is already created and could be in active use" % (svc_name,))

class RecursionException(ContainerException):
"""Raised when service definition is requiring itself, most commonly through dependencies."""
"""Raised when service definition is requiring itself."""
def __init__(self, s_def, requester_chain):
super(RecursionException, self).__init__(
"Dependency recursion error, chain was: %s"
Expand Down

0 comments on commit ca23a7d

Please sign in to comment.