Component (autotransform.util.component)¶
Utilities for handling components, such as importing custom components.
- class autotransform.util.component.ComponentFactory(components: Dict[str, ComponentImport], component_type: Type[T], component_file: str)¶
Bases:
Generic
[T
],ABC
A factory class that produces instances of components
- _components¶
A mapping from a component name to the information needed to import and use that component.
- Type:
Dict[str, ComponentImport]
- _custom_components_file¶
The name of the file containing custom component JSON.
- Type:
str
- _type¶
The type this factory will produce.
- Type:
Type[T]
- from_console(name: str, previous_value: ~typing.Optional[~autotransform.util.component.T] = <object object>, default_value: ~typing.Optional[~autotransform.util.component.T] = <object object>, simple: bool = False, allow_none: bool = True) Optional[T] ¶
Gets a component from console inputs.
- Parameters:
name (str) – The name of the component being entered.
previous_value (Optional[T], optional) – A previously used value for the component. Defaults to UNSET_VALUE.
default_value (Optional[T], optional) – A default value for the component. Defaults to UNSET_VALUE.
simple (bool, optional) – Whether to choose simple options. Defaults to False.
allow_none (bool, optional) – Whether None is a valid value. Defaults to True.
- Returns:
The component or None.
- Return type:
Optional[T]
- get_class(component_name: str) Type[T] ¶
Gets the class for a component with the specified type, usually an enum value.
- Parameters:
component_name (str) – The type of the component, usually an enum value. If a custom component is used, the type should start with custom/.
- Raises:
ValueError – If the component could not be found.
- Returns:
The class for the component.
- Return type:
Type[T]
- get_components() Dict[str, ComponentImport] ¶
A simple getter for the factory’s map of components.
- Returns:
The components in the component library.
- Return type:
Dict[str, ComponentImport]
- get_custom_components(strict: bool = False) Dict[str, ComponentImport] ¶
Builds the custom components dictionary if it’s not set, returns it otherwise.
- Parameters:
strict (bool, optional) – Whether to strictly enforce no issues arise during import. Defaults to False
- Returns:
The custom component import info.
- Return type:
Dict[str, ComponentImport]
- static get_custom_components_dict(component_file_name: str, strict: bool = False) Dict[str, ComponentImport] ¶
Builds a custom component dictionary for importing.
- Parameters:
component_file_name (str) – The name of the file that contains the custom components.
strict (bool, optional) – Whether to raise an error if JSON is malformed. Defaults to False.
- Returns:
- The custom component import info. All custom components
start with custom/ for their name.
- Return type:
Dict[str, ComponentImport]
- static get_custom_components_path(component_file_name: str) str ¶
Gets the path for the custom component file.
- Parameters:
component_file_name (str) – The name of the file that contains the custom components.
- Returns:
The path where the custom component JSON is located.
- Return type:
str
- get_instance(data: Dict[str, Any]) T ¶
Simple method to get an instance from a bundle.
- Parameters:
data (Dict[str, Any]) – The bundled component.
- Returns:
An instance of the associated component.
- Return type:
T
- class autotransform.util.component.ComponentImport(*, class_name: str, module: str)¶
Bases:
ComponentModel
The information required to import and return a component.
- class_name¶
The name of the class of the component.
- Type:
str
- module¶
The fully qualified module where the class can be imported.
- Type:
str
- class_name: str¶
- module: str¶
- class autotransform.util.component.ComponentModel¶
Bases:
BaseModel
A base class for AutoTransform components that need to handle bundling/unbundling for JSON.
- bundle() Dict[str, Any] ¶
Generates a JSON encodable bundle. If a component is not JSON encodable this method should be overridden to provide an encodable version.
- Returns:
The encodable bundle.
- Return type:
Dict[str, Any]
- classmethod from_data(data: Dict[str, Any]) TComponent ¶
Produces an instance of the component from decoded data. Override if the component had to be modified to encode.
- Parameters:
data (Mapping[str, Any]) – The JSON decoded data.
- Returns:
An instance of the component.
- Return type:
TComponent
- class autotransform.util.component.NamedComponent¶
Bases:
ComponentModel
A base class for AutoTransform components that are produced from a factory using a name to differentiate component classes.
- name¶
The name of the Component.
- Type:
ClassVar[Enum]
- bundle() Dict[str, Any] ¶
Generates a JSON encodable bundle. If a component is not JSON encodable this method should be overridden to provide an encodable version.
- Returns:
The encodable bundle.
- Return type:
Dict[str, Any]
- name: ClassVar[Enum]¶