bear-utils 0.7.23__tar.gz → 0.7.24__tar.gz

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.
Files changed (107) hide show
  1. {bear_utils-0.7.23 → bear_utils-0.7.24}/.bumpversion.cfg +1 -1
  2. {bear_utils-0.7.23 → bear_utils-0.7.24}/PKG-INFO +2 -2
  3. {bear_utils-0.7.23 → bear_utils-0.7.24}/README.md +1 -1
  4. {bear_utils-0.7.23 → bear_utils-0.7.24}/noxfile.py +17 -0
  5. {bear_utils-0.7.23 → bear_utils-0.7.24}/pyproject.toml +3 -1
  6. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/extras/responses/function_response.py +73 -38
  7. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/files/file_handlers/_base_file_handler.py +3 -9
  8. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/files/file_handlers/json_file_handler.py +2 -2
  9. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/files/file_handlers/log_file_handler.py +2 -2
  10. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/files/file_handlers/toml_file_handler.py +1 -1
  11. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/files/file_handlers/txt_file_handler.py +2 -2
  12. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/files/file_handlers/yaml_file_handler.py +1 -0
  13. bear_utils-0.7.24/tests/test_function_response.py +560 -0
  14. {bear_utils-0.7.23 → bear_utils-0.7.24}/uv.lock +61 -1
  15. {bear_utils-0.7.23 → bear_utils-0.7.24}/.gitignore +0 -0
  16. {bear_utils-0.7.23 → bear_utils-0.7.24}/.python-version +0 -0
  17. {bear_utils-0.7.23 → bear_utils-0.7.24}/AGENTS.md +0 -0
  18. {bear_utils-0.7.23 → bear_utils-0.7.24}/config/coverage.ini +0 -0
  19. {bear_utils-0.7.23 → bear_utils-0.7.24}/config/default.toml +0 -0
  20. {bear_utils-0.7.23 → bear_utils-0.7.24}/config/git-changelog.toml +0 -0
  21. {bear_utils-0.7.23 → bear_utils-0.7.24}/config/pytest.ini +0 -0
  22. {bear_utils-0.7.23 → bear_utils-0.7.24}/config/ruff.toml +0 -0
  23. {bear_utils-0.7.23 → bear_utils-0.7.24}/config/vscode/launch.json +0 -0
  24. {bear_utils-0.7.23 → bear_utils-0.7.24}/config/vscode/settings.json +0 -0
  25. {bear_utils-0.7.23 → bear_utils-0.7.24}/config/vscode/tasks.json +0 -0
  26. {bear_utils-0.7.23 → bear_utils-0.7.24}/directory_structure.txt +0 -0
  27. {bear_utils-0.7.23 → bear_utils-0.7.24}/maskfile.md +0 -0
  28. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/__init__.py +0 -0
  29. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/ai/__init__.py +0 -0
  30. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/ai/ai_helpers/__init__.py +0 -0
  31. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/ai/ai_helpers/_common.py +0 -0
  32. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/ai/ai_helpers/_config.py +0 -0
  33. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/ai/ai_helpers/_parsers.py +0 -0
  34. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/ai/ai_helpers/_types.py +0 -0
  35. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/cache/__init__.py +0 -0
  36. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/cli/__init__.py +0 -0
  37. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/cli/commands.py +0 -0
  38. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/cli/prompt_helpers.py +0 -0
  39. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/cli/shell/__init__.py +0 -0
  40. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/cli/shell/_base_command.py +0 -0
  41. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/cli/shell/_base_shell.py +0 -0
  42. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/cli/shell/_common.py +0 -0
  43. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/config/__init__.py +0 -0
  44. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/config/config_manager.py +0 -0
  45. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/config/dir_manager.py +0 -0
  46. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/config/settings_manager.py +0 -0
  47. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/constants/__init__.py +0 -0
  48. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/constants/_exceptions.py +0 -0
  49. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/constants/_lazy_typing.py +0 -0
  50. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/constants/date_related.py +0 -0
  51. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/constants/logger_protocol.py +0 -0
  52. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/constants/time_related.py +0 -0
  53. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/database/__init__.py +0 -0
  54. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/database/_db_manager.py +0 -0
  55. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/events/__init__.py +0 -0
  56. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/events/events_class.py +0 -0
  57. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/events/events_module.py +0 -0
  58. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/extras/__init__.py +0 -0
  59. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/extras/_async_helpers.py +0 -0
  60. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/extras/_tools.py +0 -0
  61. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/extras/platform_utils.py +0 -0
  62. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/extras/responses/__init__.py +0 -0
  63. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/extras/wrappers/__init__.py +0 -0
  64. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/extras/wrappers/add_methods.py +0 -0
  65. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/files/__init__.py +0 -0
  66. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/files/file_handlers/__init__.py +0 -0
  67. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/files/file_handlers/file_handler_factory.py +0 -0
  68. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/files/ignore_parser.py +0 -0
  69. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/graphics/__init__.py +0 -0
  70. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/graphics/bear_gradient.py +0 -0
  71. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/graphics/image_helpers.py +0 -0
  72. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/gui/__init__.py +0 -0
  73. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/gui/gui_tools/__init__.py +0 -0
  74. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/gui/gui_tools/_settings.py +0 -0
  75. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/gui/gui_tools/_types.py +0 -0
  76. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/gui/gui_tools/qt_app.py +0 -0
  77. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/gui/gui_tools/qt_color_picker.py +0 -0
  78. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/gui/gui_tools/qt_file_handler.py +0 -0
  79. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/gui/gui_tools/qt_input_dialog.py +0 -0
  80. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/__init__.py +0 -0
  81. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/__init__.py +0 -0
  82. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/_common.py +0 -0
  83. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/_console_junk.py +0 -0
  84. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/_styles.py +0 -0
  85. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/loggers/__init__.py +0 -0
  86. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/loggers/_base_logger.py +0 -0
  87. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/loggers/_base_logger.pyi +0 -0
  88. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/loggers/_buffer_logger.py +0 -0
  89. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/loggers/_console_logger.py +0 -0
  90. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/loggers/_console_logger.pyi +0 -0
  91. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/loggers/_file_logger.py +0 -0
  92. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/loggers/_level_sin.py +0 -0
  93. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/loggers/_logger.py +0 -0
  94. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/loggers/_sub_logger.py +0 -0
  95. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/logger_manager/loggers/_sub_logger.pyi +0 -0
  96. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/logging/loggers.py +0 -0
  97. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/monitoring/__init__.py +0 -0
  98. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/monitoring/_common.py +0 -0
  99. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/monitoring/host_monitor.py +0 -0
  100. {bear_utils-0.7.23 → bear_utils-0.7.24}/src/bear_utils/time/__init__.py +0 -0
  101. {bear_utils-0.7.23 → bear_utils-0.7.24}/tests/__init__.py +0 -0
  102. {bear_utils-0.7.23 → bear_utils-0.7.24}/tests/test_add_ord_suffix.py +0 -0
  103. {bear_utils-0.7.23 → bear_utils-0.7.24}/tests/test_clipboard.py +0 -0
  104. {bear_utils-0.7.23 → bear_utils-0.7.24}/tests/test_default_shell.py +0 -0
  105. {bear_utils-0.7.23 → bear_utils-0.7.24}/tests/test_gradient.py +0 -0
  106. {bear_utils-0.7.23 → bear_utils-0.7.24}/tests/test_logger.py +0 -0
  107. {bear_utils-0.7.23 → bear_utils-0.7.24}/tests/test_platform_utils.py +0 -0
@@ -1,5 +1,5 @@
1
1
  [bumpversion]
2
- current_version = 0.7.23
2
+ current_version = 0.7.24
3
3
 
4
4
  [bumpversion:file:pyproject.toml]
5
5
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bear-utils
3
- Version: 0.7.23
3
+ Version: 0.7.24
4
4
  Summary: Various utilities for Bear programmers, including a rich logging utility, a disk cache, and a SQLite database wrapper amongst other things.
5
5
  Author-email: chaz <bright.lid5647@fastmail.com>
6
6
  Requires-Python: >=3.12
@@ -20,7 +20,7 @@ Requires-Dist: tinydb>=4.8.2
20
20
  Requires-Dist: toml>=0.10.2
21
21
  Description-Content-Type: text/markdown
22
22
 
23
- # Bear Utils v# Bear Utils v0.7.23
23
+ # Bear Utils v# Bear Utils v0.7.24
24
24
 
25
25
  Personal set of tools and utilities for Python projects, focusing on modularity and ease of use. This library includes components for caching, database management, logging, time handling, file operations, CLI prompts, image processing, clipboard interaction, gradient utilities, event systems, and async helpers.
26
26
 
@@ -1,4 +1,4 @@
1
- # Bear Utils v# Bear Utils v0.7.23
1
+ # Bear Utils v# Bear Utils v0.7.24
2
2
 
3
3
  Personal set of tools and utilities for Python projects, focusing on modularity and ease of use. This library includes components for caching, database management, logging, time handling, file operations, CLI prompts, image processing, clipboard interaction, gradient utilities, event systems, and async helpers.
4
4
 
@@ -5,6 +5,23 @@ import nox
5
5
  PYTHON_VERSIONS: list[str] = ["3.13", "3.14"]
6
6
 
7
7
 
8
+ @nox.session(venv_backend="uv", tags=["lint", "fix"])
9
+ def coverage(session: nox.Session) -> None:
10
+ """Run tests with coverage reporting."""
11
+ session.install("coverage[toml]", "pytest", "pytest-cov")
12
+ session.install("-e", ".")
13
+
14
+ session.run(
15
+ "pytest",
16
+ "--cov=src",
17
+ "--cov-report=term-missing",
18
+ "--cov-report=html",
19
+ "--cov-config=config/coverage.ini",
20
+ )
21
+
22
+ session.log("Coverage HTML report generated in htmlcov/")
23
+
24
+
8
25
  @nox.session(venv_backend="uv", tags=["lint"])
9
26
  def ruff_check(session: nox.Session) -> None:
10
27
  """Run ruff linting and formatting checks (CI-friendly, no changes)."""
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "bear-utils"
3
- version = "0.7.23"
3
+ version = "0.7.24"
4
4
  description = "Various utilities for Bear programmers, including a rich logging utility, a disk cache, and a SQLite database wrapper amongst other things."
5
5
  authors = [{ name = "chaz", email = "bright.lid5647@fastmail.com" }]
6
6
  readme = "README.md"
@@ -29,9 +29,11 @@ build-backend = "hatchling.build"
29
29
  [dependency-groups]
30
30
  dev = [
31
31
  "bump2version>=1.0.1",
32
+ "coverage>=7.9.2",
32
33
  "isort>=6.0.1",
33
34
  "nox>=2025.5.1",
34
35
  "pytest>=8.3.5",
36
+ "pytest-cov>=6.2.1",
35
37
  "twine>=6.1.0",
36
38
  ]
37
39
  gui = [
@@ -5,12 +5,11 @@ from __future__ import annotations
5
5
  from io import StringIO
6
6
  import json
7
7
  from subprocess import CompletedProcess
8
- from typing import TYPE_CHECKING, Any, Literal, Self, overload
8
+ from typing import Any, Literal, Self, overload
9
9
 
10
10
  from pydantic import BaseModel, Field, field_validator
11
11
 
12
- if TYPE_CHECKING:
13
- from bear_utils.constants.logger_protocol import LoggerProtocol
12
+ from bear_utils.constants.logger_protocol import LoggerProtocol # noqa: TC001 # DO NOT PUT INTO A TYPE_CHECKING BLOCK
14
13
 
15
14
  SUCCESS: list[str] = ["name", "success"]
16
15
  FAILURE: list[str] = ["name"]
@@ -130,6 +129,25 @@ class FunctionResponse(BaseModel):
130
129
  """Check if the response indicates success."""
131
130
  return self.returncode == 0
132
131
 
132
+ def sub_task(
133
+ self,
134
+ name: str = "",
135
+ content: str | list[str] = "",
136
+ error: str | list[str] = "",
137
+ extra: dict[str, Any] | None = None,
138
+ returncode: int | None = None,
139
+ log_output: bool = False,
140
+ ) -> None:
141
+ """Add a sub-task response to the FunctionResponse."""
142
+ func_response: FunctionResponse = FunctionResponse(name=name, logger=self.logger).add(
143
+ content=content,
144
+ error=error,
145
+ returncode=returncode or self.returncode,
146
+ log_output=log_output,
147
+ extra=extra,
148
+ )
149
+ self.add(content=func_response)
150
+
133
151
  def successful(
134
152
  self,
135
153
  content: str | list[str] | CompletedProcess,
@@ -184,6 +202,19 @@ class FunctionResponse(BaseModel):
184
202
  except Exception as e:
185
203
  raise ValueError(f"Failed to add content: {e!s}") from e
186
204
 
205
+ def _handle_function_response(self, func_response: FunctionResponse) -> None:
206
+ """Handle a FunctionResponse object and update the current response."""
207
+ if func_response.extra:
208
+ self.extra.update(func_response.extra)
209
+ self._add_to_error(error=func_response.error, name=func_response.name)
210
+ self._add_to_content(content=func_response.content, name=func_response.name)
211
+
212
+ def _handle_completed_process(self, result: CompletedProcess[str]) -> None:
213
+ """Handle a CompletedProcess object and update the FunctionResponse."""
214
+ self._add_to_content(content=result.stdout.strip() if result.stdout else "")
215
+ self._add_to_error(error=result.stderr.strip() if result.stderr else "")
216
+ self.returncode = result.returncode
217
+
187
218
  def add(
188
219
  self,
189
220
  content: list[str] | str | FunctionResponse | CompletedProcess | None = None,
@@ -205,45 +236,49 @@ class FunctionResponse(BaseModel):
205
236
  Self: The updated FunctionResponse instance.
206
237
  """
207
238
  try:
208
- if isinstance(content, FunctionResponse):
209
- if content.extra:
210
- self.extra.update(content.extra)
211
- self._add_to_error(error=content.error, name=content.name)
212
- self._add_to_content(content=content.content, name=content.name)
213
- self.number_of_tasks += 1
214
- elif isinstance(content, CompletedProcess):
215
- result: CompletedProcess[str] = content
216
- self._add_to_content(content=result.stdout.strip() if result.stdout else "")
217
- self._add_to_error(error=result.stderr.strip() if result.stderr else "")
218
- self.returncode = result.returncode
219
- self.number_of_tasks += 1
220
- elif isinstance(content, str | list):
221
- self._add_to_content(content=content)
222
- self.number_of_tasks += 1
223
- if isinstance(error, str | list):
224
- self._add_to_error(error=error)
225
- if returncode is not None:
226
- self.returncode = returncode
227
- if extra is not None and isinstance(extra, dict):
228
- self.extra.update(extra)
229
- if log_output and self.logger is not None:
230
- if content is not None and error is None:
231
- if isinstance(content, list):
232
- for item in content:
233
- self.logger.info(message=f"{self.name}: {item}" if self.name else item)
234
- elif isinstance(content, str):
235
- self.logger.info(message=f"{self.name}: {content}" if self.name else content)
236
- elif error is not None and content is None:
237
- if isinstance(error, list):
238
- for err in error:
239
- self.logger.error(message=f"{self.name}: {err}" if self.name else err)
240
- elif isinstance(error, str):
241
- self.logger.error(message=f"{self.name}: {error}" if self.name else error)
242
-
239
+ match content:
240
+ case FunctionResponse():
241
+ self._handle_function_response(func_response=content)
242
+ self.number_of_tasks += 1
243
+ case CompletedProcess():
244
+ self._handle_completed_process(result=content)
245
+ self.number_of_tasks += 1
246
+ case str() | list() if content:
247
+ self._add_to_content(content=content)
248
+ self.number_of_tasks += 1
249
+ case None:
250
+ content = None
251
+ case _:
252
+ content = None
253
+ self._add_to_error(error=error) if isinstance(error, (str | list)) else None
254
+ self.returncode = returncode if returncode is not None else self.returncode
255
+ self.extra.update(extra) if isinstance(extra, dict) else None
256
+ if log_output and self.logger is not None and (content is not None or error is not None):
257
+ self._log_handling(content=content, error=error, logger=self.logger)
243
258
  except Exception as e:
244
259
  raise ValueError(f"Failed to add content: {e!s}") from e
245
260
  return self
246
261
 
262
+ def _log_handling(
263
+ self,
264
+ content: list[str] | str | FunctionResponse | CompletedProcess | None,
265
+ error: str | list[str] | None,
266
+ logger: LoggerProtocol,
267
+ ) -> None:
268
+ """Log the content and error messages if they exist."""
269
+ if content is not None and error is None:
270
+ if isinstance(content, list):
271
+ for item in content:
272
+ logger.info(message=f"{self.name}: {item}" if self.name else item)
273
+ elif isinstance(content, str):
274
+ logger.info(message=f"{self.name}: {content}" if self.name else content)
275
+ elif error is not None and content is None:
276
+ if isinstance(error, list):
277
+ for err in error:
278
+ logger.error(message=f"{self.name}: {err}" if self.name else err)
279
+ elif isinstance(error, str):
280
+ logger.error(message=f"{self.name}: {error}" if self.name else error)
281
+
247
282
  @overload
248
283
  def done(self, to_dict: Literal[True], suppress: list[str] | None = None) -> dict[str, Any]: ...
249
284
 
@@ -8,12 +8,6 @@ P = ParamSpec("P")
8
8
  R = TypeVar("R")
9
9
 
10
10
 
11
- def check_data_type(data: dict[str, Any] | str, valid_types: tuple[type, ...]) -> None:
12
- """Check if the data is of a valid type for text files."""
13
- if not isinstance(data, valid_types):
14
- raise TypeError(f"Data must be one of {valid_types}, got {type(data)}")
15
-
16
-
17
11
  class FileHandler(ABC):
18
12
  """Abstract class for file handling with read, write, and present methods
19
13
 
@@ -40,7 +34,7 @@ class FileHandler(ABC):
40
34
  return file_path.suffix.lstrip(".") in cls.valid_extensions
41
35
 
42
36
  @classmethod
43
- def check_data_type(cls, data: Any) -> Any:
37
+ def check_data_type(cls, data: dict[str, Any] | str, valid_types: tuple[type, ...]) -> None:
44
38
  """Check if the data is of the correct type.
45
39
 
46
40
  Args:
@@ -49,8 +43,8 @@ class FileHandler(ABC):
49
43
  Returns:
50
44
  bool: True if the data is of the correct type, False otherwise
51
45
  """
52
- if not isinstance(data, cls.valid_types):
53
- raise TypeError(f"Data must be one of {cls.valid_types}, got {type(data)}")
46
+ if not isinstance(data, valid_types):
47
+ raise TypeError(f"Data must be one of {valid_types}, got {type(data)}")
54
48
 
55
49
  @classmethod
56
50
  def ValidateFileType(cls, method: Callable[P, R]) -> Callable[P, R]: # noqa: N802 disable=invalid-name
@@ -4,7 +4,7 @@ import json
4
4
  from pathlib import Path
5
5
  from typing import Any, ClassVar, overload
6
6
 
7
- from ._base_file_handler import FileHandler, check_data_type
7
+ from ._base_file_handler import FileHandler
8
8
 
9
9
 
10
10
  class JsonFileHandler(FileHandler):
@@ -44,7 +44,7 @@ class JsonFileHandler(FileHandler):
44
44
  """Write data to a JSON file."""
45
45
  try:
46
46
  super().write_file(file_path=file_path, data=data)
47
- check_data_type(data=data, valid_types=self.valid_types)
47
+ self.check_data_type(data=data, valid_types=self.valid_types)
48
48
  data = self.present_file(data, indent=indent, sort_keys=sort_keys)
49
49
  file_path.write_text(data=data, encoding="utf-8")
50
50
  except Exception as e:
@@ -3,7 +3,7 @@
3
3
  from pathlib import Path
4
4
  from typing import Any, ClassVar, cast
5
5
 
6
- from ._base_file_handler import FileHandler, check_data_type
6
+ from ._base_file_handler import FileHandler
7
7
 
8
8
 
9
9
  class LogFileHandler(FileHandler):
@@ -27,7 +27,7 @@ class LogFileHandler(FileHandler):
27
27
  """Write data to a log file."""
28
28
  try:
29
29
  super().write_file(file_path=file_path, data=data)
30
- check_data_type(data=data, valid_types=self.valid_types)
30
+ self.check_data_type(data=data, valid_types=self.valid_types)
31
31
  with open(file_path, "w", encoding="utf-8") as file:
32
32
  file.write(cast("str", data))
33
33
  except Exception as e:
@@ -30,7 +30,7 @@ class TomlFileHandler(FileHandler):
30
30
  """Write data to a TOML file."""
31
31
  try:
32
32
  super().write_file(file_path=file_path, data=data)
33
-
33
+ self.check_data_type(data=data, valid_types=self.valid_types)
34
34
  with open(file_path, "w", encoding="utf-8") as file:
35
35
  if isinstance(data, dict):
36
36
  toml.dump(data, file, **kwargs)
@@ -4,7 +4,7 @@ import json
4
4
  from pathlib import Path
5
5
  from typing import Any, ClassVar, overload
6
6
 
7
- from ._base_file_handler import FileHandler, check_data_type
7
+ from ._base_file_handler import FileHandler
8
8
 
9
9
 
10
10
  class TextFileHandler(FileHandler):
@@ -44,7 +44,7 @@ class TextFileHandler(FileHandler):
44
44
  """Write data to a text file."""
45
45
  try:
46
46
  super().write_file(file_path=file_path, data=data)
47
- check_data_type(data, self.valid_types)
47
+ self.check_data_type(data=data, valid_types=self.valid_types)
48
48
  if isinstance(data, dict):
49
49
  data = json.dumps(data, indent=indent, sort_keys=sort_keys)
50
50
  elif isinstance(data, list):
@@ -50,6 +50,7 @@ class YamlFileHandler(FileHandler):
50
50
  """Write data to a YAML file."""
51
51
  try:
52
52
  super().write_file(file_path=file_path, data=data)
53
+ self.check_data_type(data=data, valid_types=self.valid_types)
53
54
  with open(file=file_path, mode="w", encoding="utf-8") as file:
54
55
  yaml.dump(data, stream=file, default_flow_style=False, sort_keys=False, **kwargs)
55
56
  except Exception as e: