Skip to content

Commit

Permalink
Create yaml_loader.py
Browse files Browse the repository at this point in the history
  • Loading branch information
aclerc committed Apr 22, 2024
1 parent 1086375 commit 00fa11c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions wind_up/yaml_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from pathlib import Path
from typing import IO, Any

import yaml


class Loader(yaml.SafeLoader):
def __init__(self: "Loader", stream: IO) -> None:
try:
self._root = os.path.split(stream.name)[0]
except AttributeError:
self._root = os.path.curdir

super().__init__(stream)

@property
def root(self: "Loader") -> str:
return self._root


def construct_include(loader: Loader, node: yaml.Node) -> Any: # noqa ANN401
filename = Path.resolve(Path(loader.root) / Path(loader.construct_scalar(node))) # type: ignore[arg-type]
extension = os.path.splitext(filename)[1].lstrip(".") # noqa PTH122

with Path.open(filename) as f:
return yaml.load(f, Loader) if extension in ("yaml", "yml") else "".join(f.readlines()) # noqa S506

0 comments on commit 00fa11c

Please sign in to comment.