winipedia-utils 0.1.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.
Files changed (64) hide show
  1. winipedia_utils/__init__.py +1 -0
  2. winipedia_utils/concurrent/__init__.py +1 -0
  3. winipedia_utils/concurrent/concurrent.py +242 -0
  4. winipedia_utils/concurrent/multiprocessing.py +115 -0
  5. winipedia_utils/concurrent/multithreading.py +93 -0
  6. winipedia_utils/consts.py +22 -0
  7. winipedia_utils/data/__init__.py +1 -0
  8. winipedia_utils/data/dataframe.py +7 -0
  9. winipedia_utils/django/__init__.py +27 -0
  10. winipedia_utils/django/bulk.py +536 -0
  11. winipedia_utils/django/command.py +334 -0
  12. winipedia_utils/django/database.py +304 -0
  13. winipedia_utils/git/__init__.py +1 -0
  14. winipedia_utils/git/gitignore.py +80 -0
  15. winipedia_utils/git/pre_commit/__init__.py +1 -0
  16. winipedia_utils/git/pre_commit/config.py +60 -0
  17. winipedia_utils/git/pre_commit/hooks.py +109 -0
  18. winipedia_utils/git/pre_commit/run_hooks.py +49 -0
  19. winipedia_utils/iterating/__init__.py +1 -0
  20. winipedia_utils/iterating/iterate.py +29 -0
  21. winipedia_utils/logging/__init__.py +1 -0
  22. winipedia_utils/logging/ansi.py +6 -0
  23. winipedia_utils/logging/config.py +64 -0
  24. winipedia_utils/logging/logger.py +26 -0
  25. winipedia_utils/modules/__init__.py +1 -0
  26. winipedia_utils/modules/class_.py +76 -0
  27. winipedia_utils/modules/function.py +86 -0
  28. winipedia_utils/modules/module.py +361 -0
  29. winipedia_utils/modules/package.py +350 -0
  30. winipedia_utils/oop/__init__.py +1 -0
  31. winipedia_utils/oop/mixins/__init__.py +1 -0
  32. winipedia_utils/oop/mixins/meta.py +315 -0
  33. winipedia_utils/oop/mixins/mixin.py +28 -0
  34. winipedia_utils/os/__init__.py +1 -0
  35. winipedia_utils/os/os.py +61 -0
  36. winipedia_utils/projects/__init__.py +1 -0
  37. winipedia_utils/projects/poetry/__init__.py +1 -0
  38. winipedia_utils/projects/poetry/config.py +91 -0
  39. winipedia_utils/projects/poetry/poetry.py +30 -0
  40. winipedia_utils/setup.py +36 -0
  41. winipedia_utils/testing/__init__.py +1 -0
  42. winipedia_utils/testing/assertions.py +23 -0
  43. winipedia_utils/testing/convention.py +177 -0
  44. winipedia_utils/testing/create_tests.py +286 -0
  45. winipedia_utils/testing/fixtures.py +28 -0
  46. winipedia_utils/testing/tests/__init__.py +1 -0
  47. winipedia_utils/testing/tests/base/__init__.py +1 -0
  48. winipedia_utils/testing/tests/base/fixtures/__init__.py +1 -0
  49. winipedia_utils/testing/tests/base/fixtures/fixture.py +6 -0
  50. winipedia_utils/testing/tests/base/fixtures/scopes/__init__.py +1 -0
  51. winipedia_utils/testing/tests/base/fixtures/scopes/class_.py +33 -0
  52. winipedia_utils/testing/tests/base/fixtures/scopes/function.py +7 -0
  53. winipedia_utils/testing/tests/base/fixtures/scopes/module.py +31 -0
  54. winipedia_utils/testing/tests/base/fixtures/scopes/package.py +7 -0
  55. winipedia_utils/testing/tests/base/fixtures/scopes/session.py +224 -0
  56. winipedia_utils/testing/tests/base/utils/__init__.py +1 -0
  57. winipedia_utils/testing/tests/base/utils/utils.py +82 -0
  58. winipedia_utils/testing/tests/conftest.py +26 -0
  59. winipedia_utils/text/__init__.py +1 -0
  60. winipedia_utils/text/string.py +126 -0
  61. winipedia_utils-0.1.0.dist-info/LICENSE +21 -0
  62. winipedia_utils-0.1.0.dist-info/METADATA +350 -0
  63. winipedia_utils-0.1.0.dist-info/RECORD +64 -0
  64. winipedia_utils-0.1.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,224 @@
1
+ """Session-level test fixtures and utilities.
2
+
3
+ This module provides fixtures and test functions that operate at the session scope,
4
+ ensuring that project-wide conventions are followed and that the overall project
5
+ structure is correct. These fixtures are automatically applied to the test session
6
+ through pytest's autouse mechanism.
7
+ """
8
+
9
+ from importlib import import_module
10
+ from pathlib import Path
11
+
12
+ from winipedia_utils.consts import _DEV_DEPENDENCIES
13
+ from winipedia_utils.git.pre_commit.config import (
14
+ _pre_commit_config_is_correct,
15
+ )
16
+ from winipedia_utils.modules.module import to_path
17
+ from winipedia_utils.modules.package import (
18
+ find_packages,
19
+ get_scr_package,
20
+ walk_package,
21
+ )
22
+ from winipedia_utils.projects.poetry.config import (
23
+ _pyproject_tool_configs_are_correct,
24
+ get_poetry_package_name,
25
+ laod_pyproject_toml,
26
+ )
27
+ from winipedia_utils.testing.assertions import assert_with_msg
28
+ from winipedia_utils.testing.convention import (
29
+ TESTS_PACKAGE_NAME,
30
+ make_test_obj_importpath_from_obj,
31
+ )
32
+ from winipedia_utils.testing.fixtures import autouse_session_fixture
33
+ from winipedia_utils.testing.tests.base.utils.utils import (
34
+ _conftest_content_is_correct,
35
+ )
36
+
37
+
38
+ @autouse_session_fixture
39
+ def _test_dev_dependencies_const_correct() -> None:
40
+ """Verify that the dev dependencies in consts.py are correct.
41
+
42
+ This fixture runs once per test session and checks that the dev dependencies
43
+ in consts.py are correct by comparing them to the dev dependencies in
44
+ pyproject.toml.
45
+
46
+ Raises:
47
+ AssertionError: If the dev dependencies in consts.py are not correct
48
+
49
+ """
50
+ if get_poetry_package_name() != "winipedia_utils":
51
+ # this const is only used in winipedia_utils
52
+ # to be able to install them with setup.py
53
+ return
54
+ toml_dict = laod_pyproject_toml()
55
+ actual_dev_dependencies = (
56
+ toml_dict.get("tool", {})
57
+ .get("poetry", {})
58
+ .get("group", {})
59
+ .get("dev", {})
60
+ .get("dependencies", {})
61
+ .keys()
62
+ )
63
+ assert_with_msg(
64
+ set(actual_dev_dependencies) == set(_DEV_DEPENDENCIES),
65
+ "Dev dependencies in consts.py are not correct",
66
+ )
67
+
68
+
69
+ @autouse_session_fixture
70
+ def _test_conftest_exists_and_is_correct() -> None:
71
+ """Verify that the conftest.py file exists and has the correct content.
72
+
73
+ This fixture runs once per test session and checks that the conftest.py file
74
+ exists in the tests directory and contains the correct pytest_plugins configuration.
75
+
76
+ Raises:
77
+ AssertionError: If the conftest.py file doesn't exist or has incorrect content
78
+
79
+ """
80
+ conftest_path = Path(TESTS_PACKAGE_NAME, "conftest.py")
81
+ assert_with_msg(
82
+ conftest_path.is_file(),
83
+ f"Expected conftest.py file at {conftest_path} but it doesn't exist",
84
+ )
85
+
86
+ assert_with_msg(
87
+ _conftest_content_is_correct(conftest_path),
88
+ "conftest.py has incorrect content",
89
+ )
90
+
91
+
92
+ @autouse_session_fixture
93
+ def _test_pyproject_toml_is_correct() -> None:
94
+ """Verify that the pyproject.toml file exists and has the correct content.
95
+
96
+ This fixture runs once per test session and checks that the pyproject.toml file
97
+ exists in the root directory and contains the correct content.
98
+
99
+ Raises:
100
+ AssertionError: If the pyproject.toml file doesn't exist
101
+ or has incorrect content
102
+
103
+ """
104
+ pyproject_toml_path = Path("pyproject.toml")
105
+ assert_with_msg(
106
+ pyproject_toml_path.is_file(),
107
+ f"Expected pyproject.toml file at {pyproject_toml_path} but it doesn't exist",
108
+ )
109
+ assert_with_msg(
110
+ _pyproject_tool_configs_are_correct(),
111
+ "pyproject.toml has incorrect content.",
112
+ )
113
+
114
+
115
+ @autouse_session_fixture
116
+ def _test_pre_commit_config_yaml_is_correct() -> None:
117
+ """Verify that the pre-commit yaml is correctly defining winipedia utils hook.
118
+
119
+ Checks that the yaml starts with the winipedia utils hook.
120
+ """
121
+ pre_commit_config = Path(".pre-commit-config.yaml")
122
+
123
+ assert_with_msg(
124
+ pre_commit_config.is_file(),
125
+ f"Expected {pre_commit_config} to exist but it doesn't.",
126
+ )
127
+ assert_with_msg(
128
+ _pre_commit_config_is_correct(),
129
+ "Pre commit config is not correct.",
130
+ )
131
+
132
+
133
+ @autouse_session_fixture
134
+ def _test_no_namespace_packages() -> None:
135
+ """Verify that there are no namespace packages in the project.
136
+
137
+ This fixture runs once per test session and checks that all packages in the
138
+ project are regular packages with __init__.py files, not namespace packages.
139
+
140
+ Raises:
141
+ AssertionError: If any namespace packages are found
142
+
143
+ """
144
+ packages = find_packages(depth=None)
145
+ namespace_packages = find_packages(depth=None, include_namespace_packages=True)
146
+
147
+ any_namespace_packages = set(namespace_packages) - set(packages)
148
+ assert_with_msg(
149
+ not any_namespace_packages,
150
+ f"Found namespace packages: {any_namespace_packages}. "
151
+ f"All packages should have __init__.py files.",
152
+ )
153
+
154
+
155
+ @autouse_session_fixture
156
+ def _test_all_src_code_in_one_package() -> None:
157
+ """Verify that all source code is in a single package.
158
+
159
+ This fixture runs once per test session and checks that there is only one
160
+ source package besides the tests package.
161
+
162
+ Raises:
163
+ AssertionError: If there are multiple source packages
164
+
165
+ """
166
+ packages = find_packages(depth=0)
167
+ src_package = get_scr_package().__name__
168
+ expected_packages = {TESTS_PACKAGE_NAME, src_package}
169
+ assert_with_msg(
170
+ set(packages) == expected_packages,
171
+ f"Expected only packages {expected_packages}, but found {packages}",
172
+ )
173
+
174
+
175
+ @autouse_session_fixture
176
+ def _test_project_structure_mirrored() -> None:
177
+ """Verify that the project structure is mirrored in tests.
178
+
179
+ This fixture runs once per test session and checks that for every package and
180
+ module in the source package, there is a corresponding test package and module.
181
+
182
+ Raises:
183
+ AssertionError: If any package or module doesn't have a corresponding test
184
+
185
+ """
186
+ src_package = get_scr_package()
187
+
188
+ # we will now go through all the modules in the src package and check
189
+ # that there is a corresponding test module
190
+ for package, modules in walk_package(src_package):
191
+ test_package_name = make_test_obj_importpath_from_obj(package)
192
+ test_package = import_module(test_package_name)
193
+ assert_with_msg(
194
+ bool(test_package),
195
+ f"Expected test package {test_package_name} to be a module",
196
+ )
197
+
198
+ for module in modules:
199
+ test_module_name = make_test_obj_importpath_from_obj(module)
200
+ test_module = import_module(test_module_name)
201
+ assert_with_msg(
202
+ bool(test_module),
203
+ f"Expected test module {test_module_name} to be a module",
204
+ )
205
+
206
+
207
+ @autouse_session_fixture
208
+ def _test_no_unitest_package_usage() -> None:
209
+ """Verify that the unittest package is not used in the project.
210
+
211
+ This fixture runs once per test session and checks that the unittest package
212
+ is not used in the project.
213
+
214
+ Raises:
215
+ AssertionError: If the unittest package is used
216
+
217
+ """
218
+ for path in Path().rglob("*.py"):
219
+ if path == to_path(__name__, is_package=False):
220
+ continue
221
+ assert_with_msg(
222
+ "unittest" not in path.read_text(),
223
+ f"Found unittest usage in {path}. Use pytest instead.",
224
+ )
@@ -0,0 +1 @@
1
+ """__init__ module for winipedia_utils.testing.tests.base.utils."""
@@ -0,0 +1,82 @@
1
+ """Testing utilities for introspection and validation.
2
+
3
+ This module provides utility functions for working with tests, including:
4
+ - Asserting that all objects in the source have corresponding test objects
5
+ - Generating the content for a conftest.py file
6
+
7
+ Returns:
8
+ Various utility functions for testing introspection and validation.
9
+
10
+ """
11
+
12
+ from collections.abc import Callable
13
+ from pathlib import Path
14
+ from types import ModuleType
15
+ from typing import Any
16
+
17
+ from winipedia_utils.modules.module import get_objs_from_obj, make_obj_importpath
18
+ from winipedia_utils.testing.assertions import assert_with_msg
19
+ from winipedia_utils.testing.convention import (
20
+ get_obj_from_test_obj,
21
+ make_test_obj_importpath_from_obj,
22
+ make_untested_summary_error_msg,
23
+ )
24
+
25
+
26
+ def _assert_no_untested_objs(
27
+ test_obj: ModuleType | type | Callable[..., Any],
28
+ ) -> None:
29
+ """Assert that all objects in the source have corresponding test objects.
30
+
31
+ This function verifies that every object (function, class, or method) in the
32
+ source module or class has a corresponding test object in the test module or class.
33
+
34
+ Args:
35
+ test_obj: The test object (module, class, or function) to check
36
+
37
+ Raises:
38
+ AssertionError: If any object in the source lacks a corresponding test object,
39
+ with a detailed error message listing the untested objects
40
+
41
+ """
42
+ test_objs = get_objs_from_obj(test_obj)
43
+ test_objs_paths = {make_obj_importpath(o) for o in test_objs}
44
+
45
+ obj = get_obj_from_test_obj(test_obj)
46
+ objs = get_objs_from_obj(obj)
47
+ supposed_test_objs_paths = {make_test_obj_importpath_from_obj(o) for o in objs}
48
+
49
+ untested_objs = supposed_test_objs_paths - test_objs_paths
50
+
51
+ assert_with_msg(not untested_objs, make_untested_summary_error_msg(untested_objs))
52
+
53
+
54
+ def _get_conftest_content() -> str:
55
+ """Get the content for a conftest.py file when using winipedia_utils."""
56
+ return '''
57
+ """Pytest configuration for tests.
58
+
59
+ This module configures pytest plugins for the test suite, setting up the necessary
60
+ fixtures and hooks for the different
61
+ test scopes (function, class, module, package, session).
62
+ It also import custom plugins from tests/base/scopes.
63
+ This file should not be modified manually.
64
+ """
65
+
66
+ pytest_plugins = ["winipedia_utils.testing.tests.conftest"]
67
+ '''.strip()
68
+
69
+
70
+ def _conftest_content_is_correct(conftest_path: Path) -> bool:
71
+ """Check if the conftest.py file has the correct content.
72
+
73
+ Args:
74
+ conftest_path: The path to the conftest.py file
75
+
76
+ Returns:
77
+ True if the conftest.py file exists and has the correct content, False otherwise
78
+
79
+ """
80
+ if not conftest_path.exists():
81
+ return False
82
+ return conftest_path.read_text().startswith(_get_conftest_content())
@@ -0,0 +1,26 @@
1
+ """Pytest configuration for winipedia_utils tests.
2
+
3
+ finds all the plugins in the tests directory and the package's testing module
4
+ and adds them to pytest_plugins. This way defining reusable fixtures is easy.
5
+ """
6
+
7
+ from winipedia_utils.consts import PACKAGE_NAME
8
+ from winipedia_utils.modules.module import to_module_name, to_path
9
+
10
+ custom_plugin_path = to_path("tests.base.fixtures", is_package=True)
11
+ package_plugin_path = (
12
+ to_path(f"{PACKAGE_NAME}.testing", is_package=True) / custom_plugin_path
13
+ )
14
+
15
+ custom_plugin_module_names = [
16
+ to_module_name(path) for path in custom_plugin_path.rglob("*.py")
17
+ ]
18
+ package_plugin_module_names = [
19
+ to_module_name(path) for path in package_plugin_path.rglob("*.py")
20
+ ]
21
+
22
+
23
+ pytest_plugins = [
24
+ *package_plugin_module_names,
25
+ *custom_plugin_module_names,
26
+ ]
@@ -0,0 +1 @@
1
+ """__init__ module for winipedia_utils.text."""
@@ -0,0 +1,126 @@
1
+ """String manipulation utilities for text processing.
2
+
3
+ This module provides utility functions for working with strings, including
4
+ input handling, XML parsing, string truncation, and hashing operations.
5
+ These utilities simplify common string manipulation tasks throughout the application.
6
+ """
7
+
8
+ import hashlib
9
+ import textwrap
10
+ from io import StringIO
11
+
12
+ from defusedxml import ElementTree as DefusedElementTree
13
+
14
+ from winipedia_utils.concurrent.multiprocessing import (
15
+ cancel_on_timeout,
16
+ )
17
+ from winipedia_utils.logging.logger import get_logger
18
+
19
+ logger = get_logger(__name__)
20
+
21
+
22
+ def ask_for_input_with_timeout(prompt: str, timeout: int) -> str:
23
+ """Request user input with a timeout constraint.
24
+
25
+ Args:
26
+ prompt: The text prompt to display to the user
27
+ timeout: Maximum time in seconds to wait for input
28
+
29
+ Returns:
30
+ The user's input as a string
31
+
32
+ Raises:
33
+ TimeoutError: If the user doesn't provide input within the timeout period
34
+
35
+ """
36
+
37
+ @cancel_on_timeout(timeout, "Input not given within the timeout")
38
+ def give_input() -> str:
39
+ return input(prompt)
40
+
41
+ user_input: str = give_input()
42
+
43
+ return user_input
44
+
45
+
46
+ def find_xml_namespaces(xml: str | StringIO) -> dict[str, str]:
47
+ """Extract namespace declarations from XML content.
48
+
49
+ Args:
50
+ xml: XML content as a string or StringIO object
51
+
52
+ Returns:
53
+ Dictionary mapping namespace prefixes to their URIs,
54
+ excluding the default namespace
55
+
56
+ """
57
+ if not isinstance(xml, StringIO):
58
+ xml = StringIO(xml)
59
+ # Extract the namespaces from the root tag
60
+ namespaces_: dict[str, str] = {}
61
+ iter_ns = DefusedElementTree.iterparse(xml, events=["start-ns"])
62
+ for _, elem in iter_ns:
63
+ prefix, uri = elem
64
+ namespaces_[prefix] = uri
65
+
66
+ namespaces_.pop("", None)
67
+
68
+ return namespaces_
69
+
70
+
71
+ def value_to_truncated_string(value: object, max_length: int) -> str:
72
+ """Convert any value to a string and truncate if longer than max_length.
73
+
74
+ Args:
75
+ value: Any object to convert to string
76
+ max_length: Maximum length of the resulting string
77
+
78
+ Returns:
79
+ Truncated string representation of the value
80
+
81
+ """
82
+ string = str(value)
83
+ return textwrap.shorten(string, width=max_length, placeholder="...")
84
+
85
+
86
+ def get_reusable_hash(value: object) -> str:
87
+ """Generate a consistent hash for any object.
88
+
89
+ Creates a SHA-256 hash of the string representation of the given value.
90
+ This hash is deterministic and can be used for caching or identification.
91
+
92
+ Args:
93
+ value: Any object to hash
94
+
95
+ Returns:
96
+ Hexadecimal string representation of the SHA-256 hash
97
+
98
+ """
99
+ value_str = str(value)
100
+ return hashlib.sha256(value_str.encode("utf-8")).hexdigest()
101
+
102
+
103
+ def split_on_uppercase(string: str) -> list[str]:
104
+ """Split a string on uppercase letters.
105
+
106
+ Args:
107
+ string: String to split
108
+
109
+ Returns:
110
+ List of substrings split on uppercase letters
111
+
112
+ Example:
113
+ split_on_uppercase("HelloWorld") -> ["Hello", "World"]
114
+
115
+ """
116
+ letters = list(string)
117
+ parts = []
118
+ current_part = ""
119
+ for letter in letters:
120
+ if letter.isupper() and current_part:
121
+ parts.append(current_part)
122
+ current_part = letter
123
+ else:
124
+ current_part += letter
125
+ parts.append(current_part)
126
+ return parts
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Winipedia
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.