Skip to content

transformer_factory

trestle.transforms.transformer_factory ¤

Define the TransformerFactory and corresponding transformer classes it creates.

Classes¤

FromOscalTransformer (TransformerBase) ¤

Abstract interface for transformers from OSCAL.

Source code in trestle/transforms/transformer_factory.py
class FromOscalTransformer(TransformerBase):
    """Abstract interface for transformers from OSCAL."""

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

Transform the from OSCAL.

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

ResultsTransformer (TransformerBase) ¤

Abstract interface for transformers that specifically return Results.

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

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

Transform the blob into Results.

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

ToOscalTransformer (TransformerBase) ¤

Abstract interface for transformers to OSCAL.

Source code in trestle/transforms/transformer_factory.py
class ToOscalTransformer(TransformerBase):
    """Abstract interface for transformers to OSCAL."""

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

Transform the to OSCAL.

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

TransformerBase (ABC) ¤

Abstract base interface for all transformers.

Source code in trestle/transforms/transformer_factory.py
class TransformerBase(ABC):
    """Abstract base interface for all transformers."""

    # the current time for consistent timestamping
    _timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc).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."""
Methods¤
get_timestamp() staticmethod ¤

Get the default timestamp value.

Source code in trestle/transforms/transformer_factory.py
@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
@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(self, blob) ¤

Transform the blob into a general OscalBaseModel.

Source code in trestle/transforms/transformer_factory.py
@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
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}')
Methods¤
__init__(self) special ¤

Initialize the transformers dictionary as empty.

Source code in trestle/transforms/transformer_factory.py
def __init__(self) -> None:
    """Initialize the transformers dictionary as empty."""
    self._transformers: Dict[str, Type[TransformerBase]] = {}
get(self, 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.

Exceptions:

Type Description
TrestleError

if the name does not exist in the registry.

Source code in trestle/transforms/transformer_factory.py
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(self, 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
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