Use pydantic settings management for nomad.config
Pydantic has build in support for settings management. This supports everything that our NomadConfig
-obj system provides (but better):
- nested config objects
- override from first env (limited), than config file, than default values
Here is an example of how it could work:
from typing import Dict, Any
from pydantic import BaseModel, Field, BaseSettings, HttpUrl
import yaml
import os.path
import os
class Client(BaseModel):
user: str = Field('test', description='The username.')
password: str = '*'
url: HttpUrl = Field('http://nomad-lab.eu')
class NomadConfig(BaseSettings):
client: Client = Client()
not_nested: str = 'hello'
class Config:
env_prefix = 'nomad_'
case_sensitive = False
@classmethod
def customise_sources(
cls,
init_settings,
env_settings,
file_secret_settings):
return (
init_settings,
env_settings,
yaml_config_settings_source, file_secret_settings)
def yaml_config_settings_source(settings: BaseSettings) -> Dict[str, Any]:
if not os.path.exists('nomad.yaml'):
return {}
try:
with open('nomad.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
if data is None:
return {}
return data
except yaml.YAMLError as e:
raise e
config = NomadConfig()
The support of env vars is limited as they only apply to the first level (e.g. NomadConfig). To set nested fields, the top level env var has to be set with a json value:
export NOMAD_CLIENT='{"password":"mypassword"}'
If necessary this could be replaced by a custom settings source.