mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
"""Utility Functions for Pydantic Models."""
|
|
|
|
# Standard Library Imports
|
|
import re
|
|
|
|
# Third Party Imports
|
|
from pydantic import BaseSettings
|
|
|
|
|
|
def clean_name(_name):
|
|
"""Remove unsupported characters from field names.
|
|
|
|
Converts any "desirable" seperators to underscore, then removes all
|
|
characters that are unsupported in Python class variable names.
|
|
Also removes leading numbers underscores.
|
|
|
|
Arguments:
|
|
_name {str} -- Initial field name
|
|
|
|
Returns:
|
|
{str} -- Cleaned field name
|
|
"""
|
|
_replaced = re.sub(r"[\-|\.|\@|\~|\:\/|\s]", "_", _name)
|
|
_scrubbed = "".join(re.findall(r"([a-zA-Z]\w+|\_+)", _replaced))
|
|
return _scrubbed.lower()
|
|
|
|
|
|
class HyperglassModel(BaseSettings):
|
|
"""Base model for all hyperglass configuration models."""
|
|
|
|
pass
|
|
|
|
class Config:
|
|
"""Default Pydantic configuration.
|
|
|
|
See https://pydantic-docs.helpmanual.io/usage/model_config
|
|
"""
|
|
|
|
validate_all = True
|
|
extra = "forbid"
|
|
validate_assignment = True
|
|
alias_generator = clean_name
|
|
|
|
|
|
class HyperglassModelExtra(HyperglassModel):
|
|
"""Model for hyperglass configuration models with dynamic fields."""
|
|
|
|
pass
|
|
|
|
class Config:
|
|
"""Default pydantic configuration."""
|
|
|
|
extra = "allow"
|