yaml

Mixin for handling configs stored in yaml Should be split off into another package :)

class YamlRepresenter(default_style: Any = None, default_flow_style: Any = None, dumper: Any = None)[source]

Dumper that can represent extra types like Paths

represent_path(data: Path) ScalarNode[source]

Represent a path as a string

yaml_representers: Dict[Any, Any] = {<class 'NoneType'>: <function RoundTripRepresenter.represent_none>, <class 'bool'>: <function SafeRepresenter.represent_bool>, <class 'bytes'>: <function SafeRepresenter.represent_binary>, <class 'collections.OrderedDict'>: <function SafeRepresenter.represent_ordereddict>, <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 YamlRepresenter.represent_path>, <class 'ruamel.yaml.comments.CommentedMap'>: <function RoundTripRepresenter.represent_dict>, <class 'ruamel.yaml.comments.CommentedOrderedMap'>: <function SafeRepresenter.represent_ordereddict>, <class 'ruamel.yaml.comments.CommentedSeq'>: <function RoundTripRepresenter.represent_list>, <class 'ruamel.yaml.comments.CommentedSet'>: <function RoundTripRepresenter.represent_set>, <class 'ruamel.yaml.comments.TaggedScalar'>: <function RoundTripRepresenter.represent_tagged_scalar>, <class 'ruamel.yaml.compat.ordereddict'>: <function SafeRepresenter.represent_ordereddict>, <class 'ruamel.yaml.scalarbool.ScalarBoolean'>: <function RoundTripRepresenter.represent_scalar_bool>, <class 'ruamel.yaml.scalarfloat.ScalarFloat'>: <function RoundTripRepresenter.represent_scalar_float>, <class 'ruamel.yaml.scalarint.BinaryInt'>: <function RoundTripRepresenter.represent_binary_int>, <class 'ruamel.yaml.scalarint.HexCapsInt'>: <function RoundTripRepresenter.represent_hex_caps_int>, <class 'ruamel.yaml.scalarint.HexInt'>: <function RoundTripRepresenter.represent_hex_int>, <class 'ruamel.yaml.scalarint.OctalInt'>: <function RoundTripRepresenter.represent_octal_int>, <class 'ruamel.yaml.scalarint.ScalarInt'>: <function RoundTripRepresenter.represent_scalar_int>, <class 'ruamel.yaml.scalarstring.DoubleQuotedScalarString'>: <function RoundTripRepresenter.represent_double_quoted_scalarstring>, <class 'ruamel.yaml.scalarstring.FoldedScalarString'>: <function RoundTripRepresenter.represent_folded_scalarstring>, <class 'ruamel.yaml.scalarstring.LiteralScalarString'>: <function RoundTripRepresenter.represent_literal_scalarstring>, <class 'ruamel.yaml.scalarstring.PlainScalarString'>: <function RoundTripRepresenter.represent_plain_scalarstring>, <class 'ruamel.yaml.scalarstring.SingleQuotedScalarString'>: <function RoundTripRepresenter.represent_single_quoted_scalarstring>, <class 'ruamel.yaml.timestamp.TimeStamp'>: <function RoundTripRepresenter.represent_datetime>, <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(yaml_source: CommentedMap | None = None, **kwargs: Any)[source]

Mixin class that provides from_yaml() and to_yaml() classmethods

classmethod from_yaml(file_path: str | Path) Self[source]

Instantiate this class by passing the contents of a yaml file as kwargs

to_yaml(path: Path | None = None, **kwargs: Any) str[source]

Dump the contents of this class to a yaml file, returning the contents of the dumped string

to_yamls(**kwargs: Any) str[source]

Dump the contents of this class to a yaml string

Parameters:

**kwargs – passed to BaseModel.model_dump()

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.dev232+g3ee3578",
         "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 0x77de2d4385e0>

Validated by:
field noob_version: str = '0.1.2.dev232+g3ee3578'
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 id of a config file in the user configs directory or builtin

  • a relative Path to a config file, relative to the current working directory

  • a relative Path to a config file, relative to the user config directory

  • an absolute Path to a config file

  • an 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_dir or the packaged config dir.

Note

this method does not yet validate that the config matches the model loading it

classmethod from_yaml(file_path: str | Path) Self[source]

Instantiate this class by passing the contents of a yaml file as kwargs

HEADER_FIELDS: ClassVar[tuple[str, ...]] = ('noob_id', 'noob_model', 'noob_version')
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 for

  • path (pathlib.Path , str) – The yaml file to peek into

  • root (bool) – Only find keys at the root of the document (default True ), 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