Skip to content

trestle.common.str_utils

trestle.common.str_utils ¤

Trestle String Utils.

Classes¤

AliasMode ¤

Bases: Enum


              flowchart TD
              trestle.common.str_utils.AliasMode[AliasMode]

              

              click trestle.common.str_utils.AliasMode href "" "trestle.common.str_utils.AliasMode"
            

Allowed formats for classname alias.

Currently there are only two. If others are added, check they get handled properly in the code.

Source code in trestle/common/str_utils.py
24
25
26
27
28
29
30
31
32
class AliasMode(enum.Enum):
    """
    Allowed formats for classname alias.

    Currently there are only two.  If others are added, check they get handled properly in the code.
    """

    JSON = 1
    FIELD = 2
Attributes¤
FIELD = 2 class-attribute instance-attribute ¤
JSON = 1 class-attribute instance-attribute ¤

Functions¤

alias_to_classname(alias, mode) ¤

Return class name based dashed or snake alias.

This is applicable creating dynamic wrapper model for a list or dict field.

Source code in trestle/common/str_utils.py
 96
 97
 98
 99
100
101
102
103
104
def alias_to_classname(alias: str, mode: AliasMode) -> str:
    """
    Return class name based dashed or snake alias.

    This is applicable creating dynamic wrapper model for a list or dict field.
    """
    if mode == AliasMode.JSON:
        return _snake_to_upper_camel(alias.replace('-', '_'))
    return _snake_to_upper_camel(alias)

as_bool(string_or_none) ¤

Convert string to boolean.

Source code in trestle/common/str_utils.py
128
129
130
131
132
133
134
135
136
137
def as_bool(string_or_none: Optional[str]) -> bool:
    """Convert string to boolean."""
    if string_or_none:
        if string_or_none.lower() in ['false']:
            rval = False
        else:
            rval = True
    else:
        rval = False
    return rval

as_string(string_or_none) ¤

Convert string or None to itself or empty string.

Source code in trestle/common/str_utils.py
123
124
125
def as_string(string_or_none: Optional[str]) -> str:
    """Convert string or None to itself or empty string."""
    return string_or_none if string_or_none else ''

classname_to_alias(classname, mode) ¤

Return oscal key name or field element name based on class name.

This is applicable when asking for a singular element.

Source code in trestle/common/str_utils.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def classname_to_alias(classname: str, mode: AliasMode) -> str:
    """
    Return oscal key name or field element name based on class name.

    This is applicable when asking for a singular element.
    """
    suffix = classname.split('.')[-1]

    # the alias mode is either json or field - yaml doesn't apply here
    if mode == AliasMode.JSON:
        # things like class_ should just be class
        if suffix[-1] == '_':
            suffix = suffix[:-1]
        return _camel_to_dash(suffix).rstrip(string.digits)
    # else alias mode is field
    return _camel_to_snake(suffix).rstrip(string.digits)

dash_to_underscore(name) ¤

Convert dash to underscore.

Source code in trestle/common/str_utils.py
112
113
114
def dash_to_underscore(name: str) -> str:
    """Convert dash to underscore."""
    return name.replace('-', '_')

spaces_and_caps_to_lower_single_spaces(spaced_str) ¤

Convert caps and duplicate spaces to lower with single spaces.

Source code in trestle/common/str_utils.py
72
73
74
75
def spaces_and_caps_to_lower_single_spaces(spaced_str: str) -> str:
    """Convert caps and duplicate spaces to lower with single spaces."""
    single_space = ' '.join(spaced_str.strip().split())
    return single_space.lower()

spaces_and_caps_to_snake(spaced_str) ¤

Convert caps and spaces to snake.

Source code in trestle/common/str_utils.py
66
67
68
69
def spaces_and_caps_to_snake(spaced_str: str) -> str:
    """Convert caps and spaces to snake."""
    underscored = '_'.join(spaced_str.strip().split())
    return underscored.lower()

string_from_root(item_with_root) ¤

Convert root to string if present.

Source code in trestle/common/str_utils.py
140
141
142
def string_from_root(item_with_root: Optional[Any]) -> str:
    """Convert root to string if present."""
    return as_string(item_with_root.__root__) if item_with_root else ''

strip_lower_equals(str_a, str_b) ¤

Safe test of lower string equality allowing Nones.

If either argument is None the result is False because the intent is to report if they are equal as actual strings.

Source code in trestle/common/str_utils.py
145
146
147
148
149
150
151
152
153
def strip_lower_equals(str_a: Optional[str], str_b: Optional[str]) -> bool:
    """
    Safe test of lower string equality allowing Nones.

    If either argument is None the result is False because the intent is to report if they are equal as actual strings.
    """
    if str_a is None or str_b is None:
        return False
    return str_a.strip().lower() == str_b.strip().lower()

to_ncname(name) ¤

Convert to NCName.

Source code in trestle/common/str_utils.py
156
157
158
159
160
161
def to_ncname(name: str) -> str:
    """Convert to NCName."""
    rval = name
    if name:
        rval = re.sub(r'[^A-Za-z0-9_.-]', '_', name)
    return rval

underscore_to_dash(name) ¤

Convert underscore to dash and drop final dash if present.

Source code in trestle/common/str_utils.py
117
118
119
120
def underscore_to_dash(name: str) -> str:
    """Convert underscore to dash and drop final dash if present."""
    converted = name.replace('_', '-')
    return converted if converted[-1] != '-' else converted[:-1]

handler: python