"""Nomenclature============This is the nomenclature about manifest references names (rules and properties).* A rule name starts with a prefix from ``RULE_BASE_PREFIX``;* Rule prefix is followed from its type that can be either: * The ``RULE_META`` value; * ``reference`` string;* Then rule name ends with its component name;* Rule name parts are separated with ``-``.There is a limited set of allowed characters:* For rule component name they are registred in ``RULE_ALLOWED_CHARS``;* For rule property name they are registred in ``PROPERTY_ALLOWED_CHARS``;There is some reserved name:* For rule component name they are registred in ``RESERVED_RULE_NAMES``;* For rule property name they are registred in ``RESERVED_PROPERTY_NAMES``;"""fromstringimportascii_letters,digitsfrom.exceptionsimportStyleguideValidationErrorRULE_BASE_PREFIX="styleguide"RULE_META="metas"RULE_META_REFERENCES="-".join((RULE_BASE_PREFIX,RULE_META,"references"))RULE_META_COMPILER="-".join((RULE_BASE_PREFIX,RULE_META,"compiler"))RULE_REFERENCE="-".join((RULE_BASE_PREFIX,"reference"))RULE_ALLOWED_START=ascii_lettersRULE_ALLOWED_CHARS=ascii_letters+digits+"_"PROPERTY_ALLOWED_START=ascii_lettersPROPERTY_ALLOWED_CHARS=ascii_letters+digits+"_"FORBIDDEN_PREFIXES=("_","-")"""Rule and property names can not start with following strings"""RESERVED_RULE_NAMES=("styleguide","load","to_dict","to_json","from_dict","metas",)"""Rule name can not be one of the following string"""RESERVED_PROPERTY_NAMES=("structure",)"""Property (variable) name can not be one of the following string"""REFERENCE_STRUCTURES=("flat","list","string","number","json","object-complex","nested",)"""Tuple of available reference structures. Structure name ``json`` is deprecated andsupersed by name ``object-complex``."""
[docs]defis_reserved_rule(name):""" Validate name against ``RESERVED_RULE_NAMES``. Arguments: name (string): Rule name. Returns: bool: ``True`` if name match a reserved name. """returnnameinRESERVED_RULE_NAMES
[docs]defis_reserved_property(name):""" Validate name against ``RESERVED_PROPERTY_NAMES``. Arguments: name (string): Property name. Returns: bool: ``True`` if name match a reserved name. """returnnameinRESERVED_PROPERTY_NAMES
[docs]defis_valid_rule(name):""" Validate rule name. Arguments: name (string): Rule name. Returns: bool: ``True`` if rule name is valid. """ifnotname:raiseStyleguideValidationError("Rule name is empty")ifis_reserved_rule(name):msg="Rule name '{}' is reserved, you can not use it for a rule"raiseStyleguideValidationError(msg.format(name))ifname.startswith(FORBIDDEN_PREFIXES):msg="Rule name '{}' cannot starts with special characters"raiseStyleguideValidationError(msg.format(name))ifname[0]notinRULE_ALLOWED_START:msg="Rule name '{}' must starts with a letter"raiseStyleguideValidationError(msg.format(name))foriteminname:ifitemnotinRULE_ALLOWED_CHARS:msg=("Invalid rule name '{}': it must only contains ""letters, numbers and '_' character")raiseStyleguideValidationError(msg.format(name))returnTrue
[docs]defis_valid_property(name):""" Validate property name. Arguments: name (string): Property name. Returns: bool: ``True`` if variable name is valid. """ifnotname:raiseStyleguideValidationError("Variable name is empty")ifis_reserved_property(name):msg="Variable name '{}' is reserved, you can not use it for a variable"raiseStyleguideValidationError(msg.format(name))ifname.startswith(FORBIDDEN_PREFIXES):msg="Variable name '{}' cannot starts with special characters"raiseStyleguideValidationError(msg.format(name))ifname[0]notinPROPERTY_ALLOWED_START:msg="Variable name '{}' must starts with a letter"raiseStyleguideValidationError(msg.format(name))foriteminname:ifitemnotinPROPERTY_ALLOWED_CHARS:msg=("Invalid variable name '{}': it must only contains ""letters, numbers and '_' character")raiseStyleguideValidationError(msg.format(name))returnTrue