Skip to content

filters

trestle.core.jinja.filters ¤

Trestle utilities to customize jinja filters.

Attributes¤

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

Classes¤

JinjaSSPFilters ¤

Bases: TrestleJinjaExtension

Collection of useful OSCAL-specific filters.

Source code in trestle/core/jinja/filters.py
64
65
66
67
68
69
70
71
72
73
74
75
76
class JinjaSSPFilters(TrestleJinjaExtension):
    """Collection of useful OSCAL-specific filters."""

    def __init__(self, environment: Environment) -> None:
        """Initialize class and add filters."""
        super(JinjaSSPFilters, self).__init__(environment)

        environment.filters['as_list'] = as_list
        environment.filters['get_default'] = get_default
        environment.filters['first_or_none'] = first_or_none
        environment.filters['get_party'] = get_party
        environment.filters['parties_for_role'] = parties_for_role
        environment.filters['diagram_href'] = diagram_href
Functions¤
__init__(environment) ¤

Initialize class and add filters.

Source code in trestle/core/jinja/filters.py
67
68
69
70
71
72
73
74
75
76
def __init__(self, environment: Environment) -> None:
    """Initialize class and add filters."""
    super(JinjaSSPFilters, self).__init__(environment)

    environment.filters['as_list'] = as_list
    environment.filters['get_default'] = get_default
    environment.filters['first_or_none'] = first_or_none
    environment.filters['get_party'] = get_party
    environment.filters['parties_for_role'] = parties_for_role
    environment.filters['diagram_href'] = diagram_href

Functions¤

diagram_href(diagram) ¤

Retrieve the diagrams's link href.

Source code in trestle/core/jinja/filters.py
56
57
58
59
60
61
def diagram_href(diagram: Optional[Diagram]) -> str:
    """Retrieve the diagrams's link href."""
    if diagram:
        return next((link.href for link in as_list(diagram.links) if link.rel == 'diagram'), '')
    else:
        return ''

first_or_none(value) ¤

Retrieve the first array entry, or None for lists that are None or empty.

Source code in trestle/core/jinja/filters.py
30
31
32
def first_or_none(value: Optional[List[Any]]) -> Optional[Any]:
    """Retrieve the first array entry, or None for lists that are None or empty."""
    return next(iter(as_list(value)), None)

get_party(uuid, ssp) ¤

Get the metadata.parties entry for this UUID.

Source code in trestle/core/jinja/filters.py
35
36
37
def get_party(uuid: str, ssp: SystemSecurityPlan) -> Optional[Party]:
    """Get the metadata.parties entry for this UUID."""
    return next((x for x in as_list(ssp.metadata.parties) if x.uuid == uuid), None)

parties_for_role(responsible_parties, role_id, ssp) ¤

Get a list of parties from a list of responsible_parties and a given role_id.

Source code in trestle/core/jinja/filters.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def parties_for_role(responsible_parties: List[ResponsibleParty], role_id: str,
                     ssp: SystemSecurityPlan) -> Iterator[Party]:
    """Get a list of parties from a list of responsible_parties and a given role_id."""
    logger.debug(f'Finding parties for role: {role_id}')
    for responsible_party in as_list(responsible_parties):
        if responsible_party.role_id == role_id:
            logger.debug(
                f'Found responsible party for role_id: {role_id} with {len(responsible_party.party_uuids)} parties'
            )
            for uuid in responsible_party.party_uuids:
                logger.debug(f'Looking for parties with uuid: {uuid}')
                party = get_party(uuid, ssp)
                if party:
                    yield party

handler: python