Skip to content

rename

Full documentation pages are generated for docstring reference only and may contain symbols imported from other modules. Imported symbols are not distinguished from locally defined symbols and will appear in any module that they are imported into. For better information on where symbols should be imported from, review the sourcecode on the github.

FoSpy.parsing.validators.rename

RESERVED module-attribute

RESERVED = ['rename', 'metadata']

_validator_rules

_validator_rules(*args, inherit_from=None)
Source code in FoSpy/_docs/properties/__init__.py
def _validator_rules(*args, inherit_from=None):
    from ..._docs.properties import val_rules
    def decorator(func, a=args, ih=inherit_from):
        inherited = val_rules.get(ih, [])
        a = list(a)
        a.extend(inherited)
        a = tuple(a)

        if len(a) > 0:
            val_rules[func] = a

        return func
    return decorator

rename_dict

rename_dict(nameDict, blk_cls, **kwargs)
Source code in FoSpy/parsing/validators/rename.py
@_validator_rules(
    "A dictionary of key:value string pairs",
    "Keys starting with \"`_`\" are ignored.",
    "Keys must be expected property names for the block.",[
        "For unexpected (custom) keys, renaming is not necessary."
    ],
    "Values cannot be registered property names."
)
def rename_dict(nameDict, blk_cls, **kwargs):
    from FoSpy.blocks.blocks import SingleBlock
    if not isinstance(nameDict, dict):
        raise TypeError("The rename value must be a dictionary of key:value string pairs.")
    vals = blk_cls.build_validators()
    outDict = nameDict.copy()
    for name, rename in nameDict.items():
        if name.startswith("_"):
            outDict.pop(name)
            continue
        error = f"Cannot rename '{name}' to '{rename}' for '{blk_cls.__name__}' block."
        if name not in vals:
            raise ValueError(error, f"'{name}' is not an expected key.")
        if rename in vals:
            raise ValueError(error, f"'{rename}' is already a registered key.")

    return SingleBlock.dispatch_subclass(outDict)

rename_value

rename_value(val, blk_cls, prop_name, **kwargs)
Source code in FoSpy/parsing/validators/rename.py
@_validator_rules(
    "Each property of a `Rename` object renames its parent's expected "
    "property of the same name to the provided value.",
    "Renaming properties starting with \"`_`\" is not allowed.",
    "Properties must be expected property names for the parent block.",[
        "For unexpected (custom) keys, adding a rename mapping is not necessary.",
        "(There is no required/optional validator to redirect to.)"
    ],
    "You cannot rename a property to a different expected property. (Unexpected names only.)",
    "The following reserved properties cannot be renamed:",
    [f"`{name}`" for name in RESERVED]
)
def rename_value(val, blk_cls, prop_name, **kwargs):
    if val.startswith("_"):
        raise ValueError("Renaming properties starting with \"_\" is not allowed.")

    validators = blk_cls.build_validators()

    error = f"Cannot rename '{prop_name}' to '{val}'."
    if prop_name not in validators:
        raise ValueError(error, f"'{prop_name}' is not an expected key.")
    if val in validators:
        raise ValueError(error, f"'{val}' is already a registered key.")

    if prop_name in RESERVED:
        raise ValueError(error, f"Property '{prop_name}' cannot be renamed.")

    return val