2019-09-11 01:35:31 -07:00
|
|
|
"""
|
|
|
|
Utility Functions for Pydantic Models
|
|
|
|
"""
|
|
|
|
|
2019-10-09 03:10:52 -07:00
|
|
|
# Standard Library Imports
|
2019-09-11 01:35:31 -07:00
|
|
|
import re
|
2019-10-09 03:10:52 -07:00
|
|
|
|
|
|
|
# Third Party Imports
|
2019-10-04 16:54:32 -07:00
|
|
|
from pydantic import BaseSettings
|
2019-09-11 01:35:31 -07:00
|
|
|
|
|
|
|
|
|
|
|
def clean_name(_name):
|
|
|
|
"""
|
|
|
|
Converts any "desirable" seperators to underscore, then
|
|
|
|
removes all characters that are unsupported in Python class
|
|
|
|
variable names. Also removes leading numbers underscores.
|
|
|
|
"""
|
|
|
|
_replaced = re.sub(r"[\-|\.|\@|\~|\:\/|\s]", "_", _name)
|
|
|
|
_scrubbed = "".join(re.findall(r"([a-zA-Z]\w+|\_+)", _replaced))
|
|
|
|
return _scrubbed.lower()
|
2019-10-04 16:54:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
class HyperglassModel(BaseSettings):
|
|
|
|
"""Base model for all hyperglass configuration models"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
"""Default pydantic configuration"""
|
|
|
|
|
|
|
|
validate_all = True
|
|
|
|
extra = "forbid"
|
|
|
|
validate_assignment = True
|
2019-10-09 03:10:52 -07:00
|
|
|
alias_generator = clean_name
|
|
|
|
|
|
|
|
|
|
|
|
class HyperglassModelExtra(HyperglassModel):
|
|
|
|
"""Model for hyperglass configuration models with dynamic fields"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
"""Default pydantic configuration"""
|
|
|
|
|
|
|
|
extra = "allow"
|