Source code for jgdv.structs.chainguard.mixins.loader_m

  1#!/usr/bin/env python3
  2"""
  3
  4"""
  5
  6# Imports:
  7##-- builtin imports
  8from __future__ import annotations
  9
 10import datetime
 11import enum
 12import functools as ftz
 13import itertools as itz
 14import logging as logmod
 15import pathlib as pl
 16import re
 17import time
 18import types
 19import weakref
 20import tomllib
 21from uuid import UUID, uuid1
 22
 23##-- end builtin imports
 24
 25# ##-- types
 26# isort: off
 27import abc
 28import collections.abc
 29from typing import TYPE_CHECKING, cast, assert_type, assert_never
 30from typing import Generic, NewType
 31# Protocols:
 32from typing import Protocol, runtime_checkable
 33# Typing Decorators:
 34from typing import no_type_check, final, override, overload
 35
 36if TYPE_CHECKING:
 37    from .._interface import TomlTypes
 38    from jgdv import Maybe
 39    from typing import Final
 40    from typing import ClassVar, Any, LiteralString
 41    from typing import Never, Self, Literal
 42    from typing import TypeGuard, TypeVar
 43    from collections.abc import Iterable, Iterator, Callable, Generator
 44    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
 45
 46    from .._interface import ChainGuard_i
 47    T = TypeVar('T')
 48
 49# isort: on
 50# ##-- end types
 51
 52##-- logging
 53logging = logmod.getLogger(__name__)
 54##-- end logging
 55
 56
 57LoadFailMsg           : Final[str] = "ChainGuard Failed to Load: "
 58TomlLoadFailMsg       : Final[str] = "Failed to Load Toml"
 59DirectoryLoadFailMsg  : Final[str] = "ChainGuard Failed to load Directory: "
 60
 61##--|
[docs] 62class TomlLoader_m: 63 """ Mixin for loading toml files """ 64
[docs] 65 @classmethod 66 def read(cls:type[ChainGuard_i], text:str) -> ChainGuard_i: 67 logging.debug("Reading ChainGuard for text") 68 try: 69 return cls(tomllib.loads(text)) # type: ignore[call-arg] 70 except Exception as err: 71 raise OSError(LoadFailMsg, text, err.args) from err
72
[docs] 73 @classmethod 74 def from_dict(cls:type[ChainGuard_i], data:dict[str, TomlTypes]) -> ChainGuard_i: 75 logging.debug("Making ChainGuard from dict") 76 try: 77 return cls(data) 78 except Exception as err: 79 raise OSError(LoadFailMsg, data, err.args) from err
80
[docs] 81 @classmethod 82 def load(cls:type[ChainGuard_i], *paths:str|pl.Path) -> ChainGuard_i: 83 logging.debug("Creating ChainGuard for %s", paths) 84 texts = [] 85 for path in paths: 86 texts.append(pl.Path(path).read_text()) 87 else: 88 try: 89 return cls(tomllib.loads("\n".join(texts))) 90 except tomllib.TOMLDecodeError as err: 91 raise OSError(TomlLoadFailMsg, *err.args, paths) from err
92
[docs] 93 @classmethod 94 def load_dir(cls:type[ChainGuard_i], dirp:str|pl.Path) -> ChainGuard_i: 95 logging.debug("Creating ChainGuard for directory: %s", str(dirp)) 96 try: 97 texts = [] 98 for path in pl.Path(dirp).glob("*.toml"): 99 texts.append(path.read_text()) 100 101 return cls(tomllib.loads("\n".join(texts))) 102 except Exception as err: 103 raise OSError(DirectoryLoadFailMsg, dirp, err.args) from err