|
| 1 | +import inspect |
| 2 | + |
| 3 | +from ._common import ( |
| 4 | + ClassType, |
| 5 | + InstantiatorCallable, |
| 6 | + InstantiatorsDictType, |
| 7 | + applied_instantiation_links, |
| 8 | + class_instantiators, |
| 9 | + is_subclass, |
| 10 | +) |
| 11 | + |
| 12 | +__all__ = ["add_instantiator"] |
| 13 | + |
| 14 | +_global_class_instantiators: InstantiatorsDictType = {} |
| 15 | + |
| 16 | + |
| 17 | +def add_instantiator( |
| 18 | + instantiator: InstantiatorCallable, |
| 19 | + class_type: type[ClassType], |
| 20 | + subclasses: bool = True, |
| 21 | + prepend: bool = False, |
| 22 | +) -> None: |
| 23 | + """Adds a custom instantiator for a class type. Used by ``ArgumentParser.instantiate``. |
| 24 | +
|
| 25 | + Instantiator functions are expected to have as signature ``(class_type: |
| 26 | + Type[ClassType], *args, **kwargs) -> ClassType``. |
| 27 | +
|
| 28 | + For reference, the default instantiator is ``return class_type(*args, |
| 29 | + **kwargs)``. |
| 30 | +
|
| 31 | + In some use cases, the instantiator function might need access to values |
| 32 | + applied by instantiation links. For this, the instantiator function can |
| 33 | + have an additional keyword parameter ``applied_instantiation_links: |
| 34 | + dict``. This parameter will be populated with a dictionary having as |
| 35 | + keys the targets of the instantiation links and corresponding values |
| 36 | + that were applied. |
| 37 | +
|
| 38 | + Args: |
| 39 | + instantiator: Function that instantiates a class. |
| 40 | + class_type: The class type to instantiate. |
| 41 | + subclasses: Whether to instantiate subclasses of ``class_type``. |
| 42 | + prepend: Whether to prepend the instantiator to the existing instantiators. |
| 43 | + """ |
| 44 | + _register_instantiator( |
| 45 | + _global_class_instantiators, instantiator, class_type, subclasses=subclasses, prepend=prepend |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def _register_instantiator( |
| 50 | + registry: InstantiatorsDictType, |
| 51 | + instantiator: InstantiatorCallable, |
| 52 | + class_type: type[ClassType], |
| 53 | + subclasses: bool = True, |
| 54 | + prepend: bool = False, |
| 55 | +) -> None: |
| 56 | + """Registers an instantiator in the given registry dict (in-place).""" |
| 57 | + key = (class_type, subclasses) |
| 58 | + items = {k: v for k, v in registry.items() if k != key} |
| 59 | + if prepend: |
| 60 | + registry.clear() |
| 61 | + registry.update({key: instantiator, **items}) |
| 62 | + else: |
| 63 | + items[key] = instantiator |
| 64 | + registry.clear() |
| 65 | + registry.update(items) |
| 66 | + |
| 67 | + |
| 68 | +def _get_global_class_instantiators() -> InstantiatorsDictType: |
| 69 | + """Returns the global instantiators registry.""" |
| 70 | + return _global_class_instantiators |
| 71 | + |
| 72 | + |
| 73 | +def default_class_instantiator(class_type: type[ClassType], *args, **kwargs) -> ClassType: |
| 74 | + return class_type(*args, **kwargs) |
| 75 | + |
| 76 | + |
| 77 | +class ClassInstantiator: |
| 78 | + def __init__(self, instantiators: InstantiatorsDictType) -> None: |
| 79 | + self.instantiators = instantiators |
| 80 | + |
| 81 | + def __call__(self, class_type: type[ClassType], *args, **kwargs) -> ClassType: |
| 82 | + for (cls, subclasses), instantiator in self.instantiators.items(): |
| 83 | + if class_type is cls or (subclasses and is_subclass(class_type, cls)): |
| 84 | + param_names = set(inspect.signature(instantiator).parameters) |
| 85 | + if "applied_instantiation_links" in param_names: |
| 86 | + applied_links = applied_instantiation_links.get() or set() |
| 87 | + kwargs["applied_instantiation_links"] = { |
| 88 | + action.target[0]: action.applied_value for action in applied_links |
| 89 | + } |
| 90 | + return instantiator(class_type, *args, **kwargs) |
| 91 | + return default_class_instantiator(class_type, *args, **kwargs) |
| 92 | + |
| 93 | + |
| 94 | +def get_class_instantiator() -> InstantiatorCallable: |
| 95 | + instantiators = class_instantiators.get() |
| 96 | + if not instantiators: |
| 97 | + return default_class_instantiator |
| 98 | + return ClassInstantiator(instantiators) |
| 99 | + |
| 100 | + |
| 101 | +def get_class_instantiators(parser) -> InstantiatorsDictType: |
| 102 | + """Gathers all instantiators applicable to the given parser.""" |
| 103 | + instantiators = parser._get_parser_instantiators() |
| 104 | + context_instantiators = class_instantiators.get() |
| 105 | + if context_instantiators: |
| 106 | + instantiators = instantiators.copy() |
| 107 | + instantiators.update({k: v for k, v in context_instantiators.items() if k not in instantiators}) |
| 108 | + global_instantiators = _get_global_class_instantiators() |
| 109 | + if global_instantiators: |
| 110 | + instantiators = instantiators.copy() |
| 111 | + instantiators.update({k: v for k, v in global_instantiators.items() if k not in instantiators}) |
| 112 | + return instantiators |
0 commit comments