various-api-tools 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.
@@ -0,0 +1,11 @@
1
+ """A package for various API utility tools.
2
+
3
+ Including JSON and Pydantic error translators.
4
+ """
5
+ from .translators.json import DecodeErrorTranslator
6
+ from .translators.pydantic import ValidationErrorTranslator
7
+
8
+ __all__ = (
9
+ "DecodeErrorTranslator",
10
+ "ValidationErrorTranslator",
11
+ )
@@ -0,0 +1,5 @@
1
+ """Module for importing and exporting JSON and Pydantic error translators.
2
+
3
+ This module provides access to two utility classes that translate common
4
+ validation and parsing errors into user-friendly Russian messages.
5
+ """
@@ -0,0 +1,80 @@
1
+ """Module for translating JSON decoding errors into user-friendly Russian messages.
2
+
3
+ This module provides a utility class that converts Python's `JSONDecodeError`
4
+ exceptions into more descriptive and readable error messages in Russian.
5
+ It is useful for improving the end-user experience when parsing JSON data.
6
+
7
+ """
8
+
9
+ from json import JSONDecodeError
10
+ from typing import Final
11
+
12
+ BASE_JSON_ERROR_MESSAGE: Final[str] = "Ошибка конвертации в формате JSON.\n"
13
+
14
+
15
+ class DecodeErrorTranslator:
16
+ """Translates JSONDecodeError messages into user-friendly Russian descriptions.
17
+
18
+ This class provides a method to convert Python's JSONDecodeError exceptions
19
+ into more readable error messages for end users.
20
+
21
+ """
22
+
23
+ @classmethod
24
+ def translate(
25
+ cls,
26
+ error: JSONDecodeError,
27
+ *,
28
+ msg: str = BASE_JSON_ERROR_MESSAGE,
29
+ line_number: int | None = None,
30
+ ) -> str:
31
+ """Translate a JSONDecodeError into a human-readable message in Russian.
32
+
33
+ Args:
34
+ error: The JSONDecodeError instance to translate.
35
+ msg: Optional base message to prepend. Defaults to BASE_JSON_ERROR_MESSAGE.
36
+ line_number: Optional line number for better context.
37
+
38
+ Returns:
39
+ A string containing the translated error message(position and description).
40
+
41
+ Raises:
42
+ TypeError: If 'error' is not an instance of JSONDecodeError.
43
+
44
+ Example:
45
+ ```python
46
+ try:
47
+ json.loads('{"name": "Alice",}')
48
+ except JSONDecodeError as e:
49
+ print(DecodeErrorTranslator.translate(e))
50
+
51
+ #> Ошибка конвертации в формате JSON.
52
+ #> Позиция: 16.
53
+ #> Описание: не правильно используются двойные кавычки.
54
+ ```
55
+
56
+ """
57
+ if line_number is not None:
58
+ msg += f"Строка: {line_number}, позиция {error.pos}.\n"
59
+ else:
60
+ msg += f"Позиция: {error.pos}.\n"
61
+ exc_msg = error.msg
62
+
63
+ if "Expecting" in exc_msg and "delimiter" in exc_msg:
64
+ msg += "Описание: ожидается разделитель."
65
+ elif "Expecting property name enclosed in double quotes" in exc_msg:
66
+ msg += "Описание: не правильно используются двойные кавычки."
67
+ elif "Expecting value" in exc_msg:
68
+ msg += "Описание: ожидается значение."
69
+ elif "Extra data" in exc_msg:
70
+ msg += "Описание: обнаружены дополнительные данные."
71
+ elif "Invalid" in exc_msg and "escape" in exc_msg:
72
+ msg += "Описание: обнаружены недопустимые экранирующие символы."
73
+ elif "Invalid control character at" in exc_msg:
74
+ msg += "Описание: обнаружен неэкранированный контрольный символ."
75
+ elif "Unterminated string starting at" in exc_msg:
76
+ msg += "Описание: не правильно заверена закрывающая кавычка."
77
+ else:
78
+ msg += "Описание: неизвестная ошибка."
79
+
80
+ return msg
@@ -0,0 +1,130 @@
1
+ """Module for translating Pydantic validation errors into Russian messages.
2
+
3
+ This module provides a utility class to convert Pydantic error details into
4
+ descriptive error messages in Russian, suitable for end-user feedback.
5
+
6
+ """
7
+
8
+ from collections.abc import Sequence
9
+ from typing import Final
10
+
11
+ from pydantic_core import ErrorDetails
12
+
13
+ pydantic_error_types: dict[str, str] = {
14
+ "missing": "Не заполнено обязательное поле",
15
+ "uuid_parsing": "Невалидное значение для UUID",
16
+ "uuid_type": "Невалидное значение для UUID",
17
+ "uuid_version": "Невалидное значение для UUID",
18
+ "bool_parsing": "Невалидное значение для логического типа(bool)",
19
+ "bool_type": "Невалидное значение для логического типа(bool)",
20
+ "date_type": "Невалидное значение даты(date)",
21
+ "datetime_from_date_parsing": "Невалидное значение даты и времени(datetime)",
22
+ "datetime_type": "Невалидное значение даты и времени(datetime)",
23
+ "dict_type": "Невалидное значение словаря",
24
+ "list_type": "Невалидное значение списка",
25
+ "string_type": "Невалидное строковое значение(str)",
26
+ "enum": "Невалидное значение Enum",
27
+ "float_parsing": "Невалидное значение числа с плавающей точкой(float)",
28
+ "float_type": "Невалидное значение числа с плавающей точкой(float)",
29
+ "int_from_float": "Невалидное значение для целочисленного числа(int)",
30
+ "int_parsing": "Невалидное значение для целочисленного числа(int)",
31
+ "int_parsing_size": "Невалидное значение для целочисленного числа(int)",
32
+ "int_type": "Невалидное значение для целочисленного числа(int)",
33
+ "non_negative_int": "Невалидное значение для целочисленного числа(int), "
34
+ "должно быть больше или равно 0",
35
+ "incorrect_latitude": "Некорректное значение широты, должно быть больше, "
36
+ "чем -90.0 и меньше, чем 90.0",
37
+ "incorrect_longitude": "Некорректное значение долготы, должно быть больше, "
38
+ "чем -180.0 и меньше, чем 180.0",
39
+ "string_too_short": "Cтрока слишком короткая",
40
+ "ip_address": "Невалидное значение адреса IPv4/IPv6",
41
+ "incorrect_email": "Невалидное значение email-адреса",
42
+ "list_expected": "Некорректный тип данных. Ожидается список.",
43
+ }
44
+
45
+ DEFAULT_LOCATION_PREFIX: Final[str] = "Поле"
46
+ UNKNOWN_ERROR_TYPE: Final[str] = "Неизвестная ошибка"
47
+
48
+
49
+ class ValidationErrorTranslator:
50
+ """Translate Pydantic validation errors into human-readable Russian messages.
51
+
52
+ This class provides a method to translate a sequence of Pydantic error details
53
+ into a formatted string with location, type and input information.
54
+
55
+ """
56
+
57
+ @classmethod
58
+ def get_str_pydantic_loc(cls, loc: tuple[str]) -> str:
59
+ """Convert a Pydantic location tuple into a dot-separated string.
60
+
61
+ Args:
62
+ loc: A tuple representing the field path in the model.
63
+
64
+ Returns:
65
+ A string representation of the field path.
66
+
67
+ Example:
68
+ ```python
69
+ print(ValidationErrorTranslator.get_str_pydantic_loc(("user", "name")))
70
+ #> "user.name"
71
+ ```
72
+
73
+ """
74
+ return ".".join([str(loc_value) for loc_value in loc])
75
+
76
+ @classmethod
77
+ def translate(cls, errors: Sequence[ErrorDetails]) -> str:
78
+ """Translate a list of Pydantic error details into a human-readable message.
79
+
80
+ Args:
81
+ errors: A sequence of Pydantic ErrorDetails objects.
82
+
83
+ Returns:
84
+ A formatted string containing all translated errors.
85
+
86
+ Example:
87
+ ```python
88
+ class Model(BaseModel):
89
+ email: str
90
+
91
+ try:
92
+ Model(email=1)
93
+ except ValidationError as exc:
94
+ print(ValidationErrorTranslator.translate(exc.errors())
95
+
96
+ #> Поле: "email". Ошибка: "Невалидное строковое значение(str)";
97
+ ```
98
+
99
+ """
100
+ formatted_errors: list[str] = []
101
+ for error in errors:
102
+ error_str: str = ""
103
+
104
+ location_part: str = ""
105
+ type_part: str = ""
106
+ input_part: str = ""
107
+
108
+ if "loc" in error:
109
+ prefix: str = DEFAULT_LOCATION_PREFIX
110
+ if error["loc"][0] == "query":
111
+ prefix = "Параметр запроса"
112
+ location_part = (
113
+ f'{prefix}: "{cls.get_str_pydantic_loc(loc=error["loc"])}"'
114
+ )
115
+
116
+ if "type" in error:
117
+ msg = pydantic_error_types.get(error["type"], UNKNOWN_ERROR_TYPE)
118
+ type_part = f'Ошибка: "{msg}"'
119
+
120
+ if "input" in error and pydantic_error_types["missing"] not in type_part:
121
+ input_part = f'заполнено неверно: "{error["input"]!r}"'
122
+
123
+ if input_part != "":
124
+ error_str = f"{location_part} {input_part}. {type_part};"
125
+ else:
126
+ error_str = f"{location_part}. {type_part};"
127
+
128
+ formatted_errors.append(error_str)
129
+
130
+ return "\n".join(formatted_errors)
@@ -0,0 +1,99 @@
1
+ Metadata-Version: 2.1
2
+ Name: various-api-tools
3
+ Version: 0.2.1
4
+ Summary: A lightweight utility package for common API-related tasks in Python, including JSON and Pydantic error translators that provide user-friendly Russian messages.
5
+ Author-Email: dkurchigin <kurchigin.dmitry@yandex.ru>
6
+ License: MIT
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Programming Language :: Python
9
+ Classifier: Typing :: Typed
10
+ Classifier: Programming Language :: Python :: 3 :: Only
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Project-URL: Homepage, https://gitverse.ru/dkurchigin/various-api-tools
16
+ Project-URL: Documentation, https://various-api-tools.dkurchigin.ru/
17
+ Project-URL: Source, https://gitverse.ru/dkurchigin/various-api-tools
18
+ Requires-Python: >=3.10
19
+ Requires-Dist: pydantic>=2.11.7
20
+ Description-Content-Type: text/markdown
21
+
22
+ # various_api_tools
23
+
24
+ A lightweight utility package for common API-related tasks in Python, including JSON and Pydantic error translators that provide user-friendly Russian messages.
25
+
26
+ ---
27
+
28
+ ## 📦 Features
29
+
30
+ - ✅ **JSON Error Translator** – Translates `JSONDecodeError` into clear Russian messages.
31
+ - ✅ **Pydantic Validation Error Translator** – Converts Pydantic validation errors into human-readable Russian strings.
32
+ - ✅ **Easy to integrate** – Designed for use in web APIs and data validation pipelines.
33
+
34
+ ---
35
+
36
+ ## 🐍 Installation
37
+
38
+ Install using pip from source or a private repository:
39
+
40
+ ```bash
41
+ pip install various_api_tools
42
+ ```
43
+
44
+ ## 🧪 Basic Usage
45
+
46
+ ### Translate JSON Decode Errors
47
+
48
+ ```python
49
+ import json
50
+ from various_api_tools.translators.json import DecodeErrorTranslator
51
+
52
+ try:
53
+ json.loads('{"name": "Alice",}')
54
+ except json.JSONDecodeError as e:
55
+ print(DecodeErrorTranslator.translate(e))
56
+
57
+ # Output:
58
+ # Ошибка конвертации в формате JSON.
59
+ # Позиция: 16.
60
+ # Описание: не правильно используются двойные кавычки.
61
+ ```
62
+
63
+ ---
64
+
65
+ ### Translate Pydantic Validation Errors
66
+
67
+ ```python
68
+ from pydantic import BaseModel, ValidationError
69
+ from various_api_tools.translators.pydantic import ValidationErrorTranslator
70
+
71
+ class User(BaseModel):
72
+ email: str
73
+
74
+ try:
75
+ User(email=123)
76
+ except ValidationError as e:
77
+ print(ValidationErrorTranslator.translate(e.errors()))
78
+
79
+ # Output:
80
+ # Поле: "email". Ошибка: "Невалидное строковое значение(str)";
81
+ ```
82
+
83
+ ---
84
+
85
+ ## 📄 License
86
+
87
+ MIT License — feel free to use it in any project! 🎉
88
+
89
+ ---
90
+
91
+ ## 🧑‍💻 Made with ❤️ by [@dkurchigin](https://gitverse.ru/dkurchigin)
92
+
93
+ Have questions? Open an issue or contribute to the repo!
94
+
95
+
96
+ ## 🐙 GITVERSE
97
+
98
+
99
+ 🔗 [https://gitverse.ru/dkurchigin/various-api-tools](https://gitverse.ru/dkurchigin/various-api-tools)
@@ -0,0 +1,8 @@
1
+ various_api_tools-0.2.1.dist-info/METADATA,sha256=uFPuvWCUbIR7caXA1sLgwYf2xRQOhy18asGOW4Ug9wQ,2899
2
+ various_api_tools-0.2.1.dist-info/WHEEL,sha256=9P2ygRxDrTJz3gsagc0Z96ukrxjr-LFBGOgv3AuKlCA,90
3
+ various_api_tools-0.2.1.dist-info/entry_points.txt,sha256=6OYgBcLyFCUgeqLgnvMyOJxPCWzgy7se4rLPKtNonMs,34
4
+ various_api_tools/__init__.py,sha256=-NZqqXsqmRrPlaPRLX-1v1PuVaXZXU69CP0cl6rBd-c,285
5
+ various_api_tools/translators/__init__.py,sha256=v2bJFou-sb1R7PhkPOSJ2de8XSEyvft_X73oknoaMok,220
6
+ various_api_tools/translators/json.py,sha256=iMLW0odm1pj2hR3YSkPv2y545zB08nLTSYILP0GsPBk,3363
7
+ various_api_tools/translators/pydantic.py,sha256=lUKqRMFQlslqG5IRxowrEvg8pX3RMNiB5mJbWj2n4Zs,5826
8
+ various_api_tools-0.2.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: pdm-backend (2.4.5)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,4 @@
1
+ [console_scripts]
2
+
3
+ [gui_scripts]
4
+