1
|
|
|
"""Exposes the SubclassRegistry that allows to define a single registration point of one or more subclasses of a |
2
|
|
|
(common parent) class.""" |
3
|
|
|
|
4
|
|
|
from typing import TypeVar, Generic, Dict |
5
|
|
|
|
6
|
|
|
T = TypeVar('T') |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class SubclassRegistry(type, Generic[T]): |
10
|
|
|
subclasses: Dict[str, type] |
11
|
|
|
"""Subclass Registry |
12
|
|
|
|
13
|
|
|
A (parent) class using this class as metaclass gains the 'subclasses' class attribute as well as the 'create' and |
14
|
|
|
'register_as_subclass' class methods. |
15
|
|
|
|
16
|
|
|
The 'subclasses' attribute is a python dictionary having string identifiers as keys and subclasses of the (parent) |
17
|
|
|
class as values. |
18
|
|
|
|
19
|
|
|
The 'register_as_subclass' class method can be used as a decorator to indicate that a (child) class should belong in |
20
|
|
|
the parent's class registry. An input string argument will be used as the unique key to register the subclass. |
21
|
|
|
|
22
|
|
|
The 'create' class method can be invoked with a (string) key and suitable constructor arguments to later construct |
23
|
|
|
instances of the corresponding child class. |
24
|
|
|
|
25
|
|
|
Example: |
26
|
|
|
|
27
|
|
|
>>> from so_magic.utils import SubclassRegistry |
28
|
|
|
|
29
|
|
|
>>> class ParentClass(metaclass=SubclassRegistry): |
30
|
|
|
... pass |
31
|
|
|
|
32
|
|
|
>>> ParentClass.subclasses |
33
|
|
|
{} |
34
|
|
|
|
35
|
|
|
>>> @ParentClass.register_as_subclass('child') |
36
|
|
|
... class ChildClass(ParentClass): |
37
|
|
|
... def __init__(self, child_attribute): |
38
|
|
|
... self.attr = child_attribute |
39
|
|
|
|
40
|
|
|
>>> child_instance = ParentClass.create('child', 'attribute-value') |
41
|
|
|
>>> child_instance.attr |
42
|
|
|
'attribute-value' |
43
|
|
|
|
44
|
|
|
>>> type(child_instance).__name__ |
45
|
|
|
'ChildClass' |
46
|
|
|
|
47
|
|
|
>>> isinstance(child_instance, ChildClass) |
48
|
|
|
True |
49
|
|
|
|
50
|
|
|
>>> isinstance(child_instance, ParentClass) |
51
|
|
|
True |
52
|
|
|
|
53
|
|
|
>>> {k: v.__name__ for k, v in ParentClass.subclasses.items()} |
54
|
|
|
{'child': 'ChildClass'} |
55
|
|
|
""" |
56
|
|
|
def __init__(cls, *args): |
57
|
|
|
super().__init__(*args) |
58
|
|
|
cls.subclasses = {} |
59
|
|
|
|
60
|
|
|
def create(cls, subclass_identifier, *args, **kwargs) -> T: |
61
|
|
|
"""Create an instance of a registered subclass, given its unique identifier and runtime (constructor) arguments. |
62
|
|
|
|
63
|
|
|
Invokes the identified subclass constructor passing any supplied arguments. The user needs to know the arguments |
64
|
|
|
to supply depending on the resulting constructor signature. |
65
|
|
|
|
66
|
|
|
Args: |
67
|
|
|
subclass_identifier (str): the unique identifier under which to look for the corresponding subclass |
68
|
|
|
|
69
|
|
|
Raises: |
70
|
|
|
ValueError: In case the given identifier is unknown to the parent class |
71
|
|
|
InstantiationError: In case the runtime args and kwargs do not match the constructor signature |
72
|
|
|
|
73
|
|
|
Returns: |
74
|
|
|
object: the instance of the registered subclass |
75
|
|
|
""" |
76
|
|
|
if subclass_identifier not in cls.subclasses: |
77
|
|
|
raise ValueError(f'Bad "{str(cls.__name__)}" subclass request; requested subclass with identifier ' |
78
|
|
|
f'{str(subclass_identifier)}, but known identifiers are ' |
79
|
|
|
f'[{", ".join(str(subclass_id) for subclass_id in cls.subclasses.keys())}]') |
80
|
|
|
try: |
81
|
|
|
return cls.subclasses[subclass_identifier](*args, **kwargs) |
82
|
|
|
except Exception as any_error: |
83
|
|
|
raise InstantiationError('Error during instance object construction.' |
84
|
|
|
f' Failed to create instance of class {cls.subclasses[subclass_identifier]}' |
85
|
|
|
f' using args [{", ".join((str(_) for _ in args))}]' |
86
|
|
|
f' and kwargs [{", ".join(f"{k}={v}" for k, v in kwargs.items())}]') from any_error |
87
|
|
|
|
88
|
|
|
def register_as_subclass(cls, subclass_identifier): |
89
|
|
|
"""Register a class as subclass of the parent class. |
90
|
|
|
|
91
|
|
|
Adds the subclass' constructor in the registry (dict) under the given (str) identifier. Overrides the registry |
92
|
|
|
in case of "identifier collision". Can be used as a python decorator. |
93
|
|
|
|
94
|
|
|
Args: |
95
|
|
|
subclass_identifier (str): the user-defined identifier, under which to register the subclass |
96
|
|
|
""" |
97
|
|
|
def wrapper(subclass): |
98
|
|
|
"""Add the (sub) class provided to the parent class registry. |
99
|
|
|
|
100
|
|
|
Args: |
101
|
|
|
subclass ([type]): the (sub) class to register |
102
|
|
|
|
103
|
|
|
Returns: |
104
|
|
|
object: the (sub) class |
105
|
|
|
""" |
106
|
|
|
cls.subclasses[subclass_identifier] = subclass |
107
|
|
|
return subclass |
108
|
|
|
return wrapper |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
class InstantiationError(Exception): pass |
112
|
|
|
|