Skip to content

Commit

Permalink
Merge pull request #410 from SpiNNakerManchester/no_inject
Browse files Browse the repository at this point in the history
Stop using @Inject
  • Loading branch information
Christian-B committed Nov 9, 2021
2 parents 1a50689 + dbfa746 commit 4ab4d9e
Showing 1 changed file with 0 additions and 99 deletions.
99 changes: 0 additions & 99 deletions pacman/executor/injection_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,77 +26,6 @@ class InjectionException(Exception):
"""


def supports_injection(injectable_class):
""" A :py:obj:`decorator` that indicates that the class has methods on
which objects can be injected.
"""
orig_init = injectable_class.__init__

def new_init(self, *args, **kwargs):
# pylint: disable=protected-access
orig_init(self, *args, **kwargs)
for method in injectable_class.__dict__.values():
if hasattr(method, "_type_to_inject"):
_methods[injectable_class][method._type_to_inject] = method
_instances.append(self)

injectable_class.__init__ = new_init
return injectable_class


def inject(type_to_inject):
""" A :py:obj:`decorator` that marks a method as something to be called to
inject an object of the given type. The type is just a name for the
type, and should match up at some point with some generated data.
:param str type_to_inject: The type to be injected using this method
"""
def wrap(method):
# pylint: disable=protected-access

@wraps(method)
def wrapper(obj, arg):
method(obj, arg)
if arg is not None:
wrapper._called = True

wrapper._type_to_inject = type_to_inject
wrapper._called = False
return wrapper
return wrap


def requires_injection(types_required):
""" A :py:obj:`decorator` that indicates that injection of the given types
is required before this method is called; an Exception is raised if
the types have not been injected.
:param list(str) types_required:
A list of types that must have been injected
"""
def wrap(wrapped_method):

@wraps(wrapped_method)
def wrapper(obj, *args, **kwargs):
methods = dict()
for cls in obj.__class__.__mro__:
cls_methods = _methods.get(cls, {})
methods.update(cls_methods)
for object_type in types_required:
method = methods.get(object_type, None)
if method is None:
raise InjectionException(
"No injector for type {} for object {}"
.format(object_type, obj))
if not method._called:
raise InjectionException(
"Type {} has not been injected for object {}"
.format(object_type, obj))
return wrapped_method(obj, *args, **kwargs)
return wrapper
return wrap


def inject_items(types):
""" A :py:obj:`decorator` that ndicates values that need to be injected
into the method
Expand Down Expand Up @@ -214,31 +143,3 @@ def __exit__(self, a, b, c):
global _injectables
_injectables = self._old
return False


def do_injection(objects_to_inject, objects_to_inject_into=None):
""" Perform the actual injection of objects.
:param objects_to_inject:
The objects to be injected as a dict of type name -> object of type
:type objects_to_inject: dict(str, ...)
:param objects_to_inject_into:
The objects whose classes :py:func:`support_injection`, or None to use
all instances that have been created
:type objects_to_inject_into: list or None
"""
if objects_to_inject is None:
return
injectees = objects_to_inject_into
if objects_to_inject_into is None:
injectees = _instances
for obj in injectees:
methods = dict()
for cls in obj.__class__.__mro__:
cls_methods = _methods.get(cls, {})
methods.update(cls_methods)
if methods is not None:
for object_type, object_to_inject in objects_to_inject.items():
method = methods.get(object_type, None)
if method is not None:
method(obj, object_to_inject)

0 comments on commit 4ab4d9e

Please sign in to comment.