Skip to content

export_writer

trestle.core.crm.export_writer ¤

Provided interface to write provided and responsibility exports statements to Markdown.

Attributes¤

logger = logging.getLogger(__name__) module-attribute ¤

Classes¤

ExportWriter ¤

By-Component Assembly Exports writer.

Export writer handles all operations related to writing provided and responsibility exported statements to Markdown.

Source code in trestle/core/crm/export_writer.py
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 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
112
113
114
115
116
117
class ExportWriter:
    """
    By-Component Assembly Exports writer.

    Export writer handles all operations related to writing provided and responsibility exported statements
    to Markdown.
    """

    def __init__(self, root_path: pathlib.Path, ssp: ossp.SystemSecurityPlan, leveraged_ssp_href: str):
        """
        Initialize export writer.

        Arguments:
            root_path: A root path object where all markdown files and directories should be written.
            ssp: A system security plan with exports
        """
        self._ssp: ossp.SystemSecurityPlan = ssp
        self._root_path: pathlib.Path = root_path
        self._leveraged_ssp_href: str = leveraged_ssp_href

        # Find all the components and create paths for name
        self._paths_by_comp: Dict[str, pathlib.Path] = {}
        for component in as_list(self._ssp.system_implementation.components):
            self._paths_by_comp[component.uuid] = self._root_path.joinpath(component.title)

    def write_exports_as_markdown(self) -> None:
        """Write export statement for leveraged SSP as the inheritance Markdown view."""
        # Process all information under exports in control implementation
        for implemented_requirement in as_list(self._ssp.control_implementation.implemented_requirements):
            for by_comp in as_list(implemented_requirement.by_components):
                self._process_by_component(by_comp, implemented_requirement.control_id)

            for stm in as_list(implemented_requirement.statements):
                statement_id = getattr(stm, 'statement_id', f'{implemented_requirement.control_id}_smt')
                for by_comp in as_list(stm.by_components):
                    self._process_by_component(by_comp, statement_id)

    def _process_by_component(self, by_comp: ossp.ByComponent, control_id: str) -> None:
        """Complete the Markdown writing operations for each by-component assembly."""
        if by_comp.component_uuid not in self._paths_by_comp:
            raise TrestleError(f'Component id {by_comp.component_uuid} is not in the system implementation')

        comp_path: pathlib.Path = self._paths_by_comp[by_comp.component_uuid]

        export_interface: ByComponentInterface = ByComponentInterface(by_comp=by_comp)
        leveraged_statements: Dict[str, LeveragedStatements] = self._statement_types_from_exports(export_interface)

        # Only create the directory if leveraged statements exist. If not return.
        if not leveraged_statements:
            logger.debug(f'Component {by_comp.component_uuid} has no exports for control {control_id}')
            return

        control_path: pathlib.Path = comp_path.joinpath(control_id)
        control_path.mkdir(exist_ok=True, parents=True)

        for statement_file_path, leveraged_stm in leveraged_statements.items():
            statement_path: pathlib.Path = control_path.joinpath(f'{statement_file_path}{const.MARKDOWN_FILE_EXT}')
            leveraged_stm.write_statement_md(statement_path)

    def _statement_types_from_exports(self, export_interface: ByComponentInterface) -> Dict[str, LeveragedStatements]:
        """Process all exports and return a file basename and LeveragedStatement object for each."""
        all_statements: Dict[str, LeveragedStatements] = {}

        for responsibility in export_interface.get_isolated_responsibilities():
            all_statements[responsibility.uuid] = StatementResponsibility(
                responsibility.uuid, responsibility.description, self._leveraged_ssp_href
            )

        for provided in export_interface.get_isolated_provided():
            all_statements[provided.uuid
                           ] = StatementProvided(provided.uuid, provided.description, self._leveraged_ssp_href)

        for responsibility, provided in export_interface.get_export_sets():
            path = f'{provided.uuid}_{responsibility.uuid}'
            all_statements[path] = StatementTree(
                provided.uuid,
                provided.description,
                responsibility.uuid,
                responsibility.description,
                self._leveraged_ssp_href
            )

        return all_statements
Functions¤
__init__(root_path, ssp, leveraged_ssp_href) ¤

Initialize export writer.

Parameters:

Name Type Description Default
root_path Path

A root path object where all markdown files and directories should be written.

required
ssp SystemSecurityPlan

A system security plan with exports

required
Source code in trestle/core/crm/export_writer.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def __init__(self, root_path: pathlib.Path, ssp: ossp.SystemSecurityPlan, leveraged_ssp_href: str):
    """
    Initialize export writer.

    Arguments:
        root_path: A root path object where all markdown files and directories should be written.
        ssp: A system security plan with exports
    """
    self._ssp: ossp.SystemSecurityPlan = ssp
    self._root_path: pathlib.Path = root_path
    self._leveraged_ssp_href: str = leveraged_ssp_href

    # Find all the components and create paths for name
    self._paths_by_comp: Dict[str, pathlib.Path] = {}
    for component in as_list(self._ssp.system_implementation.components):
        self._paths_by_comp[component.uuid] = self._root_path.joinpath(component.title)
write_exports_as_markdown() ¤

Write export statement for leveraged SSP as the inheritance Markdown view.

Source code in trestle/core/crm/export_writer.py
60
61
62
63
64
65
66
67
68
69
70
def write_exports_as_markdown(self) -> None:
    """Write export statement for leveraged SSP as the inheritance Markdown view."""
    # Process all information under exports in control implementation
    for implemented_requirement in as_list(self._ssp.control_implementation.implemented_requirements):
        for by_comp in as_list(implemented_requirement.by_components):
            self._process_by_component(by_comp, implemented_requirement.control_id)

        for stm in as_list(implemented_requirement.statements):
            statement_id = getattr(stm, 'statement_id', f'{implemented_requirement.control_id}_smt')
            for by_comp in as_list(stm.by_components):
                self._process_by_component(by_comp, statement_id)

Functions¤

handler: python