Skip to content

transformer_factory

trestle.transforms.transformer_factory ¤

Define the TransformerFactory and corresponding transformer classes it creates.

Classes¤

FromOscalTransformer ¤

Bases: TransformerBase

Abstract interface for transformers from OSCAL.

Source code in trestle/transforms/transformer_factory.py
49
50
51
52
53
54
class FromOscalTransformer(TransformerBase):
    """Abstract interface for transformers from OSCAL."""

    @abstractmethod
    def transform(self, obj: OscalBaseModel) -> str:
        """Transform the from OSCAL."""
Functions¤
transform(obj) abstractmethod ¤

Transform the from OSCAL.

Source code in trestle/transforms/transformer_factory.py
52
53
54
@abstractmethod
def transform(self, obj: OscalBaseModel) -> str:
    """Transform the from OSCAL."""

ResultsTransformer ¤

Bases: TransformerBase

Abstract interface for transformers that specifically return Results.

Source code in trestle/transforms/transformer_factory.py
65
66
67
68
69
70
class ResultsTransformer(TransformerBase):
    """Abstract interface for transformers that specifically return Results."""

    @abstractmethod
    def transform(self, blob: str) -> Results:
        """Transform the blob into Results."""
Functions¤
transform(blob) abstractmethod ¤

Transform the blob into Results.

Source code in trestle/transforms/transformer_factory.py
68
69
70
@abstractmethod
def transform(self, blob: str) -> Results:
    """Transform the blob into Results."""

ToOscalTransformer ¤

Bases: TransformerBase

Abstract interface for transformers to OSCAL.

Source code in trestle/transforms/transformer_factory.py
57
58
59
60
61
62
class ToOscalTransformer(TransformerBase):
    """Abstract interface for transformers to OSCAL."""

    @abstractmethod
    def transform(self, obj: str) -> OscalBaseModel:
        """Transform the to OSCAL."""
Functions¤
transform(obj) abstractmethod ¤

Transform the to OSCAL.

Source code in trestle/transforms/transformer_factory.py
60
61
62
@abstractmethod
def transform(self, obj: str) -> OscalBaseModel:
    """Transform the to OSCAL."""

TransformerBase ¤

Bases: ABC

Abstract base interface for all transformers.

Source code in trestle/transforms/transformer_factory.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class TransformerBase(ABC):
    """Abstract base interface for all transformers."""

    # the current time for consistent timestamping
    _timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat()

    @staticmethod
    def set_timestamp(value: str) -> None:
        """Set the default timestamp value."""
        datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S%z')
        TransformerBase._timestamp = value

    @staticmethod
    def get_timestamp() -> str:
        """Get the default timestamp value."""
        return TransformerBase._timestamp

    @abstractmethod
    def transform(self, blob: Any) -> Any:
        """Transform the blob into a general OscalBaseModel."""
Functions¤
get_timestamp() staticmethod ¤

Get the default timestamp value.

Source code in trestle/transforms/transformer_factory.py
39
40
41
42
@staticmethod
def get_timestamp() -> str:
    """Get the default timestamp value."""
    return TransformerBase._timestamp
set_timestamp(value) staticmethod ¤

Set the default timestamp value.

Source code in trestle/transforms/transformer_factory.py
33
34
35
36
37
@staticmethod
def set_timestamp(value: str) -> None:
    """Set the default timestamp value."""
    datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S%z')
    TransformerBase._timestamp = value
transform(blob) abstractmethod ¤

Transform the blob into a general OscalBaseModel.

Source code in trestle/transforms/transformer_factory.py
44
45
46
@abstractmethod
def transform(self, blob: Any) -> Any:
    """Transform the blob into a general OscalBaseModel."""

TransformerFactory ¤

Perform registration and creation of transformers.

Source code in trestle/transforms/transformer_factory.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
class TransformerFactory:
    """Perform registration and creation of transformers."""

    def __init__(self) -> None:
        """Initialize the transformers dictionary as empty."""
        self._transformers: Dict[str, Type[TransformerBase]] = {}

    def register_transformer(self, name: str, transformer: Type[TransformerBase]) -> None:
        """
        Register the transformer.

        This registers transformers in the factory so they may be created by name.

        Args:
            name (str): The name of the transformer.
            transformer (TransformerBase): The transformer class to be registered.

        Returns:
            None
        """
        self._transformers[name] = transformer

    def get(self, name: str) -> TransformerBase:
        """
        Create an instance of the desired transformer based its name.

        Args:
            name (str): The name of the transformer.

        Returns:
            An instance of the desired transformer.

        Raises:
            TrestleError: if the name does not exist in the registry.
        """
        t = self._transformers.get(name)
        if t is not None:
            return t()
        raise TrestleError(f'Error getting non-registered transform {name}')
Functions¤
__init__() ¤

Initialize the transformers dictionary as empty.

Source code in trestle/transforms/transformer_factory.py
76
77
78
def __init__(self) -> None:
    """Initialize the transformers dictionary as empty."""
    self._transformers: Dict[str, Type[TransformerBase]] = {}
get(name) ¤

Create an instance of the desired transformer based its name.

Parameters:

Name Type Description Default
name str

The name of the transformer.

required

Returns:

Type Description
TransformerBase

An instance of the desired transformer.

Raises:

Type Description
TrestleError

if the name does not exist in the registry.

Source code in trestle/transforms/transformer_factory.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def get(self, name: str) -> TransformerBase:
    """
    Create an instance of the desired transformer based its name.

    Args:
        name (str): The name of the transformer.

    Returns:
        An instance of the desired transformer.

    Raises:
        TrestleError: if the name does not exist in the registry.
    """
    t = self._transformers.get(name)
    if t is not None:
        return t()
    raise TrestleError(f'Error getting non-registered transform {name}')
register_transformer(name, transformer) ¤

Register the transformer.

This registers transformers in the factory so they may be created by name.

Parameters:

Name Type Description Default
name str

The name of the transformer.

required
transformer TransformerBase

The transformer class to be registered.

required

Returns:

Type Description
None

None

Source code in trestle/transforms/transformer_factory.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def register_transformer(self, name: str, transformer: Type[TransformerBase]) -> None:
    """
    Register the transformer.

    This registers transformers in the factory so they may be created by name.

    Args:
        name (str): The name of the transformer.
        transformer (TransformerBase): The transformer class to be registered.

    Returns:
        None
    """
    self._transformers[name] = transformer

handler: python