winipedia-utils 0.2.10__py3-none-any.whl → 0.2.17__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.
Potentially problematic release.
This version of winipedia-utils might be problematic. Click here for more details.
- winipedia_utils/concurrent/concurrent.py +245 -245
- winipedia_utils/concurrent/multiprocessing.py +130 -130
- winipedia_utils/concurrent/multithreading.py +93 -93
- winipedia_utils/consts.py +21 -21
- winipedia_utils/data/__init__.py +1 -1
- winipedia_utils/data/dataframe/__init__.py +1 -1
- winipedia_utils/data/dataframe/cleaning.py +378 -378
- winipedia_utils/data/structures/__init__.py +1 -1
- winipedia_utils/data/structures/dicts.py +16 -16
- winipedia_utils/git/__init__.py +1 -1
- winipedia_utils/git/gitignore/__init__.py +1 -1
- winipedia_utils/git/gitignore/gitignore.py +136 -136
- winipedia_utils/git/pre_commit/__init__.py +1 -1
- winipedia_utils/git/pre_commit/config.py +70 -70
- winipedia_utils/git/pre_commit/hooks.py +127 -109
- winipedia_utils/git/pre_commit/run_hooks.py +49 -49
- winipedia_utils/iterating/__init__.py +1 -1
- winipedia_utils/iterating/iterate.py +29 -29
- winipedia_utils/logging/ansi.py +6 -6
- winipedia_utils/logging/config.py +64 -64
- winipedia_utils/logging/logger.py +26 -26
- winipedia_utils/modules/class_.py +119 -119
- winipedia_utils/modules/function.py +101 -101
- winipedia_utils/modules/module.py +379 -379
- winipedia_utils/modules/package.py +390 -390
- winipedia_utils/oop/mixins/meta.py +333 -333
- winipedia_utils/oop/mixins/mixin.py +37 -37
- winipedia_utils/os/__init__.py +1 -1
- winipedia_utils/os/os.py +63 -63
- winipedia_utils/projects/__init__.py +1 -1
- winipedia_utils/projects/poetry/__init__.py +1 -1
- winipedia_utils/projects/poetry/config.py +117 -117
- winipedia_utils/projects/poetry/poetry.py +31 -31
- winipedia_utils/projects/project.py +48 -48
- winipedia_utils/resources/__init__.py +1 -1
- winipedia_utils/resources/svgs/__init__.py +1 -1
- winipedia_utils/resources/svgs/download_arrow.svg +2 -2
- winipedia_utils/resources/svgs/exit_fullscreen_icon.svg +5 -5
- winipedia_utils/resources/svgs/fullscreen_icon.svg +2 -2
- winipedia_utils/resources/svgs/menu_icon.svg +3 -3
- winipedia_utils/resources/svgs/pause_icon.svg +3 -3
- winipedia_utils/resources/svgs/play_icon.svg +16 -16
- winipedia_utils/resources/svgs/plus_icon.svg +23 -23
- winipedia_utils/resources/svgs/svg.py +15 -15
- winipedia_utils/security/__init__.py +1 -1
- winipedia_utils/security/cryptography.py +29 -29
- winipedia_utils/security/keyring.py +70 -70
- winipedia_utils/setup.py +47 -47
- winipedia_utils/testing/assertions.py +23 -23
- winipedia_utils/testing/convention.py +177 -177
- winipedia_utils/testing/create_tests.py +297 -297
- winipedia_utils/testing/fixtures.py +28 -28
- winipedia_utils/testing/tests/base/fixtures/__init__.py +1 -1
- winipedia_utils/testing/tests/base/fixtures/fixture.py +6 -6
- winipedia_utils/testing/tests/base/fixtures/scopes/class_.py +33 -33
- winipedia_utils/testing/tests/base/fixtures/scopes/function.py +7 -7
- winipedia_utils/testing/tests/base/fixtures/scopes/module.py +33 -33
- winipedia_utils/testing/tests/base/fixtures/scopes/package.py +7 -7
- winipedia_utils/testing/tests/base/fixtures/scopes/session.py +296 -296
- winipedia_utils/testing/tests/base/utils/utils.py +111 -111
- winipedia_utils/testing/tests/conftest.py +32 -32
- winipedia_utils/text/string.py +126 -126
- winipedia_utils-0.2.17.dist-info/METADATA +716 -0
- winipedia_utils-0.2.17.dist-info/RECORD +80 -0
- {winipedia_utils-0.2.10.dist-info → winipedia_utils-0.2.17.dist-info}/licenses/LICENSE +21 -21
- winipedia_utils/testing/tests/test_0.py +0 -8
- winipedia_utils-0.2.10.dist-info/METADATA +0 -355
- winipedia_utils-0.2.10.dist-info/RECORD +0 -81
- {winipedia_utils-0.2.10.dist-info → winipedia_utils-0.2.17.dist-info}/WHEEL +0 -0
|
@@ -1,111 +1,111 @@
|
|
|
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())
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
def _get_test_0_content() -> str:
|
|
86
|
-
"""Get the content for a test_0.py file when using winipedia_utils."""
|
|
87
|
-
return '''
|
|
88
|
-
"""Contains an empty test."""
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
def test_0() -> None:
|
|
92
|
-
"""Empty test.
|
|
93
|
-
|
|
94
|
-
Exists so that when no tests are written yet the base fixtures are executed.
|
|
95
|
-
"""
|
|
96
|
-
'''.strip()
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
def _test_0_content_is_correct(test_0_path: Path) -> bool:
|
|
100
|
-
"""Check if the test_0.py file has the correct content.
|
|
101
|
-
|
|
102
|
-
Args:
|
|
103
|
-
test_0_path: The path to the test_0.py file
|
|
104
|
-
|
|
105
|
-
Returns:
|
|
106
|
-
True if the test_0.py file exists and has the correct content, False otherwise
|
|
107
|
-
|
|
108
|
-
"""
|
|
109
|
-
if not test_0_path.exists():
|
|
110
|
-
return False
|
|
111
|
-
return test_0_path.read_text().startswith(_get_test_0_content())
|
|
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())
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _get_test_0_content() -> str:
|
|
86
|
+
"""Get the content for a test_0.py file when using winipedia_utils."""
|
|
87
|
+
return '''
|
|
88
|
+
"""Contains an empty test."""
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_0() -> None:
|
|
92
|
+
"""Empty test.
|
|
93
|
+
|
|
94
|
+
Exists so that when no tests are written yet the base fixtures are executed.
|
|
95
|
+
"""
|
|
96
|
+
'''.strip()
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _test_0_content_is_correct(test_0_path: Path) -> bool:
|
|
100
|
+
"""Check if the test_0.py file has the correct content.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
test_0_path: The path to the test_0.py file
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
True if the test_0.py file exists and has the correct content, False otherwise
|
|
107
|
+
|
|
108
|
+
"""
|
|
109
|
+
if not test_0_path.exists():
|
|
110
|
+
return False
|
|
111
|
+
return test_0_path.read_text().startswith(_get_test_0_content())
|
|
@@ -1,32 +1,32 @@
|
|
|
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 pathlib import Path
|
|
8
|
-
|
|
9
|
-
import winipedia_utils
|
|
10
|
-
from winipedia_utils.modules.module import to_module_name, to_path
|
|
11
|
-
|
|
12
|
-
package_path = Path(winipedia_utils.__path__[0])
|
|
13
|
-
|
|
14
|
-
custom_plugin_path = to_path("tests.base.fixtures", is_package=True)
|
|
15
|
-
package_plugin_path = (
|
|
16
|
-
package_path / to_path("testing", is_package=True) / custom_plugin_path
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
custom_plugin_module_names = [
|
|
20
|
-
to_module_name(path) for path in custom_plugin_path.rglob("*.py")
|
|
21
|
-
]
|
|
22
|
-
|
|
23
|
-
package_plugin_module_names = [
|
|
24
|
-
to_module_name(path.relative_to(package_path.parent))
|
|
25
|
-
for path in package_plugin_path.rglob("*.py")
|
|
26
|
-
]
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
pytest_plugins = [
|
|
30
|
-
*package_plugin_module_names,
|
|
31
|
-
*custom_plugin_module_names,
|
|
32
|
-
]
|
|
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 pathlib import Path
|
|
8
|
+
|
|
9
|
+
import winipedia_utils
|
|
10
|
+
from winipedia_utils.modules.module import to_module_name, to_path
|
|
11
|
+
|
|
12
|
+
package_path = Path(winipedia_utils.__path__[0])
|
|
13
|
+
|
|
14
|
+
custom_plugin_path = to_path("tests.base.fixtures", is_package=True)
|
|
15
|
+
package_plugin_path = (
|
|
16
|
+
package_path / to_path("testing", is_package=True) / custom_plugin_path
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
custom_plugin_module_names = [
|
|
20
|
+
to_module_name(path) for path in custom_plugin_path.rglob("*.py")
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
package_plugin_module_names = [
|
|
24
|
+
to_module_name(path.relative_to(package_path.parent))
|
|
25
|
+
for path in package_plugin_path.rglob("*.py")
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
pytest_plugins = [
|
|
30
|
+
*package_plugin_module_names,
|
|
31
|
+
*custom_plugin_module_names,
|
|
32
|
+
]
|
winipedia_utils/text/string.py
CHANGED
|
@@ -1,126 +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 _, namespace_data in iter_ns:
|
|
63
|
-
prefix, uri = namespace_data
|
|
64
|
-
namespaces_[str(prefix)] = str(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
|
|
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 _, namespace_data in iter_ns:
|
|
63
|
+
prefix, uri = namespace_data
|
|
64
|
+
namespaces_[str(prefix)] = str(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
|