Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Nov 12, 2024
1 parent 4591da9 commit ce4b02f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 93 deletions.
39 changes: 6 additions & 33 deletions mknodes/info/configfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import TYPE_CHECKING, Any

import upath
import yamling

from mknodes.utils import superdict

Expand All @@ -12,6 +12,8 @@


class ConfigFile(superdict.SuperDict):
filetype: str | None = None

def __init__(self, path: str | os.PathLike[str] | None = None):
"""Constructor.
Expand Down Expand Up @@ -45,19 +47,11 @@ def get_section_text(
if not sections:
raise ValueError(sections)
section = self.get_section(*sections, keep_path=keep_path)
return "" if section is None else self._dump(section)

def load_config(self, data: str):
"""Load a string with loader of given file type.
Args:
data: String with markup of type as config file
"""
self._data = self._load(data)
return "" if section is None else yamling.dump(section, mode=self.filetype) # type: ignore[arg-type]

def dump_config(self) -> str:
"""Dump to string with dumper of given file type."""
return self._dump(self._data)
return yamling.dump(self._data, mode=self.filetype) # type: ignore[arg-type]

def load_file(
self,
Expand All @@ -70,28 +64,7 @@ def load_file(
path: Path to the config file (also supports fsspec protocol URLs)
storage_options: Options for fsspec backend
"""
opts = storage_options or {}
file = upath.UPath(path, **opts)
text = file.read_text(encoding="utf-8")
self.load_config(text)

@classmethod
def _dump(cls, data: dict) -> str:
"""Needs to be reimplemented by subclasses.
Args:
data: Data to dump
"""
raise NotImplementedError

@classmethod
def _load(cls, data: str) -> dict | list:
"""Needs to be reimplemented by subclasses.
Args:
data: Data to load
"""
raise NotImplementedError
self._data = yamling.load_file(path, storage_options=storage_options or {})


if __name__ == "__main__":
Expand Down
14 changes: 1 addition & 13 deletions mknodes/info/tomlfile.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
from __future__ import annotations

import tomllib

import tomli_w

from mknodes.info import configfile


class TomlFile(configfile.ConfigFile):
multiline_strings = False

@classmethod
def _dump(cls, data: dict) -> str:
return tomli_w.dumps(data, multiline_strings=cls.multiline_strings)

@classmethod
def _load(cls, data: str) -> dict | list:
return tomllib.loads(data)
filetype = "toml"


if __name__ == "__main__":
Expand Down
61 changes: 14 additions & 47 deletions mknodes/info/yamlfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

from typing import TYPE_CHECKING, Any

from jinjarope import serializefilters
import upath
import yamling
from yamling import yamltypes

from mknodes.info import configfile
from mknodes.utils import log
Expand All @@ -19,57 +16,27 @@


class YamlFile(configfile.ConfigFile):
def __init__(
self,
path: str | os.PathLike[str] | None = None,
mode: yamltypes.LoaderStr = "unsafe",
resolve_inherit_tag: bool = False,
):
super().__init__(path)
if resolve_inherit_tag:
self.resolve_inherit_tag(mode)
filetype = "yaml"

def resolve_inherit_tag(
def load_file(
self,
mode: yamltypes.LoaderStr = "unsafe",
path: str | os.PathLike[str],
**storage_options: Any,
):
"""Resolve INHERIT key-value pair for this YAML file.
If this YAML file contains a key-value pair like "INHERIT: path_to_config.yml",
this method will resolve that tag by using the config at given path as the
"parent config".
Also supports a list of files for INHERIT.
"""Load a file with loader of given file type.
Args:
mode: The Yaml loader type
path: Path to the config file (also supports fsspec protocol URLs)
storage_options: Options for fsspec backend
"""
if not self.path:
msg = "Config file needs file path (INHERIT path is relative to file path)"
raise ValueError(msg)
abspath = upath.UPath(self.path).absolute()
if "INHERIT" not in self._data:
return
file_path = self._data.pop("INHERIT")
file_paths = [file_path] if isinstance(file_path, str) else file_path
for path in file_paths:
parent_cfg = abspath.parent / path
logger.debug("Loading inherited configuration file: %s", parent_cfg)
text = parent_cfg.read_text("utf-8")
parent = yamling.load_yaml(text, mode)
self._data: dict[str, Any] = serializefilters.merge(parent, self._data)

@classmethod
def _dump(cls, data: dict[str, Any]) -> str:
return yamling.dump_yaml(data)

@classmethod
def _load(
cls, data: str, mode: yamltypes.LoaderStr = "unsafe"
) -> dict[str, Any] | list[Any]:
return yamling.load_yaml(data, mode)
self._data = yamling.load_yaml_file(
path,
storage_options=storage_options or {},
resolve_inherit=True,
)
# type: ignore[arg-type]


if __name__ == "__main__":
info = YamlFile(".pre-commit-config.yaml")
print(info)
print(info._data)

0 comments on commit ce4b02f

Please sign in to comment.