Pass kwargs to new instances when solving dependencies
This commit is contained in:
parent
07079cbe73
commit
9e717a5f5d
2 changed files with 20 additions and 7 deletions
|
@ -19,7 +19,9 @@ from tracetools_analysis.processor import DependencySolver
|
||||||
|
|
||||||
|
|
||||||
class DepEmtpy(Dependant):
|
class DepEmtpy(Dependant):
|
||||||
pass
|
|
||||||
|
def __init__(self, **kwargs) -> None:
|
||||||
|
self.myparam = kwargs.get('myparam', None)
|
||||||
|
|
||||||
|
|
||||||
class DepOne(Dependant):
|
class DepOne(Dependant):
|
||||||
|
@ -102,6 +104,14 @@ class TestDependencySolver(unittest.TestCase):
|
||||||
self.assertIsInstance(solution[2], DepOne2)
|
self.assertIsInstance(solution[2], DepOne2)
|
||||||
self.assertIs(solution[3], deptwo_instance)
|
self.assertIs(solution[3], deptwo_instance)
|
||||||
|
|
||||||
|
def test_kwargs(self) -> None:
|
||||||
|
depone_instance = DepOne()
|
||||||
|
|
||||||
|
# Pass parameter and check that the new instance has it
|
||||||
|
solution = DependencySolver([depone_instance], myparam='myvalue').solve()
|
||||||
|
self.assertEqual(len(solution), 2, 'solution length invalid')
|
||||||
|
self.assertEqual(solution[0].myparam, 'myvalue', 'parameter not passed on')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
"""Base processor module."""
|
"""Base processor module."""
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
from typing import Any
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import List
|
from typing import List
|
||||||
|
@ -156,13 +157,16 @@ class DependencySolver():
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
initial_dependants: List[Dependant],
|
initial_dependants: List[Dependant],
|
||||||
|
**kwargs,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Constructor.
|
Constructor.
|
||||||
|
|
||||||
:param initial_dependants: the initial dependant instances, in order
|
:param initial_dependants: the initial dependant instances, in order
|
||||||
|
:param kwargs: the parameters to pass on to new instances
|
||||||
"""
|
"""
|
||||||
self._initial_deps = initial_dependants
|
self._initial_deps = initial_dependants
|
||||||
|
self._kwargs = kwargs
|
||||||
|
|
||||||
def solve(self) -> List[Dependant]:
|
def solve(self) -> List[Dependant]:
|
||||||
"""
|
"""
|
||||||
|
@ -191,7 +195,7 @@ class DependencySolver():
|
||||||
) -> None:
|
) -> None:
|
||||||
if type(dep_instance) not in visited:
|
if type(dep_instance) not in visited:
|
||||||
for dependency_type in type(dep_instance).dependencies():
|
for dependency_type in type(dep_instance).dependencies():
|
||||||
DependencySolver.__solve_type(
|
self.__solve_type(
|
||||||
dependency_type,
|
dependency_type,
|
||||||
visited,
|
visited,
|
||||||
initial_map,
|
initial_map,
|
||||||
|
@ -200,8 +204,8 @@ class DependencySolver():
|
||||||
solution.append(dep_instance)
|
solution.append(dep_instance)
|
||||||
visited.add(type(dep_instance))
|
visited.add(type(dep_instance))
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def __solve_type(
|
def __solve_type(
|
||||||
|
self,
|
||||||
dep_type: Type[Dependant],
|
dep_type: Type[Dependant],
|
||||||
visited: Set[Type[Dependant]],
|
visited: Set[Type[Dependant]],
|
||||||
initial_map: Dict[Type[Dependant], Dependant],
|
initial_map: Dict[Type[Dependant], Dependant],
|
||||||
|
@ -209,7 +213,7 @@ class DependencySolver():
|
||||||
) -> None:
|
) -> None:
|
||||||
if dep_type not in visited:
|
if dep_type not in visited:
|
||||||
for dependency_type in dep_type.dependencies():
|
for dependency_type in dep_type.dependencies():
|
||||||
DependencySolver.__solve_type(
|
self.__solve_type(
|
||||||
dependency_type,
|
dependency_type,
|
||||||
visited,
|
visited,
|
||||||
initial_map,
|
initial_map,
|
||||||
|
@ -220,7 +224,7 @@ class DependencySolver():
|
||||||
if dep_type in initial_map:
|
if dep_type in initial_map:
|
||||||
new_instance = initial_map.get(dep_type)
|
new_instance = initial_map.get(dep_type)
|
||||||
else:
|
else:
|
||||||
new_instance = dep_type()
|
new_instance = dep_type(**self._kwargs)
|
||||||
solution.append(new_instance)
|
solution.append(new_instance)
|
||||||
visited.add(dep_type)
|
visited.add(dep_type)
|
||||||
|
|
||||||
|
@ -260,8 +264,7 @@ class Processor():
|
||||||
|
|
||||||
:param handlers: the list of primary `EventHandler`s
|
:param handlers: the list of primary `EventHandler`s
|
||||||
"""
|
"""
|
||||||
# TODO pass on **kwargs
|
return DependencySolver(handlers, **kwargs).solve()
|
||||||
return DependencySolver(handlers).solve()
|
|
||||||
|
|
||||||
def _get_handler_maps(self) -> HandlerMultimap:
|
def _get_handler_maps(self) -> HandlerMultimap:
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue