Passed
Push — master ( ee1e78...515b92 )
by Konstantinos
01:14
created

artificial_artwork.utils.subclass_registry   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

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