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

 1#!/usr/bin/env python3
 2"""
 3
 4"""
 5
 6##-- builtin imports
 7from __future__ import annotations
 8
 9import datetime
10import enum
11import functools as ftz
12import itertools as itz
13import logging as logmod
14import re
15import time
16import types
17import weakref
18from uuid import UUID, uuid1
19
20##-- end builtin imports
21
22# ##-- types
23# isort: off
24import abc
25import collections.abc
26from typing import TYPE_CHECKING, cast, assert_type, assert_never
27from typing import Generic, NewType, Never
28# Protocols:
29from typing import Protocol, runtime_checkable
30# Typing Decorators:
31from typing import no_type_check, final, override, overload
32
33if TYPE_CHECKING:
34    import pathlib as pl
35    from jgdv import Maybe
36    from typing import Final
37    from typing import ClassVar, Any, LiteralString
38    from typing import Self, Literal
39    from typing import TypeGuard
40    from collections.abc import Iterable, Iterator, Callable, Generator
41    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
42
43    from .._interface import ChainGuard_i
44
45##--|
46
47# isort: on
48# ##-- end types
49
50##-- logging
51logging = logmod.getLogger(__name__)
52##-- end logging
53
54try:
55    import tomli_w
56
57    class TomlWriter_m:
58        """ A mixin for adding toml-writing functionality """
59
60        @override
61        def __str__(self:ChainGuard_i) -> str:
62            return tomli_w.dumps(self._table())
63
64        def to_file(self:ChainGuard_i, path:pl.Path) -> None:
65            path.write_text(str(self))
66
67except ImportError:
68    logging.debug("No Tomli-w found, ChainGuard will not write toml, only read it")
69
[docs] 70 class TomlWriter_m: # type: ignore[no-redef] 71 """ A fallback mixin for when toml-writing isnt available""" 72
[docs] 73 def to_file(self, path:pl.Path) -> None: 74 msg = "Tomli-w isn't installed, so ChainGuard can't write, only read" 75 raise NotImplementedError(msg)