mm-std 0.1.14__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mm_std/config.py
CHANGED
@@ -1,63 +1,24 @@
|
|
1
|
-
import io
|
2
1
|
import sys
|
2
|
+
import tomllib
|
3
3
|
from pathlib import Path
|
4
|
+
from typing import NoReturn
|
4
5
|
|
5
|
-
import yaml
|
6
6
|
from pydantic import BaseModel, ConfigDict, ValidationError
|
7
7
|
|
8
|
-
from .print_ import print_plain
|
8
|
+
from .print_ import print_json, print_plain
|
9
9
|
from .result import Err, Ok, Result
|
10
|
-
from .str import str_to_list
|
11
10
|
from .zip import read_text_from_zip_archive
|
12
11
|
|
13
12
|
|
14
13
|
class BaseConfig(BaseModel):
|
15
14
|
model_config = ConfigDict(extra="forbid")
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
v: str | list[str] | None,
|
21
|
-
*,
|
22
|
-
lower: bool = False,
|
23
|
-
unique: bool = False,
|
24
|
-
remove_comments: bool = False,
|
25
|
-
split_line: bool = False,
|
26
|
-
) -> list[str]:
|
27
|
-
if v is None:
|
28
|
-
return []
|
29
|
-
if isinstance(v, str):
|
30
|
-
return str_to_list(v, unique=unique, remove_comments=remove_comments, split_line=split_line, lower=lower)
|
31
|
-
return v
|
16
|
+
def print_and_exit(self, exclude: set[str] | None = None) -> NoReturn:
|
17
|
+
print_json(self.model_dump(exclude=exclude))
|
18
|
+
sys.exit(0)
|
32
19
|
|
33
20
|
@classmethod
|
34
|
-
def
|
35
|
-
try:
|
36
|
-
# is it zip archive?
|
37
|
-
if isinstance(config_path, str) and config_path.endswith(".zip"):
|
38
|
-
config_path = str(Path(config_path).expanduser())
|
39
|
-
return Ok(cls(**yaml.full_load(read_text_from_zip_archive(config_path, password=zip_password))))
|
40
|
-
if isinstance(config_path, io.TextIOWrapper) and config_path.name.endswith(".zip"):
|
41
|
-
config_path = str(Path(config_path.name).expanduser())
|
42
|
-
return Ok(cls(**yaml.full_load(read_text_from_zip_archive(config_path, password=zip_password))))
|
43
|
-
if isinstance(config_path, Path) and config_path.name.endswith(".zip"):
|
44
|
-
config_path = str(config_path.expanduser())
|
45
|
-
return Ok(cls(**yaml.full_load(read_text_from_zip_archive(config_path, password=zip_password))))
|
46
|
-
|
47
|
-
# plain yml file
|
48
|
-
if isinstance(config_path, str):
|
49
|
-
return Ok(cls(**yaml.full_load(Path(config_path).expanduser().read_text())))
|
50
|
-
if isinstance(config_path, Path):
|
51
|
-
return Ok(cls(**yaml.full_load(config_path.expanduser().read_text())))
|
52
|
-
|
53
|
-
return Ok(cls(**yaml.full_load(config_path)))
|
54
|
-
except ValidationError as err:
|
55
|
-
return Err("validator_error", data={"errors": err.errors()})
|
56
|
-
except Exception as err:
|
57
|
-
return Err(err)
|
58
|
-
|
59
|
-
@classmethod
|
60
|
-
def read_config_or_exit[T](cls: type[T], config_path: io.TextIOWrapper | str | Path, zip_password: str = "") -> T: # noqa: PYI019 # nosec
|
21
|
+
def read_toml_config_or_exit[T](cls: type[T], config_path: Path, zip_password: str = "") -> T: # noqa: PYI019 # nosec
|
61
22
|
res = cls.read_config(config_path, zip_password) # type: ignore[attr-defined]
|
62
23
|
if isinstance(res, Ok):
|
63
24
|
return res.unwrap() # type: ignore[no-any-return]
|
@@ -72,3 +33,18 @@ class BaseConfig(BaseModel):
|
|
72
33
|
print_plain(f"can't parse config file: {res.err}")
|
73
34
|
|
74
35
|
sys.exit(1)
|
36
|
+
|
37
|
+
@classmethod
|
38
|
+
def read_toml_config[T](cls: type[T], config_path: Path, zip_password: str = "") -> Result[T]: # nosec
|
39
|
+
try:
|
40
|
+
config_path = config_path.expanduser()
|
41
|
+
if config_path.name.endswith(".zip"):
|
42
|
+
data = tomllib.loads(read_text_from_zip_archive(config_path, password=zip_password))
|
43
|
+
else:
|
44
|
+
with config_path.open("rb") as f:
|
45
|
+
data = tomllib.load(f)
|
46
|
+
return Ok(cls(**data))
|
47
|
+
except ValidationError as err:
|
48
|
+
return Err("validator_error", data={"errors": err.errors()})
|
49
|
+
except Exception as err:
|
50
|
+
return Err(err)
|
mm_std/print_.py
CHANGED
@@ -22,7 +22,7 @@ def fatal(message: str, code: int = 1) -> NoReturn:
|
|
22
22
|
sys.exit(code)
|
23
23
|
|
24
24
|
|
25
|
-
def print_console(*messages: object, print_json: bool = False, default: Callable[[object], str] | None =
|
25
|
+
def print_console(*messages: object, print_json: bool = False, default: Callable[[object], str] | None = str) -> None:
|
26
26
|
if len(messages) == 1:
|
27
27
|
message = messages[0]
|
28
28
|
if isinstance(message, str):
|
mm_std/zip.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
+
from pathlib import Path
|
1
2
|
from zipfile import ZipFile
|
2
3
|
|
3
4
|
|
4
|
-
def read_text_from_zip_archive(zip_archive_path:
|
5
|
+
def read_text_from_zip_archive(zip_archive_path: Path, filename: str | None = None, password: str | None = None) -> str:
|
5
6
|
with ZipFile(zip_archive_path) as zipfile:
|
6
7
|
if filename is None:
|
7
8
|
filename = zipfile.filelist[0].filename
|
@@ -1,7 +1,7 @@
|
|
1
1
|
mm_std/__init__.py,sha256=dtYnmQP_HkWxIJvuCJGpex3RHvG2V0Ekh7oYstRQoco,2291
|
2
2
|
mm_std/command.py,sha256=ze286wjUjg0QSTgIu-2WZks53_Vclg69UaYYgPpQvCU,1283
|
3
3
|
mm_std/concurrency.py,sha256=4kKLhde6YQYsjJJjH6K5eMQj6FtegEz55Mo5TmhQMM0,5242
|
4
|
-
mm_std/config.py,sha256=
|
4
|
+
mm_std/config.py,sha256=RV_TFVLCvb9RDOg3xmt0GKs_-Hfkfm04R3Kqj7MGdAE,1869
|
5
5
|
mm_std/crypto.py,sha256=jdk0_TCmeU0pPXMyz9xH6kQHSjjZ9GcGClBwQps5vBo,340
|
6
6
|
mm_std/date.py,sha256=976eEkSONuNqHQBgSRu8hrtH23tJqztbmHFHLdbP2TY,1879
|
7
7
|
mm_std/dict.py,sha256=kJBPVG9vEqHiSgKKoji8gVGL1yEBbxAmFNn0zz17AUg,180
|
@@ -11,13 +11,13 @@ mm_std/http_.py,sha256=QaPPXVb-rOS0BpoKdYQ0ABm_-mcR5dNa7Uqn-SeW_kE,4119
|
|
11
11
|
mm_std/json_.py,sha256=jAL-JrSMPIwxu3y87xNoTcwhOOEHGHrTpX1BTj44Uhw,1091
|
12
12
|
mm_std/log.py,sha256=6ux6njNKc_ZCQlvWn1FZR6vcSY2Cem-mQzmNXvsg5IE,913
|
13
13
|
mm_std/net.py,sha256=qdRCBIDneip6FaPNe5mx31UtYVmzqam_AoUF7ydEyjA,590
|
14
|
-
mm_std/print_.py,sha256=
|
14
|
+
mm_std/print_.py,sha256=sf_Dq8BaYETyyHl6qXMDkrVZtAq0X6vcfj6lxNtYByA,1690
|
15
15
|
mm_std/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
16
16
|
mm_std/random_.py,sha256=OuUX4VJeSd13NZBya4qrGpR2TfN7_87tfebOY6DBUnI,1113
|
17
17
|
mm_std/result.py,sha256=KtYZbVJMkwwCPcV-Tnt0TkTNyDgiALvQB1DtIEp1prc,7405
|
18
18
|
mm_std/str.py,sha256=jS7VAI7i_a3iqnfaW4Iw2LZRTv0Tml4kmMbP2S2IUF4,3067
|
19
19
|
mm_std/types_.py,sha256=hvZlnvBWyB8CL_MeEWWD0Y0nN677plibYn3yD-5g7xs,99
|
20
|
-
mm_std/zip.py,sha256=
|
21
|
-
mm_std-0.
|
22
|
-
mm_std-0.
|
23
|
-
mm_std-0.
|
20
|
+
mm_std/zip.py,sha256=axzF1BwcIygtfNNTefZH7hXKaQqwe-ZH3ChuRWr9dnk,396
|
21
|
+
mm_std-0.2.0.dist-info/METADATA,sha256=r9EWtjknasr6kFFz7lT9-IBa3JRo-jT5uQFNB_X1vUA,306
|
22
|
+
mm_std-0.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
23
|
+
mm_std-0.2.0.dist-info/RECORD,,
|
File without changes
|