rite 0.0.2__tar.gz → 0.0.4__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.
- {rite-0.0.2 → rite-0.0.4}/PKG-INFO +4 -3
- {rite-0.0.2 → rite-0.0.4}/pyproject.toml +10 -11
- rite-0.0.2/src/rite/__main__.py → rite-0.0.4/src/rite/--__main__.py +1 -1
- {rite-0.0.2 → rite-0.0.4}/src/rite/__init__.py +10 -10
- rite-0.0.4/src/rite/error/error_handler.py +175 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/file/geojson.py +1 -3
- {rite-0.0.2 → rite-0.0.4}/src/rite/file/ini.py +28 -22
- {rite-0.0.2 → rite-0.0.4}/src/rite/file/json.py +32 -36
- {rite-0.0.2 → rite-0.0.4}/src/rite/file/obj.py +10 -6
- {rite-0.0.2 → rite-0.0.4}/src/rite/folder/file.py +4 -8
- {rite-0.0.2 → rite-0.0.4}/src/rite/folder/folder.py +19 -23
- rite-0.0.4/src/rite/function/debounce.py +46 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/log/__init__.py +2 -2
- rite-0.0.4/src/rite/math/__init__.py +22 -0
- rite-0.0.4/src/rite/math/clamp.py +6 -0
- rite-0.0.4/src/rite/server/__init__.py +25 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/server/http.py +33 -35
- rite-0.0.4/src/rite/server/http_handler.py +184 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/server/sqlite.py +23 -15
- rite-0.0.4/src/rite/structures/__init__.py +11 -0
- rite-0.0.4/src/rite/structures/structure_set_nested.py +145 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/text/case.py +283 -106
- {rite-0.0.2 → rite-0.0.4}/src/rite/text/cipher.py +151 -73
- {rite-0.0.2 → rite-0.0.4}/src/rite/text/morse.py +94 -23
- {rite-0.0.2 → rite-0.0.4}/src/rite/text/slug.py +61 -53
- {rite-0.0.2 → rite-0.0.4}/src/rite/text/url.py +0 -2
- {rite-0.0.2 → rite-0.0.4}/src/rite/text/uuid.py +5 -7
- rite-0.0.4/src/rite/time/__init__.py +65 -0
- rite-0.0.4/src/rite/time/duration.py +217 -0
- rite-0.0.4/src/rite/time/timestamp.py +344 -0
- rite-0.0.4/src/rite/time/timezone.py +215 -0
- rite-0.0.4/src/rite/utils/io.py +214 -0
- rite-0.0.4/src/rite/utils/system.py +124 -0
- rite-0.0.2/src/main.py +0 -0
- rite-0.0.2/src/rite/server/__init__.py +0 -5
- rite-0.0.2/src/tests/__init__.py +0 -0
- rite-0.0.2/src/tests/data/test_buffer_circular.py +0 -213
- rite-0.0.2/src/tests/data/test_singleton.py +0 -145
- rite-0.0.2/src/tests/log/logger.py +0 -137
- {rite-0.0.2 → rite-0.0.4}/LICENSE +0 -0
- {rite-0.0.2 → rite-0.0.4}/README.md +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/CNAME +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/about/authors.md +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/favicon/android-chrome-192x192.png +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/favicon/android-chrome-512x512.png +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/favicon/apple-touch-icon.png +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/favicon/favicon-16x16.png +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/favicon/favicon-32x32.png +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/favicon/favicon.ico +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-bold.otf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-bold.ttf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-bold.woff2 +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-extrabold.otf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-extrabold.ttf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-extrabold.woff2 +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-extralight.otf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-extralight.ttf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-extralight.woff2 +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-light.otf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-light.ttf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-light.woff2 +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-medium.otf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-medium.ttf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-medium.woff2 +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-regular.otf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-regular.ttf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-regular.woff2 +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-semibold.otf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-semibold.ttf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-semibold.woff2 +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-variable.ttf +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/font/manrope/manrope-variable.woff2 +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/image/rite_logo_dark.png +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/image/rite_logo_light.png +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/script/extra.js +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/assets/style/extra.css +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/development/build.md +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/index.md +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/legal/disclaimer.md +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/legal/license.md +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/legal/privacy_policy.md +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/overrides/_main.html +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/quick_start.md +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/resources/glossary.md +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/resources/links.md +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/robots.txt +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/security.txt +0 -0
- {rite-0.0.2 → rite-0.0.4}/docs/site.webmanifest +0 -0
- {rite-0.0.2/src → rite-0.0.4/src/rite/data}/__init__.py +0 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/data/buffer_circular.py +0 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/data/colour.py +0 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/data/image_raster.py +0 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/data/pixel.py +0 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/data/singleton.py +0 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/data/utils.py +0 -0
- {rite-0.0.2/src/rite/data → rite-0.0.4/src/rite/error}/__init__.py +0 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/file/__init__.py +0 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/file/utils.py +1 -1
- {rite-0.0.2 → rite-0.0.4}/src/rite/folder/__init__.py +0 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/folder/path.py +0 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/log/logger.py +0 -0
- {rite-0.0.2 → rite-0.0.4}/src/rite/text/__init__.py +1 -1
- {rite-0.0.2/src/rite/data → rite-0.0.4/src/rite/time}/date.py +0 -0
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
2
|
Name: rite
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.4
|
|
4
4
|
Summary: Python Utility Package
|
|
5
|
-
Home-page: https://www.pyrites.dev
|
|
6
5
|
License: MIT
|
|
7
6
|
Keywords: rite
|
|
8
7
|
Author: Scape Agency
|
|
@@ -18,7 +17,9 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
18
17
|
Classifier: Programming Language :: Python :: 3.12
|
|
19
18
|
Classifier: Programming Language :: Python :: 3.13
|
|
20
19
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Requires-Dist: pytz (>=2024.2,<2025.0)
|
|
21
21
|
Project-URL: Documentation, https://github.com/scape-agency/rite/docs
|
|
22
|
+
Project-URL: Homepage, https://www.pyrites.dev
|
|
22
23
|
Project-URL: Repository, https://github.com/scape-agency/rite
|
|
23
24
|
Description-Content-Type: text/markdown
|
|
24
25
|
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
[tool.poetry]
|
|
9
9
|
name = "rite"
|
|
10
|
-
version = "0.0.
|
|
10
|
+
version = "0.0.4"
|
|
11
11
|
description = "Python Utility Package"
|
|
12
12
|
authors = ["Scape Agency <info@scape.agency>"]
|
|
13
13
|
license = "MIT License"
|
|
@@ -28,7 +28,7 @@ classifiers = [
|
|
|
28
28
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
29
29
|
]
|
|
30
30
|
include = [
|
|
31
|
-
"src/**",
|
|
31
|
+
"src/rite/**",
|
|
32
32
|
"README.md",
|
|
33
33
|
"LICENSE",
|
|
34
34
|
"docs/**", # Include documentation in the package
|
|
@@ -42,18 +42,19 @@ packages = [{ include = "rite", from = "src" }]
|
|
|
42
42
|
|
|
43
43
|
[tool.poetry.dependencies]
|
|
44
44
|
python = ">=3.10,<4.0"
|
|
45
|
+
pytz = "^2024.2"
|
|
45
46
|
|
|
46
47
|
|
|
47
48
|
# =============================================================================
|
|
48
49
|
# Development Dependencies
|
|
49
50
|
# =============================================================================
|
|
50
51
|
|
|
51
|
-
[tool.poetry.dev
|
|
52
|
+
[tool.poetry.group.dev.dependencies]
|
|
52
53
|
pytest = "^8.3.4" # Framework for testing
|
|
53
54
|
pytest-cov = "^6.0.0" # Code coverage for tests
|
|
54
55
|
black = "^24.8.0" # Code formatter
|
|
55
56
|
flake8 = "^7.1.0" # Linter for code quality
|
|
56
|
-
mypy = "^1.14.
|
|
57
|
+
mypy = "^1.14.1" # Static type checker
|
|
57
58
|
sphinx = "^8.0.2" # Documentation generator
|
|
58
59
|
isort = "^5.10.1" # Import sorter
|
|
59
60
|
tox = "^4.4.6" # Testing multiple environments
|
|
@@ -65,8 +66,6 @@ pre-commit = "^4.0.1" # Pre-commit hooks for linting and formatting
|
|
|
65
66
|
# =============================================================================
|
|
66
67
|
|
|
67
68
|
[build-system]
|
|
68
|
-
# requires = ["setuptools>=42", "wheel"] # PEP 508 specifications.
|
|
69
|
-
# build-backend = "setuptools.build_meta"
|
|
70
69
|
requires = ["poetry-core>=1.5.0"]
|
|
71
70
|
build-backend = "poetry.core.masonry.api"
|
|
72
71
|
|
|
@@ -76,11 +75,11 @@ build-backend = "poetry.core.masonry.api"
|
|
|
76
75
|
# =============================================================================
|
|
77
76
|
|
|
78
77
|
[tool.poetry.scripts]
|
|
79
|
-
rite = "rite.__main__:main"
|
|
80
|
-
test = "pytest --cov=rite tests/:main"
|
|
81
|
-
lint = "flake8 src/rite/:main" # Run linter on source files
|
|
82
|
-
format = "black src/rite/:main" # Format source files
|
|
83
|
-
type-check = "mypy src/rite/:main" # Type-check source files
|
|
78
|
+
rite = "rite.__main__:main" # Entry point for the package
|
|
79
|
+
# test = "pytest --cov=rite tests/:main" # Run tests with coverage
|
|
80
|
+
# lint = "flake8 src/rite/:main" # Run linter on source files
|
|
81
|
+
# format = "black src/rite/:main" # Format source files
|
|
82
|
+
# type-check = "mypy src/rite/:main" # Type-check source files
|
|
84
83
|
|
|
85
84
|
|
|
86
85
|
# =============================================================================
|
|
@@ -47,7 +47,7 @@ if __name__ == "__main__":
|
|
|
47
47
|
print()
|
|
48
48
|
print("rite is set!")
|
|
49
49
|
print()
|
|
50
|
-
print("
|
|
50
|
+
print("rite: {}".format(rite.__version__))
|
|
51
51
|
print(
|
|
52
52
|
"Python: {} ({})".format(
|
|
53
53
|
platform.python_version(), platform.python_implementation()
|
|
@@ -33,13 +33,13 @@ Links:
|
|
|
33
33
|
# Information
|
|
34
34
|
# =============================================================================
|
|
35
35
|
|
|
36
|
-
__author__ =
|
|
37
|
-
__copyright__ =
|
|
38
|
-
__credits__ =
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
__license__ =
|
|
42
|
-
__version__ =
|
|
43
|
-
__maintainer__ =
|
|
44
|
-
__email__ =
|
|
45
|
-
__status__ =
|
|
36
|
+
__author__ = "Lars van Vianen"
|
|
37
|
+
__copyright__ = "Copyright 2022, Scape Agency"
|
|
38
|
+
__credits__ = [
|
|
39
|
+
"Lars van Vianen",
|
|
40
|
+
]
|
|
41
|
+
__license__ = "MIT License"
|
|
42
|
+
__version__ = "0.0.4"
|
|
43
|
+
__maintainer__ = "Scape Agency"
|
|
44
|
+
__email__ = "info@scape.agency"
|
|
45
|
+
__status__ = "Alpha"
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# =============================================================================
|
|
5
|
+
# Docstring
|
|
6
|
+
# =============================================================================
|
|
7
|
+
|
|
8
|
+
"""
|
|
9
|
+
Error Handling Module
|
|
10
|
+
=====================
|
|
11
|
+
|
|
12
|
+
This module provides the `ErrorHandler` class for managing and logging errors
|
|
13
|
+
in the system. It supports error logging, raising alerts, and retrying failed
|
|
14
|
+
operations with customizable retry logic.
|
|
15
|
+
|
|
16
|
+
Classes:
|
|
17
|
+
--------
|
|
18
|
+
- ErrorHandler: Handles error logging, alerting, and retrying operations.
|
|
19
|
+
|
|
20
|
+
Features:
|
|
21
|
+
---------
|
|
22
|
+
- Logs errors to a specified log file.
|
|
23
|
+
- Raises alerts for critical errors.
|
|
24
|
+
- Retries failed operations with configurable retry limits and exponential
|
|
25
|
+
backoff.
|
|
26
|
+
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# =============================================================================
|
|
31
|
+
# Imports
|
|
32
|
+
# =============================================================================
|
|
33
|
+
|
|
34
|
+
import logging
|
|
35
|
+
import time
|
|
36
|
+
from typing import Callable, Optional
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# =============================================================================
|
|
40
|
+
# Classes
|
|
41
|
+
# =============================================================================
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class ErrorHandler:
|
|
45
|
+
"""
|
|
46
|
+
Error Handler Class
|
|
47
|
+
===================
|
|
48
|
+
|
|
49
|
+
A class to handle and log errors in the system. It provides methods for
|
|
50
|
+
logging errors, raising alerts, and retrying operations.
|
|
51
|
+
|
|
52
|
+
Attributes:
|
|
53
|
+
-----------
|
|
54
|
+
log_file : Optional[str]
|
|
55
|
+
The path to the log file for error logging.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
def __init__(
|
|
59
|
+
self,
|
|
60
|
+
log_file: Optional[str] = "error_log.txt",
|
|
61
|
+
) -> None:
|
|
62
|
+
"""
|
|
63
|
+
Initializes the ErrorHandler with a specified log file.
|
|
64
|
+
|
|
65
|
+
Parameters:
|
|
66
|
+
-----------
|
|
67
|
+
log_file : Optional[str]
|
|
68
|
+
The path to the log file (default: 'error_log.txt').
|
|
69
|
+
"""
|
|
70
|
+
self.log_file = log_file
|
|
71
|
+
logging.basicConfig(
|
|
72
|
+
filename=self.log_file,
|
|
73
|
+
level=logging.ERROR,
|
|
74
|
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
def log_error(self, error: str) -> None:
|
|
78
|
+
"""
|
|
79
|
+
Logs an error message to the log file.
|
|
80
|
+
|
|
81
|
+
Parameters:
|
|
82
|
+
-----------
|
|
83
|
+
error : str
|
|
84
|
+
The error message to log.
|
|
85
|
+
"""
|
|
86
|
+
logging.error(error)
|
|
87
|
+
print(f"Error logged: {error}")
|
|
88
|
+
|
|
89
|
+
def raise_alert(
|
|
90
|
+
self,
|
|
91
|
+
error: str,
|
|
92
|
+
) -> None:
|
|
93
|
+
"""
|
|
94
|
+
Raises an alert for critical errors and logs the alert message.
|
|
95
|
+
|
|
96
|
+
Parameters:
|
|
97
|
+
-----------
|
|
98
|
+
error : str
|
|
99
|
+
The error message to alert on.
|
|
100
|
+
"""
|
|
101
|
+
# Extendable to include email, SMS, or notification services
|
|
102
|
+
print(f"ALERT: {error}")
|
|
103
|
+
self.log_error(error)
|
|
104
|
+
|
|
105
|
+
def retry_operation(
|
|
106
|
+
self,
|
|
107
|
+
operation: Callable,
|
|
108
|
+
retries: int = 3,
|
|
109
|
+
backoff: float = 1.0,
|
|
110
|
+
) -> bool:
|
|
111
|
+
"""
|
|
112
|
+
Retries a specified operation a given number of times with optional
|
|
113
|
+
exponential backoff.
|
|
114
|
+
|
|
115
|
+
Parameters:
|
|
116
|
+
-----------
|
|
117
|
+
operation : Callable
|
|
118
|
+
The operation (function) to retry.
|
|
119
|
+
retries : int, optional
|
|
120
|
+
The number of retry attempts (default: 3).
|
|
121
|
+
backoff : float, optional
|
|
122
|
+
The initial backoff time in seconds between retries (default: 1.0).
|
|
123
|
+
|
|
124
|
+
Returns:
|
|
125
|
+
--------
|
|
126
|
+
bool:
|
|
127
|
+
True if the operation succeeded, False if all retries failed.
|
|
128
|
+
"""
|
|
129
|
+
for attempt in range(1, retries + 1):
|
|
130
|
+
try:
|
|
131
|
+
operation()
|
|
132
|
+
print(f"Operation succeeded on attempt {attempt}.")
|
|
133
|
+
return True
|
|
134
|
+
except (
|
|
135
|
+
RuntimeError,
|
|
136
|
+
ValueError,
|
|
137
|
+
) as e: # Catch specific exceptions
|
|
138
|
+
self.log_error(f"Attempt {attempt} failed with error: {e}")
|
|
139
|
+
if attempt < retries:
|
|
140
|
+
sleep_time = backoff * (
|
|
141
|
+
2 ** (attempt - 1)
|
|
142
|
+
) # Exponential backoff
|
|
143
|
+
print(f"Retrying in {sleep_time:.2f} seconds...")
|
|
144
|
+
time.sleep(sleep_time)
|
|
145
|
+
print(f"Operation failed after {retries} attempts.")
|
|
146
|
+
return False
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
# =============================================================================
|
|
150
|
+
# Example Usage
|
|
151
|
+
# =============================================================================
|
|
152
|
+
|
|
153
|
+
if __name__ == "__main__":
|
|
154
|
+
# Initialize the error handler
|
|
155
|
+
error_handler = ErrorHandler()
|
|
156
|
+
|
|
157
|
+
# Example of a failing operation
|
|
158
|
+
def failing_operation():
|
|
159
|
+
"""
|
|
160
|
+
A simulated operation that raises a RuntimeError.
|
|
161
|
+
"""
|
|
162
|
+
raise RuntimeError("Simulated operation failure.")
|
|
163
|
+
|
|
164
|
+
# Log an error
|
|
165
|
+
error_handler.log_error("This is a test error.")
|
|
166
|
+
|
|
167
|
+
# Raise an alert
|
|
168
|
+
error_handler.raise_alert("Critical system failure detected.")
|
|
169
|
+
|
|
170
|
+
# Retry a failing operation
|
|
171
|
+
SUCCESS = error_handler.retry_operation(
|
|
172
|
+
failing_operation, retries=3, backoff=2.0
|
|
173
|
+
)
|
|
174
|
+
if not SUCCESS:
|
|
175
|
+
print("All retry attempts failed.")
|
|
@@ -22,17 +22,15 @@ Todo:
|
|
|
22
22
|
|
|
23
23
|
# Import | Standard Library
|
|
24
24
|
import json
|
|
25
|
-
# […]
|
|
26
25
|
|
|
27
26
|
# Import | Libraries
|
|
28
27
|
import geojson
|
|
29
|
-
|
|
28
|
+
|
|
30
29
|
|
|
31
30
|
# Import | Local Modules
|
|
32
31
|
# […]
|
|
33
32
|
|
|
34
33
|
|
|
35
|
-
|
|
36
34
|
class FileGeoJSON:
|
|
37
35
|
"""
|
|
38
36
|
A class used to represent a GeoJSON File
|
|
@@ -35,6 +35,7 @@ from typing import Any, Dict, List, Optional
|
|
|
35
35
|
# Classes
|
|
36
36
|
# =============================================================================
|
|
37
37
|
|
|
38
|
+
|
|
38
39
|
class INIHandler(object):
|
|
39
40
|
"""
|
|
40
41
|
A class for handling INI configuration files.
|
|
@@ -89,12 +90,14 @@ class INIHandler(object):
|
|
|
89
90
|
config (configparser.ConfigParser): The ConfigParser object to save.
|
|
90
91
|
file_path (str): Path to the INI file where the data will be saved.
|
|
91
92
|
"""
|
|
92
|
-
with open(file_path,
|
|
93
|
+
with open(file_path, "w", encoding="utf-8") as file:
|
|
93
94
|
config.write(file)
|
|
94
95
|
|
|
95
96
|
@staticmethod
|
|
96
97
|
def update_ini(
|
|
97
|
-
config: configparser.ConfigParser,
|
|
98
|
+
config: configparser.ConfigParser,
|
|
99
|
+
section: str,
|
|
100
|
+
updates: Dict[str, Any],
|
|
98
101
|
):
|
|
99
102
|
"""
|
|
100
103
|
Updates a section in the ConfigParser object.
|
|
@@ -109,13 +112,12 @@ class INIHandler(object):
|
|
|
109
112
|
for key, value in updates.items():
|
|
110
113
|
config.set(section, key, str(value))
|
|
111
114
|
|
|
112
|
-
|
|
113
115
|
@staticmethod
|
|
114
116
|
def get_value(
|
|
115
117
|
config: configparser.ConfigParser,
|
|
116
118
|
section: str,
|
|
117
119
|
key: str,
|
|
118
|
-
fallback: Optional[Any] = None
|
|
120
|
+
fallback: Optional[Any] = None,
|
|
119
121
|
) -> Any:
|
|
120
122
|
"""
|
|
121
123
|
Gets a value from a section in the ConfigParser object.
|
|
@@ -180,7 +182,9 @@ class INIHandler(object):
|
|
|
180
182
|
return config.sections()
|
|
181
183
|
|
|
182
184
|
@staticmethod
|
|
183
|
-
def list_keys(
|
|
185
|
+
def list_keys(
|
|
186
|
+
config: configparser.ConfigParser, section: str
|
|
187
|
+
) -> List[str]:
|
|
184
188
|
"""
|
|
185
189
|
Lists all keys in a specific section.
|
|
186
190
|
|
|
@@ -197,7 +201,8 @@ class INIHandler(object):
|
|
|
197
201
|
|
|
198
202
|
@staticmethod
|
|
199
203
|
def has_key(
|
|
200
|
-
config: configparser.ConfigParser, section: str, key: str
|
|
204
|
+
config: configparser.ConfigParser, section: str, key: str
|
|
205
|
+
) -> bool:
|
|
201
206
|
"""
|
|
202
207
|
Checks if a specific key exists in a section.
|
|
203
208
|
|
|
@@ -213,8 +218,7 @@ class INIHandler(object):
|
|
|
213
218
|
|
|
214
219
|
@staticmethod
|
|
215
220
|
def remove_key(
|
|
216
|
-
config: configparser.ConfigParser,
|
|
217
|
-
section: str, key: str
|
|
221
|
+
config: configparser.ConfigParser, section: str, key: str
|
|
218
222
|
) -> bool:
|
|
219
223
|
"""
|
|
220
224
|
Removes a specific key from a section.
|
|
@@ -238,68 +242,70 @@ class INIHandler(object):
|
|
|
238
242
|
# Functions
|
|
239
243
|
# =============================================================================
|
|
240
244
|
|
|
245
|
+
|
|
241
246
|
def test():
|
|
242
247
|
"""
|
|
243
248
|
Test Function
|
|
244
249
|
"""
|
|
245
250
|
|
|
246
251
|
# Example usage
|
|
247
|
-
config = INIHandler.load_ini(
|
|
252
|
+
config = INIHandler.load_ini("config.ini")
|
|
248
253
|
|
|
249
254
|
# Update configuration
|
|
250
255
|
INIHandler.update_ini(
|
|
251
|
-
config,
|
|
256
|
+
config, "section1", {"key1": "value1", "key2": "value2"}
|
|
252
257
|
)
|
|
253
258
|
|
|
254
259
|
# Save configuration
|
|
255
|
-
INIHandler.save_ini(config,
|
|
260
|
+
INIHandler.save_ini(config, "config_updated.ini")
|
|
256
261
|
|
|
257
262
|
# Example usage
|
|
258
|
-
config = INIHandler.load_ini(
|
|
263
|
+
config = INIHandler.load_ini("config.ini")
|
|
259
264
|
|
|
260
265
|
# Get value with a fallback
|
|
261
266
|
value = INIHandler.get_value(
|
|
262
|
-
config,
|
|
267
|
+
config, "section1", "key1", fallback="default"
|
|
263
268
|
)
|
|
264
269
|
|
|
265
270
|
# Add a new section
|
|
266
|
-
added = INIHandler.add_section(config,
|
|
271
|
+
added = INIHandler.add_section(config, "new_section")
|
|
267
272
|
|
|
268
273
|
# Remove a section
|
|
269
|
-
removed = INIHandler.remove_section(config,
|
|
274
|
+
removed = INIHandler.remove_section(config, "section_to_remove")
|
|
270
275
|
|
|
271
276
|
# Save configuration
|
|
272
|
-
INIHandler.save_ini(config,
|
|
277
|
+
INIHandler.save_ini(config, "config_updated.ini")
|
|
273
278
|
|
|
274
279
|
# Example usage
|
|
275
|
-
config = INIHandler.load_ini(
|
|
280
|
+
config = INIHandler.load_ini("config.ini")
|
|
276
281
|
|
|
277
282
|
# List all sections
|
|
278
283
|
sections = INIHandler.list_sections(config)
|
|
279
284
|
print(sections)
|
|
280
285
|
|
|
281
286
|
# List all keys in a section
|
|
282
|
-
keys = INIHandler.list_keys(config,
|
|
287
|
+
keys = INIHandler.list_keys(config, "section1")
|
|
283
288
|
print(keys)
|
|
284
289
|
|
|
285
290
|
# Check if a specific key exists
|
|
286
|
-
exists = INIHandler.has_key(config,
|
|
291
|
+
exists = INIHandler.has_key(config, "section1", "key1")
|
|
287
292
|
print(exists)
|
|
288
293
|
|
|
289
294
|
# Remove a specific key
|
|
290
|
-
removed = INIHandler.remove_key(config,
|
|
295
|
+
removed = INIHandler.remove_key(config, "section1", "key1")
|
|
291
296
|
print(removed)
|
|
292
297
|
|
|
293
298
|
# Save configuration
|
|
294
|
-
INIHandler.save_ini(config,
|
|
299
|
+
INIHandler.save_ini(config, "config_updated.ini")
|
|
295
300
|
|
|
296
301
|
|
|
297
302
|
# =============================================================================
|
|
298
303
|
# Main
|
|
299
304
|
# =============================================================================
|
|
300
305
|
|
|
301
|
-
if __name__ ==
|
|
306
|
+
if __name__ == "__main__":
|
|
302
307
|
"""Main"""
|
|
303
308
|
import doctest
|
|
309
|
+
|
|
304
310
|
doctest.testmod()
|
|
305
311
|
test()
|
|
@@ -22,14 +22,15 @@ Links:
|
|
|
22
22
|
# Import
|
|
23
23
|
# =============================================================================
|
|
24
24
|
|
|
25
|
-
# Import | Standard Library
|
|
26
|
-
from typing import List, Any, Dict, Callable
|
|
27
|
-
import json
|
|
28
|
-
import yaml
|
|
29
25
|
import csv
|
|
26
|
+
import json
|
|
27
|
+
|
|
28
|
+
# Import | Standard Library
|
|
29
|
+
from typing import Any, Callable, Dict, List
|
|
30
30
|
|
|
31
31
|
# Import | Libraries
|
|
32
32
|
import xmltodict # Requires installation: pip install xmltodict
|
|
33
|
+
import yaml
|
|
33
34
|
|
|
34
35
|
# Import | Local Modules
|
|
35
36
|
|
|
@@ -38,6 +39,7 @@ import xmltodict # Requires installation: pip install xmltodict
|
|
|
38
39
|
# Classes
|
|
39
40
|
# =============================================================================
|
|
40
41
|
|
|
42
|
+
|
|
41
43
|
class JSONHandler(object):
|
|
42
44
|
"""
|
|
43
45
|
A class for handling JSON data operations.
|
|
@@ -64,7 +66,7 @@ class JSONHandler(object):
|
|
|
64
66
|
Any: The data read from the JSON file.
|
|
65
67
|
"""
|
|
66
68
|
try:
|
|
67
|
-
with open(file_path,
|
|
69
|
+
with open(file_path, "r", encoding="utf-8") as file:
|
|
68
70
|
return json.load(file)
|
|
69
71
|
except Exception as e:
|
|
70
72
|
print(f"Error reading JSON file: {e}")
|
|
@@ -97,7 +99,7 @@ class JSONHandler(object):
|
|
|
97
99
|
data.
|
|
98
100
|
"""
|
|
99
101
|
try:
|
|
100
|
-
with open(file_path,
|
|
102
|
+
with open(file_path, "w", encoding="utf-8") as file:
|
|
101
103
|
json.dump(data, file, indent=indent)
|
|
102
104
|
except Exception as e:
|
|
103
105
|
print(f"Error writing JSON file: {e}")
|
|
@@ -223,7 +225,7 @@ class JSONHandler(object):
|
|
|
223
225
|
return data
|
|
224
226
|
|
|
225
227
|
@staticmethod
|
|
226
|
-
def flatten_json(data: Dict, parent_key: str =
|
|
228
|
+
def flatten_json(data: Dict, parent_key: str = "", sep: str = ".") -> Dict:
|
|
227
229
|
"""
|
|
228
230
|
Flatten a nested JSON object into a single level.
|
|
229
231
|
|
|
@@ -311,7 +313,7 @@ class JSONHandler(object):
|
|
|
311
313
|
sorted(
|
|
312
314
|
data.items(),
|
|
313
315
|
key=lambda item: item[0 if by_key else 1],
|
|
314
|
-
reverse=reverse
|
|
316
|
+
reverse=reverse,
|
|
315
317
|
)
|
|
316
318
|
)
|
|
317
319
|
|
|
@@ -367,7 +369,7 @@ class JSONHandler(object):
|
|
|
367
369
|
|
|
368
370
|
Note: Only works with flat JSON structures.
|
|
369
371
|
"""
|
|
370
|
-
with open(csv_file,
|
|
372
|
+
with open(csv_file, "w", newline="", encoding="utf-8") as file:
|
|
371
373
|
writer = csv.DictWriter(file, fieldnames=json_data[0].keys())
|
|
372
374
|
writer.writeheader()
|
|
373
375
|
for row in json_data:
|
|
@@ -384,7 +386,7 @@ class JSONHandler(object):
|
|
|
384
386
|
Returns:
|
|
385
387
|
List[Dict]: The converted data in JSON format.
|
|
386
388
|
"""
|
|
387
|
-
with open(csv_file,
|
|
389
|
+
with open(csv_file, "r", encoding="utf-8") as file:
|
|
388
390
|
return list(csv.DictReader(file))
|
|
389
391
|
|
|
390
392
|
@staticmethod
|
|
@@ -399,7 +401,7 @@ class JSONHandler(object):
|
|
|
399
401
|
Returns:
|
|
400
402
|
Any: The extracted data.
|
|
401
403
|
"""
|
|
402
|
-
elements = path.split(
|
|
404
|
+
elements = path.split("/")
|
|
403
405
|
for elem in elements:
|
|
404
406
|
json_data = json_data.get(elem)
|
|
405
407
|
if json_data is None:
|
|
@@ -431,7 +433,7 @@ class JSONHandler(object):
|
|
|
431
433
|
Returns:
|
|
432
434
|
str: Compressed JSON string.
|
|
433
435
|
"""
|
|
434
|
-
return json.dumps(json_data, separators=(
|
|
436
|
+
return json.dumps(json_data, separators=(",", ":"))
|
|
435
437
|
|
|
436
438
|
@staticmethod
|
|
437
439
|
def transform_keys(
|
|
@@ -451,13 +453,13 @@ class JSONHandler(object):
|
|
|
451
453
|
return {
|
|
452
454
|
transform_func(k): JSONHandler.transform_keys(
|
|
453
455
|
v, transform_func
|
|
454
|
-
)
|
|
456
|
+
)
|
|
457
|
+
for k, v in json_data.items()
|
|
455
458
|
}
|
|
456
459
|
elif isinstance(json_data, list):
|
|
457
460
|
return [
|
|
458
|
-
JSONHandler.transform_keys(
|
|
459
|
-
|
|
460
|
-
) for elem in json_data
|
|
461
|
+
JSONHandler.transform_keys(elem, transform_func)
|
|
462
|
+
for elem in json_data
|
|
461
463
|
]
|
|
462
464
|
else:
|
|
463
465
|
return json_data
|
|
@@ -474,7 +476,7 @@ class JSONHandler(object):
|
|
|
474
476
|
Returns:
|
|
475
477
|
bool: True if the path exists, False otherwise.
|
|
476
478
|
"""
|
|
477
|
-
elements = path.split(
|
|
479
|
+
elements = path.split("/")
|
|
478
480
|
for elem in elements:
|
|
479
481
|
if isinstance(json_data, dict) and elem in json_data:
|
|
480
482
|
json_data = json_data[elem]
|
|
@@ -495,7 +497,7 @@ class JSONHandler(object):
|
|
|
495
497
|
Returns:
|
|
496
498
|
Any: The updated JSON data.
|
|
497
499
|
"""
|
|
498
|
-
elements = path.split(
|
|
500
|
+
elements = path.split("/")
|
|
499
501
|
for i, elem in enumerate(elements):
|
|
500
502
|
if i == len(elements) - 1:
|
|
501
503
|
json_data[elem] = value
|
|
@@ -516,7 +518,7 @@ class JSONHandler(object):
|
|
|
516
518
|
data.
|
|
517
519
|
"""
|
|
518
520
|
try:
|
|
519
|
-
with open(file_path,
|
|
521
|
+
with open(file_path, "w", encoding="utf-8") as file:
|
|
520
522
|
json.dump(data, file, indent=indent)
|
|
521
523
|
except Exception as e:
|
|
522
524
|
print(f"Error saving dictionary to JSON file: {e}")
|
|
@@ -527,23 +529,20 @@ class JSONHandler(object):
|
|
|
527
529
|
# Functions
|
|
528
530
|
# =============================================================================
|
|
529
531
|
|
|
532
|
+
|
|
530
533
|
def test():
|
|
531
534
|
"""
|
|
532
535
|
Test Function
|
|
533
536
|
"""
|
|
534
537
|
|
|
535
538
|
# Example usage
|
|
536
|
-
json_data = {
|
|
537
|
-
'name': 'John Doe',
|
|
538
|
-
'age': 30,
|
|
539
|
-
'is_employee': True
|
|
540
|
-
}
|
|
539
|
+
json_data = {"name": "John Doe", "age": 30, "is_employee": True}
|
|
541
540
|
|
|
542
541
|
# Write JSON to file
|
|
543
|
-
JSONHandler.write_json(
|
|
542
|
+
JSONHandler.write_json("user.json", json_data)
|
|
544
543
|
|
|
545
544
|
# Read JSON from file
|
|
546
|
-
read_data = JSONHandler.read_json(
|
|
545
|
+
read_data = JSONHandler.read_json("user.json")
|
|
547
546
|
print(read_data)
|
|
548
547
|
|
|
549
548
|
# Validate JSON
|
|
@@ -591,7 +590,7 @@ def test():
|
|
|
591
590
|
# Deep Merge JSON
|
|
592
591
|
merged_json = JSONHandler.deep_merge_json(
|
|
593
592
|
{"person": {"name": "John", "age": 30}},
|
|
594
|
-
{"person": {"age": 31, "city": "New York"}}
|
|
593
|
+
{"person": {"age": 31, "city": "New York"}},
|
|
595
594
|
)
|
|
596
595
|
print(merged_json)
|
|
597
596
|
|
|
@@ -616,8 +615,8 @@ def test():
|
|
|
616
615
|
|
|
617
616
|
# Convert JSON to CSV and back
|
|
618
617
|
json_data = [{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]
|
|
619
|
-
JSONHandler.json_to_csv(json_data,
|
|
620
|
-
csv_to_json_data = JSONHandler.csv_to_json(
|
|
618
|
+
JSONHandler.json_to_csv(json_data, "data.csv")
|
|
619
|
+
csv_to_json_data = JSONHandler.csv_to_json("data.csv")
|
|
621
620
|
print(csv_to_json_data)
|
|
622
621
|
|
|
623
622
|
# Extract data using path
|
|
@@ -645,22 +644,19 @@ def test():
|
|
|
645
644
|
updated_json = JSONHandler.nested_update({"a": {"b": 1}}, "a/b", 2)
|
|
646
645
|
print(updated_json)
|
|
647
646
|
|
|
648
|
-
data_dict = {
|
|
649
|
-
"name": "John Doe",
|
|
650
|
-
"age": 30,
|
|
651
|
-
"is_employee": True
|
|
652
|
-
}
|
|
647
|
+
data_dict = {"name": "John Doe", "age": 30, "is_employee": True}
|
|
653
648
|
|
|
654
649
|
# Save the dictionary to a JSON file
|
|
655
|
-
JSONHandler.save_dict_to_json(data_dict,
|
|
650
|
+
JSONHandler.save_dict_to_json(data_dict, "data.json")
|
|
656
651
|
|
|
657
652
|
|
|
658
653
|
# =============================================================================
|
|
659
654
|
# Main
|
|
660
655
|
# =============================================================================
|
|
661
656
|
|
|
662
|
-
if __name__ ==
|
|
657
|
+
if __name__ == "__main__":
|
|
663
658
|
"""Main"""
|
|
664
659
|
import doctest
|
|
660
|
+
|
|
665
661
|
doctest.testmod()
|
|
666
662
|
test()
|