Skip to main content

A dependency injection system/container using setuptools entry points

Project description

Build Status

Introduction

Weedi is a dependency injection container based on setuptools and entry 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.5

Installation

You will need to have setuptools installed and to use it in your project as weedi uses the entry_points feature.

You can add weedi as a dependency of your project in your setup.py :

from setuptools import setup, find_packages

setup(
    ...
    install_requires=('weedi'),
    ...
)

Then run python setup.py install or python setup.py develop

Usage

Let’s suppose you are managing locators and you have a service LocatorManager which depends on the database.

You will have these class in a module services.py :

import weedi.loadable as loadable


class Database(loadable.Service):
    spec = {
        'host': 'string(default="localhost")',
        'port': 'integer(default=3306)',
        'debug': 'boolean(default=False)',
    }

    load_priority = -10

    def __init__(self, host, port, debug):
        self.host = host
        self.port = port
        self.debug = debug


class LocatorManager(loadable.Service):
    def __init__(self, database_service):
        self.db = database_service

You need to register these services in the entry_points or your project setup.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 module repository.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 be services) and the name of the section in the config file for this repository (cf config.ini below, it will be services too).

import weedi.loadables_repository as loadables_repository


class ServicesRepository(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 file config.ini :

[services]

[[database]]
host = "database.local"
port = 5432
debug = True

Everything 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 :

class ObjectNeedsService(object):
  def __init__(self, database_service):
    self.db = database_service
    self.manager = None

  def set_services(self, manager_service):
    self.manager = manager_service

new_instance = service_repository(ObjectNeedsService)
assert new_instance.db == service_repository['database']
assert new_instance.manager is None
service_repository(new_instance.set_services)
assert new_instance.db == service_repository['database']
assert new_instance.manager == service_repository['manager']

You can pass arguments to the called function when using the container :

class ObjectWithArgs(object):
  def __init__(self, param1, param2, database_service, param3=None, param4={}):
    self.db = database_service
    self.param1 = param1
    self.param2 = param2
    self.param3 = param3
    self.param4 = param4

new_instance = service_repository(ObjectWithArgs, 'param1', 'param2', param4='param4')
assert new_instance.db == service_repository['database']
assert new_instance.param1 == 'param1'
assert new_instance.param2 == 'param2'
assert new_instance.param3 is None
assert new_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 exception ServiceWrongPriority : 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 exception ServiceMissing : you forgot to define (or mispelled) a service definition in your project entry points.

  • You are getting an exception WrongConfiguration : You are missing some configuration key for a service in your config file or you are missing a config file altogether.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

weedi-1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

weedi-1.0-py2.py3-none-any.whl (10.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file weedi-1.0.tar.gz.

File metadata

  • Download URL: weedi-1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for weedi-1.0.tar.gz
Algorithm Hash digest
SHA256 5b2e7df91a6b46bbba72935572a11469ba53e108b8fb6f2dc64d9424d755b30f
MD5 79a4527f1ce1ee9cf4d67b5d1088ff5d
BLAKE2b-256 0e558ce9c349a059a623db45495340fafbac823466c8bc2895d10128db3a83c5

See more details on using hashes here.

File details

Details for the file weedi-1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for weedi-1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c2e0bb7afd51755b4e426952886e0296f013eb31387e98ecffc1b0cec7bc3ea5
MD5 a730c24b389cc35306ae3931b97d8759
BLAKE2b-256 227882104779e8b737a8f53fe46481d7f6599d8bb3304d107fc1d3e0cf5804af

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page