openrig.naming package
Naming package for OpenRig.
This package provides a configurable system for building, parsing, and validating names according to a defined convention.
The main entry point is get_manager(), which returns a singleton
Manager instance pre-configured with the rules and normalizers
declared in naming/config.json.
- Public API:
get_manager() -> Manager: returns the singleton manager.Manager: the naming manager class.Normalizer: type alias for normalizer callables.
Example
>>> from openrig.naming import get_manager
>>> manager = get_manager()
>>> manager.build_name(descriptor="arm", side="l", usage="jnt")
'arm_l_jnt'
- class openrig.naming.Manager(tokens: Sequence[str] | None = None, separator: str | None = None, rules: dict[str, RegexRule | ListRule | CallableRule] | None = None, normalizers: dict[str, Callable[[object], str]] | None = None, global_rules: GlobalRules | None = None)[source]
Bases:
objectManages naming conventions by validating and operating on strings.
Builds, parses, and validates names according to a configurable token structure. All token values are normalized to plain strings before validation, so callers may pass raw
strorEnummembers.- tokens
Ordered list of token name strings.
- Type:
list[str]
- separator
Character used to join token values.
- Type:
str
- rules
Mapping of token name to its
ConcreteRule.- Type:
dict[str, openrig.naming.types.RegexRule | openrig.naming.types.ListRule | openrig.naming.types.CallableRule]
- normalizers
Mapping of token name to its normalizer callable.
- Type:
dict[str, Callable[[object], str]]
- global_rules
Global constraints applied to every built name.
Example
>>> from openrig.naming.types import RegexRule, ListRule, GlobalRules >>> manager = Manager( ... tokens=["descriptor", "side", "usage"], ... separator="_", ... rules={ ... "descriptor": RegexRule(pattern=r"^[a-z][a-zA-Z0-9]*$"), ... "side": ListRule(allowed=frozenset({"l", "r", "c"})), ... }, ... global_rules=GlobalRules(max_length=80), ... ) >>> manager.build_name(descriptor="arm", side="l", usage="jnt") 'arm_l_jnt'
- __init__(tokens: Sequence[str] | None = None, separator: str | None = None, rules: dict[str, RegexRule | ListRule | CallableRule] | None = None, normalizers: dict[str, Callable[[object], str]] | None = None, global_rules: GlobalRules | None = None) None[source]
Initializes the naming Manager.
- Parameters:
tokens – Ordered sequence of token name strings.
separator – Character used to join token values. Must not be empty.
rules – Mapping of token name to its
ConcreteRule. Tokens without a rule accept any value.normalizers – Mapping of token name to its normalizer callable. Normalizers are applied before validation.
global_rules – Global constraints (max length, forbidden patterns). Defaults to an unconstrained
GlobalRulesinstance.
- Raises:
NamingConfigError – If
separatorisNoneor empty.
- tokens: list[str]
- separator: str
- rules: dict[str, RegexRule | ListRule | CallableRule]
- normalizers: dict[str, Callable[[object], str]]
- global_rules: GlobalRules
- classmethod from_dict(data: ManagerConfig) Manager[source]
Creates a
Managerinstance from aManagerConfigdictionary.- Parameters:
data – A
ManagerConfigtyped dict containing the configuration.- Returns:
A new
Managerinstance.
- to_dict() ManagerConfig[source]
Exports the current configuration as a
ManagerConfigdictionary.- Returns:
A
ManagerConfigtyped dict representing the current state.
- is_valid(name: str) bool[source]
Returns
Trueifnameis fully valid against all tokens.A name is fully valid when it contains a non-empty, rule-conforming value for every token defined in the convention. For partial validation use
get_dataand inspect individual token values.- Parameters:
name – The candidate name string.
- Returns:
Trueif the name is strictly valid,Falseotherwise.
- is_valid_token(token: str, value: str) bool[source]
Returns
Trueifvalueis valid for the given token.Checks that the value does not contain the separator, then delegates to the token’s
ConcreteRule.validatemethod.- Parameters:
token – The token name.
value – The normalized (post-normalization) string value.
- Returns:
Trueif valid,Falseotherwise.
- get_data(name: str) dict[str, str][source]
Extracts token values from a name string using the matching regex.
- Parameters:
name – The name string to parse.
- Returns:
A
TokenDatadict mapping every token name to its extracted value, or""for tokens not present in the name.
- parse(name: str) dict[str, str] | str[source]
Attempts to parse a name into its constituent token values.
Tries in order: 1. Regex-based extraction (strict structural match). 2. Split-based extraction (loose positional match). 3. Returns the original string if both fail.
- Parameters:
name – The name string to parse.
- Returns:
A
TokenDatadict if parsing succeeds, or the originalstrif the name does not conform to the convention.
- get_token_value(name: str, token_name: str) str[source]
Returns the value of a specific token extracted from a name.
- Parameters:
name – The name string to extract from.
token_name – The name of the token to retrieve.
- Returns:
The extracted token value, or
""if not present.- Raises:
NamingValidationError – If
token_nameis not defined in this manager’s token list.
- get_errors(name: str) list[str][source]
Returns a list of human-readable validation errors for a name.
- Parameters:
name – The name string to validate.
- Returns:
A list of error message strings. Empty if the name is valid.
- get_matching_regex(capture_groups: bool = False, full_match: bool = True, strict: bool = False) str[source]
Generates a regex pattern matching names against this convention.
Tokens are optional from right to left: for tokens
["descriptor", "side", "usage"]the pattern matches"arm","arm_l", and"arm_l_jnt", but never"arm__jnt".The pattern for each token is derived from its
ConcreteRuleviarule.to_regex_pattern(). Tokens without a rule use a catch-all pattern that excludes the separator.Results are cached by
(capture_groups, full_match, strict).- Parameters:
capture_groups – If
True, wraps each token in a named capture group(?P<token_name>...).full_match – If
True, anchors the pattern with^/$.strict – If
True, all tokens are required (no optional groups).
- Returns:
The assembled regex pattern string.
- add_rule(token: str, rule: RegexRule | ListRule | CallableRule) None[source]
Adds or replaces the validation rule for a token.
Clears the regex cache because the pattern depends on all rules.
- Parameters:
token – The token name.
rule – The
ConcreteRuleinstance to associate with the token.
- remove_rule(token: str) None[source]
Removes the validation rule for a token, if present.
Clears the regex cache because the pattern depends on all rules.
- Parameters:
token – The token name whose rule should be removed.
- build_name(**kwargs: str | Enum) str[source]
Builds a name string from token values.
Normalizes each provided value, validates it against the token’s rule, joins all non-empty values with the separator, and then checks the result against the global rules.
- Parameters:
**kwargs – Token values keyed by token name. Values may be
strorEnummembers; they are normalized before validation.- Returns:
The assembled name string.
- Raises:
NamingValidationError – If unknown tokens are provided, if a normalized value fails its rule, or if the assembled name violates a global constraint.
- update_name(name: str, **kwargs: str | Enum) str[source]
Updates specific token values in an existing name.
Parses
nameinto its token values, applieskwargsas overrides, then rebuilds the name.If
namecannot be parsed against the full convention, it is treated as a bare descriptor (first token) provided it passes the first token’s rule.- Parameters:
name – The existing name string to update.
**kwargs – Token values to override. Same rules as
build_name.
- Returns:
The updated name string.
- Raises:
NamingValidationError – If
namecannot be interpreted and is not empty, or if the updated values are invalid.
- resolve_name(value: str | dict[str, str] | Sequence[str | Enum], tokens: list[str] | None = None, rules: dict[str, RegexRule | ListRule | CallableRule] | None = None, normalizers: dict[str, Callable[[object], str]] | None = None) str[source]
Resolves a flexible input into a final name string.
Provides a unified entry point for callers that may supply names as dicts, sequences, or plain strings. When
tokens,rules, ornormalizersoverrides are provided, a temporaryManageris created with those overrides applied on top of the current config.Supported
valuetypes: -dict(TokenData): passed directly tobuild_name. -list/tuple(Sequence[TokenValue]): values aremapped positionally to tokens, then passed to
build_name.str: returned as-is (no building or validation).
- Parameters:
value – The input to resolve.
tokens – Optional token list override.
rules – Optional rules override.
normalizers – Optional normalizers override.
- Returns:
The resolved name string.
- Raises:
NamingValidationError – If a sequence input has more items than there are tokens.
- openrig.naming.get_manager() Manager[source]
Returns the pre-configured singleton
Managerinstance.Builds the instance on first call using the convention defined in
naming/config.json(viarules.py). Subsequent calls return the same instance.- Returns:
The singleton
Managerinstance.
Submodules
- openrig.naming.manager module
NamingErrorNamingConfigErrorNamingValidationErrorManagerConfigManagerManager.tokensManager.separatorManager.rulesManager.normalizersManager.global_rulesManager.__init__()Manager.tokensManager.separatorManager.rulesManager.normalizersManager.global_rulesManager.__repr__()Manager.from_dict()Manager.to_dict()Manager.is_valid()Manager.is_valid_token()Manager.get_data()Manager.parse()Manager.get_token_value()Manager.get_errors()Manager.get_matching_regex()Manager.add_rule()Manager.remove_rule()Manager.build_name()Manager.update_name()Manager.resolve_name()
- openrig.naming.normalizers module
- openrig.naming.rules module
- openrig.naming.types module
- openrig.naming.utils module
is_string()is_digit()is_camel_case()is_pascal_case()is_snake_case()is_kebab_case()is_character_in()get_case_style()get_digits()get_digit_by_index()get_first_digit()get_last_digit()get_digits_between_brackets()get_data_between_underscores()get_version()get_namespace()replace_spaces()normalize_text()to_upper()to_lower()to_camel_case()to_pascal_case()to_snake_case()to_kebab_case()value_to_str()str_to_value()strip_digits()split_text()join_tokens()capitalize_first()remove_prefix()remove_suffix()clean_txt()strip_namespace()get_base_name()truncate()split_name_number()get_unique_name()increment_character()increment_digit()replace_padding()add_suffix()add_prefix()add_text()swap_substrings()decrement_character()decrement_digit()