mm-std 0.4.3__py3-none-any.whl → 0.4.4__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,7 +1,7 @@
1
1
  import sys
2
2
  import tomllib
3
3
  from pathlib import Path
4
- from typing import NoReturn, Self
4
+ from typing import Any, NoReturn, Self, TypeVar
5
5
 
6
6
  from pydantic import BaseModel, ConfigDict, ValidationError
7
7
 
@@ -9,6 +9,8 @@ from .print_ import print_json, print_plain
9
9
  from .result import Result
10
10
  from .zip import read_text_from_zip_archive
11
11
 
12
+ T = TypeVar("T", bound="BaseConfig")
13
+
12
14
 
13
15
  class BaseConfig(BaseModel):
14
16
  model_config = ConfigDict(extra="forbid")
@@ -18,21 +20,18 @@ class BaseConfig(BaseModel):
18
20
  sys.exit(0)
19
21
 
20
22
  @classmethod
21
- def read_toml_config_or_exit[T](cls: type[T], config_path: Path, zip_password: str = "") -> T: # noqa: PYI019 # nosec
22
- res: Result[T] = cls.read_toml_config(config_path, zip_password) # type:ignore[attr-defined]
23
+ def read_toml_config_or_exit(cls, config_path: Path, zip_password: str = "") -> Self: # nosec
24
+ res: Result[Self] = cls.read_toml_config(config_path, zip_password)
23
25
  if res.is_ok():
24
26
  return res.unwrap()
27
+ cls._print_error_and_exit(res)
25
28
 
26
- if res.error == "validator_error" and res.extra:
27
- print_plain("config validation errors")
28
- for e in res.extra["errors"]:
29
- loc = e["loc"]
30
- field = ".".join(str(lo) for lo in loc) if len(loc) > 0 else ""
31
- print_plain(f"{field} {e['msg']}")
32
- else:
33
- print_plain(f"can't parse config file: {res.error}")
34
-
35
- sys.exit(1)
29
+ @classmethod
30
+ async def read_toml_config_or_exit_async(cls, config_path: Path, zip_password: str = "") -> Self: # nosec
31
+ res: Result[Self] = await cls.read_toml_config_async(config_path, zip_password)
32
+ if res.is_ok():
33
+ return res.unwrap()
34
+ cls._print_error_and_exit(res)
36
35
 
37
36
  @classmethod
38
37
  def read_toml_config(cls, config_path: Path, zip_password: str = "") -> Result[Self]: # nosec
@@ -48,3 +47,31 @@ class BaseConfig(BaseModel):
48
47
  return Result.failure(("validator_error", e), extra={"errors": e.errors()})
49
48
  except Exception as e:
50
49
  return Result.failure(e)
50
+
51
+ @classmethod
52
+ async def read_toml_config_async(cls, config_path: Path, zip_password: str = "") -> Result[Self]: # nosec
53
+ try:
54
+ config_path = config_path.expanduser()
55
+ if config_path.name.endswith(".zip"):
56
+ data = tomllib.loads(read_text_from_zip_archive(config_path, password=zip_password))
57
+ else:
58
+ with config_path.open("rb") as f:
59
+ data = tomllib.load(f)
60
+ model = await cls.model_validate_async(data) # type:ignore[attr-defined]
61
+ return Result.success(model)
62
+ except ValidationError as e:
63
+ return Result.failure(("validator_error", e), extra={"errors": e.errors()})
64
+ except Exception as e:
65
+ return Result.failure(e)
66
+
67
+ @classmethod
68
+ def _print_error_and_exit(cls, res: Result[Any]) -> NoReturn:
69
+ if res.error == "validator_error" and res.extra:
70
+ print_plain("config validation errors")
71
+ for e in res.extra["errors"]:
72
+ loc = e["loc"]
73
+ field = ".".join(str(lo) for lo in loc) if len(loc) > 0 else ""
74
+ print_plain(f"{field} {e['msg']}")
75
+ else:
76
+ print_plain(f"can't parse config file: {res.error}")
77
+ sys.exit(1)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mm-std
3
- Version: 0.4.3
3
+ Version: 0.4.4
4
4
  Requires-Python: >=3.12
5
5
  Requires-Dist: aiohttp-socks~=0.10.1
6
6
  Requires-Dist: aiohttp~=3.11.16
@@ -1,6 +1,6 @@
1
1
  mm_std/__init__.py,sha256=gSMy_KY9kffPFnrDzdUti_JiQP1hB3ZtF7GxhOCc1Vc,2814
2
2
  mm_std/command.py,sha256=ze286wjUjg0QSTgIu-2WZks53_Vclg69UaYYgPpQvCU,1283
3
- mm_std/config.py,sha256=QtF-SCzaHZf511miTDjmIWR2dFfVPsnw2x6B5rmhMMk,1884
3
+ mm_std/config.py,sha256=KArBvdt1gr_YvAtdXA4bqzF5W3IyUh25TexFoJCcN-E,3094
4
4
  mm_std/crypto.py,sha256=jdk0_TCmeU0pPXMyz9xH6kQHSjjZ9GcGClBwQps5vBo,340
5
5
  mm_std/date.py,sha256=976eEkSONuNqHQBgSRu8hrtH23tJqztbmHFHLdbP2TY,1879
6
6
  mm_std/dict.py,sha256=6GkhJPXD0LiJDxPcYe6jPdEDw-MN7P7mKu6U5XxwYDk,675
@@ -28,6 +28,6 @@ mm_std/http/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
28
28
  mm_std/http/http_request.py,sha256=VnjZKrfaXQfMxrHRUqQ-Sxtr5Qf9FXBiJ-mmJTCzNkY,3709
29
29
  mm_std/http/http_request_sync.py,sha256=bqCBilbe4ZJ9vkhuBQeU5UMTJh6BtvtUwjieEodu6rw,1542
30
30
  mm_std/http/http_response.py,sha256=gz4kCCL0qr7wvw4NNiK1K2hXlrY0B0Iq4fJuYSrv3bw,3920
31
- mm_std-0.4.3.dist-info/METADATA,sha256=JSvnB6eXS5YrbuHr15igF-YOvHK7_YD_1bPP4was71k,446
32
- mm_std-0.4.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
33
- mm_std-0.4.3.dist-info/RECORD,,
31
+ mm_std-0.4.4.dist-info/METADATA,sha256=zQdcBSHWHf-504aqEWMZzK84MwT6stxPdvRryHjymZ4,446
32
+ mm_std-0.4.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
33
+ mm_std-0.4.4.dist-info/RECORD,,
File without changes