yaml¶
Mixin for handling configs stored in yaml Should be split off into another package :)
- class YamlDumper(stream, default_style=None, default_flow_style=False, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, sort_keys=True)[source]¶
Dumper that can represent extra types like Paths
- yaml_representers = {<class 'NoneType'>: <function SafeRepresenter.represent_none>, <class 'bool'>: <function SafeRepresenter.represent_bool>, <class 'bytes'>: <function SafeRepresenter.represent_binary>, <class 'datetime.date'>: <function SafeRepresenter.represent_date>, <class 'datetime.datetime'>: <function SafeRepresenter.represent_datetime>, <class 'dict'>: <function SafeRepresenter.represent_dict>, <class 'float'>: <function SafeRepresenter.represent_float>, <class 'int'>: <function SafeRepresenter.represent_int>, <class 'list'>: <function SafeRepresenter.represent_list>, <class 'pathlib._local.PosixPath'>: <function YamlDumper.represent_path>, <class 'set'>: <function SafeRepresenter.represent_set>, <class 'str'>: <function SafeRepresenter.represent_str>, <class 'tuple'>: <function SafeRepresenter.represent_list>, None: <function SafeRepresenter.represent_undefined>}¶
- class YAMLMixin[source]¶
Mixin class that provides
from_yaml()andto_yaml()classmethods- classmethod from_yaml(file_path: str | Path) Self[source]¶
Instantiate this class by passing the contents of a yaml file as kwargs
- pydantic model ConfigYAMLMixin[source]¶
Yaml Mixin class that always puts a header consisting of
id - unique identifier for this config
noob_model - fully-qualified module path to model class
noob_version - version of noob when this model was created
at the top of the file.
Show JSON schema
{ "title": "ConfigYAMLMixin", "description": "Yaml Mixin class that always puts a header consisting of\n\n * `id` - unique identifier for this config\n * `noob_model` - fully-qualified module path to model class\n * `noob_version` - version of noob when this model was created\n\n at the top of the file.", "type": "object", "properties": { "noob_id": { "anyOf": [ { "pattern": "[\\w\\-\\/#]+", "type": "string" }, { "type": "null" } ], "default": null, "title": "Noob Id" }, "noob_model": { "default": null, "title": "Noob Model", "type": "string" }, "noob_version": { "default": "0.1.2.dev70+g67234e2", "title": "Noob Version", "type": "string" } } }
- Config:
validate_default: bool = True
- Fields:
- Validators:
- field noob_id: Annotated[str, FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(pattern='[\\w\\-\\/#]+')])] | None = None¶
- field noob_model: Annotated[str, AfterValidator(func=_is_absolute_identifier)] = None¶
- Constraints:
func = <function _is_absolute_identifier at 0x763d55715e40>
- Validated by:
- classmethod config_sources() list[Path][source]¶
Directories to search for config files, in order of priority such that earlier sources are preferred over later sources.
- validator fill_noob_model » noob_model[source]¶
Get name of instantiating model, if not provided
- classmethod from_any(source: Path | PathLike[str] | Annotated[str, FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(pattern='[\\w\\-\\/#]+')])] | Self) Self[source]¶
Try and instantiate a config model from any supported constructor.
- Parameters:
source (
ConfigID,Path,PathLike[str]) –Either
the
idof a config file in the user configs directory or builtina relative
Pathto a config file, relative to the current working directorya relative
Pathto a config file, relative to the user config directoryan absolute
Pathto a config filean instance of the class to be constructed (returned unchanged)
- classmethod from_id(id: Annotated[str, FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(pattern='[\\w\\-\\/#]+')])]) Self[source]¶
Instantiate a model from a config id specified in one of the .yaml configs in either the user
Config.config_diror the packagedconfigdir.Note
this method does not yet validate that the config matches the model loading it
- yaml_peek(key: str, path: str | Path, root: bool = True, first: Literal[True] = True) str[source]¶
- yaml_peek(key: str, path: str | Path, root: bool = True, first: Literal[False] = False) list[str]
- yaml_peek(key: str, path: str | Path, root: bool = True, first: bool = True) str | list[str]
Peek into a yaml file without parsing the whole file to retrieve the value of a single key.
This function is _not_ designed for robustness to the yaml spec, it is for simple key: value pairs, not fancy shit like multiline strings, tagged values, etc. If you want it to be, then i’m afraid you’ll have to make a PR about it.
Returns a string no matter what the yaml type is so ya have to do your own casting if you want
- Parameters:
key (
str) – The key to peek forpath (
pathlib.Path,str) – The yaml file to peek intoroot (
bool) – Only find keys at the root of the document (defaultTrue), otherwise find keys at any level of nesting.first (
bool) – Only return the first appearance of the key (default). Otherwise return a list of values (not implemented lol)
- Returns:
str