mm-std 0.1.15__py3-none-any.whl → 0.2.1__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,14 +1,12 @@
1
- import io
2
1
  import sys
2
+ import tomllib
3
3
  from pathlib import Path
4
4
  from typing import NoReturn
5
5
 
6
- import yaml
7
6
  from pydantic import BaseModel, ConfigDict, ValidationError
8
7
 
9
8
  from .print_ import print_json, print_plain
10
9
  from .result import Err, Ok, Result
11
- from .str import str_to_list
12
10
  from .zip import read_text_from_zip_archive
13
11
 
14
12
 
@@ -20,50 +18,8 @@ class BaseConfig(BaseModel):
20
18
  sys.exit(0)
21
19
 
22
20
  @classmethod
23
- def to_list_str_validator(
24
- cls,
25
- v: str | list[str] | None,
26
- *,
27
- lower: bool = False,
28
- unique: bool = False,
29
- remove_comments: bool = False,
30
- split_line: bool = False,
31
- ) -> list[str]:
32
- if v is None:
33
- return []
34
- if isinstance(v, str):
35
- return str_to_list(v, unique=unique, remove_comments=remove_comments, split_line=split_line, lower=lower)
36
- return v
37
-
38
- @classmethod
39
- def read_config[T](cls: type[T], config_path: io.TextIOWrapper | str | Path, zip_password: str = "") -> Result[T]: # nosec
40
- try:
41
- # is it zip archive?
42
- if isinstance(config_path, str) and config_path.endswith(".zip"):
43
- config_path = str(Path(config_path).expanduser())
44
- return Ok(cls(**yaml.full_load(read_text_from_zip_archive(config_path, password=zip_password))))
45
- if isinstance(config_path, io.TextIOWrapper) and config_path.name.endswith(".zip"):
46
- config_path = str(Path(config_path.name).expanduser())
47
- return Ok(cls(**yaml.full_load(read_text_from_zip_archive(config_path, password=zip_password))))
48
- if isinstance(config_path, Path) and config_path.name.endswith(".zip"):
49
- config_path = str(config_path.expanduser())
50
- return Ok(cls(**yaml.full_load(read_text_from_zip_archive(config_path, password=zip_password))))
51
-
52
- # plain yml file
53
- if isinstance(config_path, str):
54
- return Ok(cls(**yaml.full_load(Path(config_path).expanduser().read_text())))
55
- if isinstance(config_path, Path):
56
- return Ok(cls(**yaml.full_load(config_path.expanduser().read_text())))
57
-
58
- return Ok(cls(**yaml.full_load(config_path)))
59
- except ValidationError as err:
60
- return Err("validator_error", data={"errors": err.errors()})
61
- except Exception as err:
62
- return Err(err)
63
-
64
- @classmethod
65
- def read_config_or_exit[T](cls: type[T], config_path: io.TextIOWrapper | str | Path, zip_password: str = "") -> T: # noqa: PYI019 # nosec
66
- res = cls.read_config(config_path, zip_password) # type: ignore[attr-defined]
21
+ def read_toml_config_or_exit[T](cls: type[T], config_path: Path, zip_password: str = "") -> T: # noqa: PYI019 # nosec
22
+ res = cls.read_toml_config(config_path, zip_password) # type: ignore[attr-defined]
67
23
  if isinstance(res, Ok):
68
24
  return res.unwrap() # type: ignore[no-any-return]
69
25
 
@@ -77,3 +33,18 @@ class BaseConfig(BaseModel):
77
33
  print_plain(f"can't parse config file: {res.err}")
78
34
 
79
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/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: str, filename: str | None = None, password: str | None = None) -> str:
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,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mm-std
3
- Version: 0.1.15
3
+ Version: 0.2.1
4
4
  Requires-Python: >=3.12
5
5
  Requires-Dist: cryptography~=44.0.0
6
6
  Requires-Dist: httpx[http2,socks]~=0.28.1
@@ -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=kbJuNlPfMu5gpQ3SWtMlJAwz-KpE_tiAsm9kb1lOAaA,3254
4
+ mm_std/config.py,sha256=4DGzjLO8TYno1xF8wj2hRaw2-gfNzt_W1_bt7w8ovno,1874
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
@@ -17,7 +17,7 @@ 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=2EXcae4HO5U4kObj2Lj8jl5F2OUpT-WRlJybTyFzt6I,370
21
- mm_std-0.1.15.dist-info/METADATA,sha256=PijQfHjs_xKg4Bcn9qj2oRKcpRBMbhMwz8BGbKsJlUI,307
22
- mm_std-0.1.15.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
23
- mm_std-0.1.15.dist-info/RECORD,,
20
+ mm_std/zip.py,sha256=axzF1BwcIygtfNNTefZH7hXKaQqwe-ZH3ChuRWr9dnk,396
21
+ mm_std-0.2.1.dist-info/METADATA,sha256=S49oqOxFAZ8eehYTZDVdQWsLbURlP8G0Uw1h95WOjCg,306
22
+ mm_std-0.2.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
23
+ mm_std-0.2.1.dist-info/RECORD,,