Skip to content

ListProvider with config from yaml #443

Open
@milokmet

Description

@milokmet

Hello Roman, I am playing a little bit with dependency_injector and I don't know, how could I configure in yaml and write into Container ListProvider next example:

Is it possible to rewrite this with dependency_injector?

from abc import ABC, abstractmethod
class Source(ABC):
    @abstractmethod
    def do_something(self):
        pass

class MultiSource(Source):
    _sources = []
    def __init__(self, sources=[]):
        self._sources = sources
    def add(self, source):
        if source not in self._sources:
            self._sources.append(source)
    def do_something(self):
        print('Starting to do something')
        for s in self._sources:
            s.do_something()

class OracleSource(Source):
    def __init__(self, dbparams):
        self.name = dbparams['dsn']
        # self._conn = cx_Oracle.connect(**dbparams)
        ...
    def do_something(self):
        # fetch from db and return
        print('fetching and returning from {}'.format(self.name))
        ...

class ListSource(Source):
    _values = []
    def __init__(self, values):
        self._values = values

    def do_something(self):
        print('fetching and returning from provided list')

src = MultiSource()
src.add(OracleSource({'dsn': 'db1'}))
src.add(OracleSource({'dsn': 'db2'}))
src.add(ListSource(['task1', 'task2']))

src.do_something()

And the yaml confg should like like this:

source:
  - type: OracleSource
    params:
      dsn: "db1"

  - type: ListSource
    tasks:
      - { name: task1 }
      - { name: task2 }

Will it be possible to detect by type of config? If config.source will be dict, it will configure the source by type directly, if it will be list, it will use MultiSource?

source:
  type: list
  tasks:
   - { name: task1 }
   - { name: task2 }

Is it possible to rewrite this into container with list provider a config?

Thanks

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions