Package: weedi
IntroductionWeedi is a dependency injection container based onsetuptoolsandentry
points.It was first developed by [@Net-ng](https://github.com/Net-ng) and later
opensourced and maintained by Weenect.It is tested for python’s versions 2.7, 3.3, 3.4 and 3.5InstallationYou will need to havesetuptoolsinstalled and to use it in your
project as weedi uses theentry_pointsfeature.You can addweedias a dependency of your project in your setup.py :fromsetuptoolsimportsetup,find_packagessetup(...install_requires=('weedi'),...)Then runpython setup.py installorpython setup.py developUsageLet’s suppose you are managing locators and you have a service
LocatorManager which depends on the database.You will have these class in a moduleservices.py:importweedi.loadableasloadableclassDatabase(loadable.Service):spec={'host':'string(default="localhost")','port':'integer(default=3306)','debug':'boolean(default=False)',}load_priority=-10def__init__(self,host,port,debug):self.host=hostself.port=portself.debug=debugclassLocatorManager(loadable.Service):def__init__(self,database_service):self.db=database_serviceYou need to register these services in theentry_pointsor your
projectsetup.py.setup(...entry_points='''
        [services]
        database = my_project.services:Database
        manager = my_project.services:LocatorManager
    '''...)Note : the name of the argument in LocatorManager constructor is
database_service. This is the concatenation of the code of the database
entry point and the string ``_service``. This is the way the library
recognizes that it needs to inject the database service when it
instanciates the manager service.Then, you need to configure your container. Let’s create a modulerepository.py. When creating a container, you need to define the
name of the section managed by this repository in the entry points
(here, it will beservices) and the name of the section in the
config file for this repository (cf config.ini below, it will beservicestoo).importweedi.loadables_repositoryasloadables_repositoryclassServicesRepository(loadables_repository.LoadablesRepository):entry_point='services'conf_section='services'The database service will have default value injected when it is created
based on its spec. You can override this by creating a config fileconfig.ini:[services][[database]]host="database.local"port=5432debug=TrueEverything is ready. You just have to start your container.service_repository=ServicesRepository()service_repository.load('path_to/config.ini')You can access the services from the container :database_service=service_repository['database']locator_manager_service=service_repository['manager']You can inject these services in an object by constructor or by method :classObjectNeedsService(object):def__init__(self,database_service):self.db=database_serviceself.manager=Nonedefset_services(self,manager_service):self.manager=manager_servicenew_instance=service_repository(ObjectNeedsService)assertnew_instance.db==service_repository['database']assertnew_instance.managerisNoneservice_repository(new_instance.set_services)assertnew_instance.db==service_repository['database']assertnew_instance.manager==service_repository['manager']You can pass arguments to the called function when using the container :classObjectWithArgs(object):def__init__(self,param1,param2,database_service,param3=None,param4={}):self.db=database_serviceself.param1=param1self.param2=param2self.param3=param3self.param4=param4new_instance=service_repository(ObjectWithArgs,'param1','param2',param4='param4')assertnew_instance.db==service_repository['database']assertnew_instance.param1=='param1'assertnew_instance.param2=='param2'assertnew_instance.param3isNoneassertnew_instance.param4=='param4'The ``project`` folder is used to both run functional tests and to
provide examples of use cases. Don’t hesitate to go see the `test
cases <https://github.com/weenect/weedi/blob/master/project/project/tests.py>`__Troubleshooting.You are getting an exceptionServiceWrongPriority: change the
load_priority value of your services to change the order of
instanciation. The lesser the value is, the sooner it is
instanciated.You are getting an exceptionServiceMissing: you forgot to
define (or mispelled) a service definition in your project entry
points.You are getting an exceptionWrongConfiguration: You are missing
some configuration key for a service in your config file or you are
missing a config file altogether.