MapleX 3.0.0.dev2__tar.gz → 3.0.0.dev3__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.
- {maplex-3.0.0.dev2/src/MapleX.egg-info → maplex-3.0.0.dev3}/PKG-INFO +7 -7
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/README.md +6 -6
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/pyproject.toml +1 -1
- maplex-3.0.0.dev3/readmes/README_Json.md +87 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/readmes/README_Logger.md +10 -10
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3/src/MapleX.egg-info}/PKG-INFO +7 -7
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/src/MapleX.egg-info/SOURCES.txt +1 -1
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/src/maplex/__init__.py +4 -3
- maplex-3.0.0.dev2/src/maplex/mapleJson.py → maplex-3.0.0.dev3/src/maplex/json.py +30 -9
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/src/maplex/mapleLogger.py +155 -125
- maplex-3.0.0.dev2/readmes/README_Json.md +0 -10
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/LICENSE +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/MANIFEST.in +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/logErrorOutputSample.png +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/logOutputSample.png +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/readmes/LoggingTips.md +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/readmes/README_ConsoleColors.md +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/readmes/README_Exceptions.md +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/readmes/README_MapleTree.md +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/setup.cfg +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/src/MapleX.egg-info/dependency_links.txt +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/src/MapleX.egg-info/requires.txt +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/src/MapleX.egg-info/top_level.txt +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/src/maplex/mapleColors.py +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/src/maplex/mapleExceptions.py +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/src/maplex/mapleTreeEditor.py +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/src/maplex/utils.py +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/tests/test_logger_unittest.py +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/tests/test_maplejson_unittest.py +0 -0
- {maplex-3.0.0.dev2 → maplex-3.0.0.dev3}/tests/test_mapletree_unittest.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: MapleX
|
|
3
|
-
Version: 3.0.0.
|
|
3
|
+
Version: 3.0.0.dev3
|
|
4
4
|
Summary: A Python library for simple logging, json file operations, Maple file format operations, and console color utilities.
|
|
5
5
|
Author: RyujiHazama
|
|
6
6
|
Project-URL: PyPI, https://pypi.org/project/MapleX/
|
|
@@ -59,7 +59,7 @@ logger = maplex.getLogger("FunctionName")
|
|
|
59
59
|
logger.info("Hello there!")
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
-
This outputs to console:
|
|
62
|
+
This outputs to the console:
|
|
63
63
|
|
|
64
64
|
```console
|
|
65
65
|
[INFO ][FunctionName] <module>(4) Hello there!
|
|
@@ -77,9 +77,9 @@ Also outputs to the log file:
|
|
|
77
77
|
|
|
78
78
|
## MapleJson
|
|
79
79
|
|
|
80
|
-
MapleJson class is a library class for
|
|
80
|
+
MapleJson class is a library class for converting data between `dict` data and JSON-formatted file data.
|
|
81
81
|
|
|
82
|
-
You can read, write and also encrypt a JSON file as a `dict` data.
|
|
82
|
+
You can read, write, and also encrypt a JSON file as a `dict` data.
|
|
83
83
|
|
|
84
84
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_Json.md)
|
|
85
85
|
|
|
@@ -152,20 +152,20 @@ All datas after "\nEOF\n" will be ignored
|
|
|
152
152
|
|
|
153
153
|
## MapleTree Class
|
|
154
154
|
|
|
155
|
-
MapleTree class is a Python library for
|
|
155
|
+
MapleTree class is a Python library for reading, editing, and writeing the Maple formatted file from the Python code.
|
|
156
156
|
You can also automatically encrypt the file data using the library functions.
|
|
157
157
|
|
|
158
158
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_MapleTree.md#mapletree-class)
|
|
159
159
|
|
|
160
160
|
## Exceptions
|
|
161
161
|
|
|
162
|
-
Excption classes are the specialized classes for MapleX to make your
|
|
162
|
+
Excption classes are the specialized classes for MapleX to make your debugging easier.
|
|
163
163
|
|
|
164
164
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_Exceptions.md#exceptions)
|
|
165
165
|
|
|
166
166
|
## Console Colors
|
|
167
167
|
|
|
168
|
-
ConsoleColors class is a class library for color-
|
|
168
|
+
ConsoleColors class is a class library for color-highlighting the standard output.
|
|
169
169
|
|
|
170
170
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_ConsoleColors.md)
|
|
171
171
|
|
|
@@ -37,7 +37,7 @@ logger = maplex.getLogger("FunctionName")
|
|
|
37
37
|
logger.info("Hello there!")
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
This outputs to console:
|
|
40
|
+
This outputs to the console:
|
|
41
41
|
|
|
42
42
|
```console
|
|
43
43
|
[INFO ][FunctionName] <module>(4) Hello there!
|
|
@@ -55,9 +55,9 @@ Also outputs to the log file:
|
|
|
55
55
|
|
|
56
56
|
## MapleJson
|
|
57
57
|
|
|
58
|
-
MapleJson class is a library class for
|
|
58
|
+
MapleJson class is a library class for converting data between `dict` data and JSON-formatted file data.
|
|
59
59
|
|
|
60
|
-
You can read, write and also encrypt a JSON file as a `dict` data.
|
|
60
|
+
You can read, write, and also encrypt a JSON file as a `dict` data.
|
|
61
61
|
|
|
62
62
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_Json.md)
|
|
63
63
|
|
|
@@ -130,20 +130,20 @@ All datas after "\nEOF\n" will be ignored
|
|
|
130
130
|
|
|
131
131
|
## MapleTree Class
|
|
132
132
|
|
|
133
|
-
MapleTree class is a Python library for
|
|
133
|
+
MapleTree class is a Python library for reading, editing, and writeing the Maple formatted file from the Python code.
|
|
134
134
|
You can also automatically encrypt the file data using the library functions.
|
|
135
135
|
|
|
136
136
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_MapleTree.md#mapletree-class)
|
|
137
137
|
|
|
138
138
|
## Exceptions
|
|
139
139
|
|
|
140
|
-
Excption classes are the specialized classes for MapleX to make your
|
|
140
|
+
Excption classes are the specialized classes for MapleX to make your debugging easier.
|
|
141
141
|
|
|
142
142
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_Exceptions.md#exceptions)
|
|
143
143
|
|
|
144
144
|
## Console Colors
|
|
145
145
|
|
|
146
|
-
ConsoleColors class is a class library for color-
|
|
146
|
+
ConsoleColors class is a class library for color-highlighting the standard output.
|
|
147
147
|
|
|
148
148
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_ConsoleColors.md)
|
|
149
149
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "MapleX"
|
|
7
|
-
version = "3.0.0.
|
|
7
|
+
version = "3.0.0.dev3"
|
|
8
8
|
description = """A Python library for simple logging, json file operations, Maple file format operations, and console color utilities."""
|
|
9
9
|
keywords = ["logging", "logger", "json", "file operations", "maple file format", "console colors"]
|
|
10
10
|
readme = "README.md"
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# MapleJson Class
|
|
2
|
+
|
|
3
|
+
MapleJson class is a class library to manage the JSON formatted files.
|
|
4
|
+
|
|
5
|
+
- You can read a JSON file as a `dict` data.
|
|
6
|
+
- You can write the `dict` data into a file as a JSON formatted string
|
|
7
|
+
- You can save the data as an encrypted data string.
|
|
8
|
+
- You can decrypt the encrypted data.
|
|
9
|
+
|
|
10
|
+
## Class Initialization
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
|
|
14
|
+
def __init__(
|
|
15
|
+
filePath: str,
|
|
16
|
+
fileEncoding: str = 'utf-8',
|
|
17
|
+
indent: int = 4,
|
|
18
|
+
ensure_ascii: bool = False,
|
|
19
|
+
encrypt: bool = False,
|
|
20
|
+
key: bytes = None
|
|
21
|
+
) -> None:
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
|Property|Required|Value|Version|
|
|
25
|
+
|--------|--------|-----|-------|
|
|
26
|
+
|**filePath**|\*|JSON file path|3.0.0|
|
|
27
|
+
|**fileEncoding**||File encoding|3.0.0|
|
|
28
|
+
|**indent**||Indent size for save as a JSON file|3.0.0|
|
|
29
|
+
|**ensureAscii**||Ensure ASCII flag when save to a file|3.0.0|
|
|
30
|
+
|**encrypt**||Encryption flag|3.0.0|
|
|
31
|
+
|**key**|(\*)|Encryption key|3.0.0|
|
|
32
|
+
|
|
33
|
+
Initialize the class with a file path.
|
|
34
|
+
|
|
35
|
+
### File encoding
|
|
36
|
+
|
|
37
|
+
You can set a specific file character encoding. Default: `UTF-8`
|
|
38
|
+
|
|
39
|
+
E.g.: If you are using `Shift_JIS` (Japanese system), you should initialize the class like the example below:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from maplex import MapleJson
|
|
43
|
+
|
|
44
|
+
jsonInstance = MapleJson("jsonFile.json", fileEncoding="shift_jis")
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Indent size
|
|
48
|
+
|
|
49
|
+
You can set the block indent size with `indent` parameter. Default: `4`
|
|
50
|
+
|
|
51
|
+
If you set `indent=2`, the file will be save like the example below:
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"Data": {
|
|
56
|
+
"Key": "Value",
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Ensure ASCII
|
|
62
|
+
|
|
63
|
+
You can set the flag to ensure ASCII encoding.
|
|
64
|
+
|
|
65
|
+
If you don't set the parameter (Default: `False`), the file contents after saving are look like the example below:
|
|
66
|
+
|
|
67
|
+
```json
|
|
68
|
+
{
|
|
69
|
+
"data": {
|
|
70
|
+
"japanese": "値",
|
|
71
|
+
"russian": "значение",
|
|
72
|
+
"english": "value"
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
But, if you set `ensureAscii=True`, the file contents after saving will be changed like:
|
|
78
|
+
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"data": {
|
|
82
|
+
"japanese": "\u5024",
|
|
83
|
+
"russian": "\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435",
|
|
84
|
+
"english": "value"
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
@@ -5,16 +5,16 @@
|
|
|
5
5
|
## Logger Initialization
|
|
6
6
|
|
|
7
7
|
```python
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
def __init__(
|
|
9
|
+
func: str = "",
|
|
10
|
+
workingDirectory: str | None = None,
|
|
11
|
+
cmdLogLevel: str | None = None,
|
|
12
|
+
fileLogLevel: str | None = None,
|
|
13
|
+
maxLogSize: float | None = None,
|
|
14
|
+
fileMode: Literal["append", "overwrite", "daily"] | None = None,
|
|
15
|
+
configFile: str = "config.json",
|
|
16
|
+
encoding: str | None = None,
|
|
17
|
+
) -> None:
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
|Property|Required|Value|Version|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: MapleX
|
|
3
|
-
Version: 3.0.0.
|
|
3
|
+
Version: 3.0.0.dev3
|
|
4
4
|
Summary: A Python library for simple logging, json file operations, Maple file format operations, and console color utilities.
|
|
5
5
|
Author: RyujiHazama
|
|
6
6
|
Project-URL: PyPI, https://pypi.org/project/MapleX/
|
|
@@ -59,7 +59,7 @@ logger = maplex.getLogger("FunctionName")
|
|
|
59
59
|
logger.info("Hello there!")
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
-
This outputs to console:
|
|
62
|
+
This outputs to the console:
|
|
63
63
|
|
|
64
64
|
```console
|
|
65
65
|
[INFO ][FunctionName] <module>(4) Hello there!
|
|
@@ -77,9 +77,9 @@ Also outputs to the log file:
|
|
|
77
77
|
|
|
78
78
|
## MapleJson
|
|
79
79
|
|
|
80
|
-
MapleJson class is a library class for
|
|
80
|
+
MapleJson class is a library class for converting data between `dict` data and JSON-formatted file data.
|
|
81
81
|
|
|
82
|
-
You can read, write and also encrypt a JSON file as a `dict` data.
|
|
82
|
+
You can read, write, and also encrypt a JSON file as a `dict` data.
|
|
83
83
|
|
|
84
84
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_Json.md)
|
|
85
85
|
|
|
@@ -152,20 +152,20 @@ All datas after "\nEOF\n" will be ignored
|
|
|
152
152
|
|
|
153
153
|
## MapleTree Class
|
|
154
154
|
|
|
155
|
-
MapleTree class is a Python library for
|
|
155
|
+
MapleTree class is a Python library for reading, editing, and writeing the Maple formatted file from the Python code.
|
|
156
156
|
You can also automatically encrypt the file data using the library functions.
|
|
157
157
|
|
|
158
158
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_MapleTree.md#mapletree-class)
|
|
159
159
|
|
|
160
160
|
## Exceptions
|
|
161
161
|
|
|
162
|
-
Excption classes are the specialized classes for MapleX to make your
|
|
162
|
+
Excption classes are the specialized classes for MapleX to make your debugging easier.
|
|
163
163
|
|
|
164
164
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_Exceptions.md#exceptions)
|
|
165
165
|
|
|
166
166
|
## Console Colors
|
|
167
167
|
|
|
168
|
-
ConsoleColors class is a class library for color-
|
|
168
|
+
ConsoleColors class is a class library for color-highlighting the standard output.
|
|
169
169
|
|
|
170
170
|
[More details](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_ConsoleColors.md)
|
|
171
171
|
|
|
@@ -16,9 +16,9 @@ src/MapleX.egg-info/dependency_links.txt
|
|
|
16
16
|
src/MapleX.egg-info/requires.txt
|
|
17
17
|
src/MapleX.egg-info/top_level.txt
|
|
18
18
|
src/maplex/__init__.py
|
|
19
|
+
src/maplex/json.py
|
|
19
20
|
src/maplex/mapleColors.py
|
|
20
21
|
src/maplex/mapleExceptions.py
|
|
21
|
-
src/maplex/mapleJson.py
|
|
22
22
|
src/maplex/mapleLogger.py
|
|
23
23
|
src/maplex/mapleTreeEditor.py
|
|
24
24
|
src/maplex/utils.py
|
|
@@ -4,8 +4,8 @@ Logger: A simple logging utility for tracking events and debugging.
|
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
from .mapleColors import ConsoleColors
|
|
7
|
-
from .
|
|
8
|
-
from .mapleLogger import Logger, getLogger
|
|
7
|
+
from .json import MapleJson
|
|
8
|
+
from .mapleLogger import Logger, getLogger, getDailyLogger
|
|
9
9
|
from .mapleExceptions import (
|
|
10
10
|
InvalidMapleFileFormatException,
|
|
11
11
|
KeyEmptyException,
|
|
@@ -26,6 +26,7 @@ from .utils import winHide, winUnHide
|
|
|
26
26
|
|
|
27
27
|
__all__ = [
|
|
28
28
|
'ConsoleColors',
|
|
29
|
+
'getDailyLogger',
|
|
29
30
|
'getLogger',
|
|
30
31
|
'InvalidMapleFileFormatException',
|
|
31
32
|
'KeyEmptyException',
|
|
@@ -47,6 +48,6 @@ __all__ = [
|
|
|
47
48
|
'winUnHide'
|
|
48
49
|
]
|
|
49
50
|
|
|
50
|
-
__version__ = "3.0.0.
|
|
51
|
+
__version__ = "3.0.0.dev3"
|
|
51
52
|
__author__ = "Ryuji Hazama"
|
|
52
53
|
__license__ = "MIT"
|
|
@@ -6,12 +6,19 @@ from . import mapleExceptions as mExc
|
|
|
6
6
|
|
|
7
7
|
class MapleJson:
|
|
8
8
|
|
|
9
|
-
def __init__(self,
|
|
9
|
+
def __init__(self,
|
|
10
|
+
filePath: str,
|
|
11
|
+
fileEncoding: str = 'utf-8',
|
|
12
|
+
indent: int = 4,
|
|
13
|
+
ensureAscii: bool = False,
|
|
14
|
+
encrypt: bool = False,
|
|
15
|
+
key: bytes = None
|
|
16
|
+
) -> None:
|
|
10
17
|
|
|
11
18
|
self.filePath = filePath
|
|
12
19
|
self.fileEncoding = fileEncoding
|
|
13
20
|
self.indent = indent
|
|
14
|
-
self.
|
|
21
|
+
self.ensureAscii = ensureAscii
|
|
15
22
|
self.encrypt = encrypt
|
|
16
23
|
self.key = key
|
|
17
24
|
self.fernet = Fernet(key) if encrypt and key else None
|
|
@@ -46,11 +53,11 @@ class MapleJson:
|
|
|
46
53
|
|
|
47
54
|
def getEnsureAscii(self) -> bool:
|
|
48
55
|
|
|
49
|
-
return self.
|
|
56
|
+
return self.ensureAscii
|
|
50
57
|
|
|
51
|
-
def setEnsureAscii(self,
|
|
58
|
+
def setEnsureAscii(self, ensureAscii: bool) -> None:
|
|
52
59
|
|
|
53
|
-
self.
|
|
60
|
+
self.ensureAscii = ensureAscii
|
|
54
61
|
|
|
55
62
|
def isEncrypted(self) -> bool:
|
|
56
63
|
|
|
@@ -80,7 +87,7 @@ class MapleJson:
|
|
|
80
87
|
#####################
|
|
81
88
|
# Basic File Operations
|
|
82
89
|
|
|
83
|
-
def read(self) -> dict:
|
|
90
|
+
def read(self, *keys) -> dict | None:
|
|
84
91
|
|
|
85
92
|
try:
|
|
86
93
|
|
|
@@ -91,12 +98,26 @@ class MapleJson:
|
|
|
91
98
|
if self.encrypt and self.fernet:
|
|
92
99
|
|
|
93
100
|
decryptedData = self.fernet.decrypt(data)
|
|
94
|
-
|
|
101
|
+
jsonData = json.loads(decryptedData.decode(self.fileEncoding))
|
|
95
102
|
|
|
96
103
|
else:
|
|
97
104
|
|
|
98
|
-
|
|
105
|
+
jsonData = json.loads(data.decode(self.fileEncoding))
|
|
99
106
|
|
|
107
|
+
# Navigate through keys if provided
|
|
108
|
+
|
|
109
|
+
if keys:
|
|
110
|
+
|
|
111
|
+
for jsonKey in keys:
|
|
112
|
+
|
|
113
|
+
if jsonData is None:
|
|
114
|
+
|
|
115
|
+
return None
|
|
116
|
+
|
|
117
|
+
jsonData = jsonData.get(jsonKey, None)
|
|
118
|
+
|
|
119
|
+
return jsonData
|
|
120
|
+
|
|
100
121
|
except FileNotFoundError:
|
|
101
122
|
|
|
102
123
|
raise mExc.MapleFileNotFoundException(self.filePath)
|
|
@@ -113,7 +134,7 @@ class MapleJson:
|
|
|
113
134
|
|
|
114
135
|
raise mExc.MapleTypeException(self.filePath, "Data to write must be a dictionary")
|
|
115
136
|
|
|
116
|
-
jsonData = json.dumps(data, indent=self.indent, ensure_ascii=self.
|
|
137
|
+
jsonData = json.dumps(data, indent=self.indent, ensure_ascii=self.ensureAscii).encode(self.fileEncoding)
|
|
117
138
|
|
|
118
139
|
if self.encrypt and self.fernet:
|
|
119
140
|
|
|
@@ -6,7 +6,7 @@ import sys
|
|
|
6
6
|
import traceback
|
|
7
7
|
from enum import IntEnum
|
|
8
8
|
from typing import Literal
|
|
9
|
-
from .
|
|
9
|
+
from .json import MapleJson
|
|
10
10
|
from .mapleColors import ConsoleColors
|
|
11
11
|
from .mapleExceptions import *
|
|
12
12
|
|
|
@@ -38,18 +38,71 @@ class Logger:
|
|
|
38
38
|
self.fileMode = "append" if fileMode is None else fileMode
|
|
39
39
|
self.encoding = encoding
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
try:
|
|
42
|
+
|
|
43
|
+
# Check the OS (Windows 10 or older cannot change the console color)
|
|
44
|
+
|
|
45
|
+
if hasattr(sys, "getwindowsversion") and sys.getwindowsversion().build < 22000:
|
|
46
|
+
|
|
47
|
+
self.consoleColors = ConsoleColors(Black="", Red="", Green="", Yellow="", Blue="", Magenta="", LightBlue="", White="",
|
|
48
|
+
bgBlack="", bgRed="", bgGreen="", bgYellow="", bgBlue="", bgMagenta="", bgLightBlue="", bgWhite="",
|
|
49
|
+
bBlack="", bRed="", bGreen="", bYellow="", bBlue="", bMagenta="", bLightBlue="", bWhite="",
|
|
50
|
+
Bold="", Underline="", Reversed="", Reset="")
|
|
51
|
+
|
|
52
|
+
logConfInstance = self.__checkConfigFile(configFile)
|
|
53
|
+
self.__checkOutputDirectory(workingDirectory)
|
|
54
|
+
self.__setLogFileName(self.fileMode)
|
|
55
|
+
self.__setFuncName(kwargs.get("getLogger", False), func)
|
|
56
|
+
self.__setLogFileSize(maxLogSize)
|
|
57
|
+
self.__setOutputLogLevels(cmdLogLevel, fileLogLevel)
|
|
58
|
+
self.__setFileEncoding(encoding)
|
|
59
|
+
|
|
60
|
+
# Save config file
|
|
61
|
+
|
|
62
|
+
if logConfInstance is not None:
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
|
|
66
|
+
confJson = logConfInstance.read()
|
|
67
|
+
|
|
68
|
+
except Exception:
|
|
69
|
+
|
|
70
|
+
confJson = {}
|
|
71
|
+
|
|
72
|
+
try:
|
|
73
|
+
|
|
74
|
+
confJson[self.CONFIG_KEY] = self.logConf
|
|
75
|
+
logConfInstance.write(confJson)
|
|
76
|
+
|
|
77
|
+
except Exception as ex:
|
|
42
78
|
|
|
43
|
-
|
|
79
|
+
print(f"{self.consoleColors.Red}Warning: Failed to write logger config file: {ex}{self.consoleColors.Reset}")
|
|
80
|
+
|
|
81
|
+
except Exception as ex:
|
|
82
|
+
|
|
83
|
+
print(f"{self.consoleColors.Red}Error initializing logger: {ex}{self.consoleColors.Reset}")
|
|
84
|
+
raise MapleLoggerException(f"Error initializing logger: {ex}") from ex
|
|
85
|
+
|
|
86
|
+
#
|
|
87
|
+
#####################
|
|
88
|
+
# Set log level enum
|
|
89
|
+
|
|
90
|
+
class LogLevel(IntEnum):
|
|
91
|
+
|
|
92
|
+
TRACE = 0
|
|
93
|
+
DEBUG = 1
|
|
94
|
+
INFO = 2
|
|
95
|
+
WARN = 3
|
|
96
|
+
ERROR = 4
|
|
97
|
+
FATAL = 5
|
|
98
|
+
NONE = 6
|
|
44
99
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
bBlack="", bRed="", bGreen="", bYellow="", bBlue="", bMagenta="", bLightBlue="", bWhite="",
|
|
48
|
-
Bold="", Underline="", Reversed="", Reset="")
|
|
100
|
+
###########################################
|
|
101
|
+
# Special Methods for Class Initialization
|
|
49
102
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
103
|
+
def __checkConfigFile(self, configFile: str) -> MapleJson | None:
|
|
104
|
+
|
|
105
|
+
'''Check logger config file and read settings'''
|
|
53
106
|
|
|
54
107
|
self.CONFIG_KEY = "MapleLogger"
|
|
55
108
|
self.CONSOLE_LOG_LEVEL = "ConsoleLogLevel"
|
|
@@ -60,13 +113,7 @@ class Logger:
|
|
|
60
113
|
|
|
61
114
|
# Set config file path
|
|
62
115
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
self.configFile = configFile
|
|
66
|
-
|
|
67
|
-
else:
|
|
68
|
-
|
|
69
|
-
self.configFile = path.join(self.CWD, configFile)
|
|
116
|
+
self.configFile = self.__checkFilePath(configFile)
|
|
70
117
|
|
|
71
118
|
# Try to read config file
|
|
72
119
|
|
|
@@ -100,29 +147,55 @@ class Logger:
|
|
|
100
147
|
logConf[self.MAX_LOG_SIZE] = 3
|
|
101
148
|
logConf[self.WORKING_DIRECTORY] = "logs"
|
|
102
149
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
150
|
+
self.logConf = logConf
|
|
151
|
+
return logConfInstance
|
|
152
|
+
|
|
153
|
+
def __checkFilePath(self, filePath: str) -> str:
|
|
154
|
+
|
|
155
|
+
'''Check and return absolute file path'''
|
|
156
|
+
|
|
157
|
+
if path.isabs(filePath):
|
|
158
|
+
|
|
159
|
+
return filePath
|
|
160
|
+
|
|
161
|
+
else:
|
|
162
|
+
|
|
163
|
+
return path.join(os.getcwd(), filePath)
|
|
108
164
|
|
|
109
|
-
|
|
165
|
+
def __checkOutputDirectory(self, outputDir: str) -> None:
|
|
166
|
+
|
|
167
|
+
'''Check and set output directory'''
|
|
168
|
+
|
|
169
|
+
# Check parameter and config file
|
|
170
|
+
|
|
171
|
+
if outputDir is not None:
|
|
172
|
+
|
|
173
|
+
self.CWD = outputDir
|
|
110
174
|
|
|
111
175
|
else:
|
|
112
176
|
|
|
113
|
-
self.CWD = logConf.get(self.WORKING_DIRECTORY, None)
|
|
177
|
+
self.CWD = self.logConf.get(self.WORKING_DIRECTORY, None)
|
|
178
|
+
|
|
179
|
+
# Set absolute path
|
|
114
180
|
|
|
115
181
|
if self.CWD in {"", None}:
|
|
116
182
|
|
|
117
183
|
self.CWD = path.join(os.getcwd(), "logs")
|
|
118
|
-
logConf[self.WORKING_DIRECTORY] = self.CWD
|
|
184
|
+
self.logConf[self.WORKING_DIRECTORY] = self.CWD
|
|
119
185
|
|
|
120
186
|
elif not path.isabs(self.CWD):
|
|
121
187
|
|
|
122
188
|
self.CWD = path.join(os.getcwd(), self.CWD)
|
|
123
189
|
|
|
124
|
-
|
|
125
|
-
|
|
190
|
+
# Check if directory exists
|
|
191
|
+
|
|
192
|
+
if not path.isdir(self.CWD):
|
|
193
|
+
|
|
194
|
+
os.makedirs(self.CWD)
|
|
195
|
+
|
|
196
|
+
def __setLogFileName(self, fileMode: str) -> None:
|
|
197
|
+
|
|
198
|
+
'''Set log file name'''
|
|
126
199
|
|
|
127
200
|
if fileMode == "daily":
|
|
128
201
|
|
|
@@ -132,26 +205,15 @@ class Logger:
|
|
|
132
205
|
|
|
133
206
|
self.logfile = path.join(self.CWD, "AppLog.log")
|
|
134
207
|
|
|
135
|
-
|
|
136
|
-
############################
|
|
137
|
-
# Check log directory
|
|
138
|
-
|
|
139
|
-
if not path.isdir(path.join(self.CWD)):
|
|
140
|
-
os.makedirs(path.join(self.CWD))
|
|
141
|
-
|
|
142
|
-
#
|
|
143
|
-
############################
|
|
144
|
-
# Set function name
|
|
145
|
-
|
|
146
|
-
isGetLogger = kwargs.get("getLogger", False)
|
|
208
|
+
def __setFuncName(self, isGetLogger: bool, func: str | None = None) -> None:
|
|
147
209
|
|
|
148
210
|
if isGetLogger:
|
|
149
211
|
|
|
150
|
-
caller = inspect.
|
|
212
|
+
caller = inspect.stack()[3].frame.f_globals.get("__name__", "")
|
|
151
213
|
|
|
152
214
|
else:
|
|
153
215
|
|
|
154
|
-
caller = inspect.
|
|
216
|
+
caller = inspect.stack()[2].frame.f_globals.get("__name__", "")
|
|
155
217
|
|
|
156
218
|
if func in {None, ""}:
|
|
157
219
|
|
|
@@ -168,9 +230,7 @@ class Logger:
|
|
|
168
230
|
self.func = ""
|
|
169
231
|
self.callerName = f"{caller}."
|
|
170
232
|
|
|
171
|
-
|
|
172
|
-
############################
|
|
173
|
-
# Set max log file size
|
|
233
|
+
def __setLogFileSize(self, maxLogSize: any) -> None:
|
|
174
234
|
|
|
175
235
|
self.maxLogSize = 0
|
|
176
236
|
|
|
@@ -182,7 +242,7 @@ class Logger:
|
|
|
182
242
|
|
|
183
243
|
try:
|
|
184
244
|
|
|
185
|
-
logSize = logConf.get(self.MAX_LOG_SIZE, None)
|
|
245
|
+
logSize = self.logConf.get(self.MAX_LOG_SIZE, None)
|
|
186
246
|
|
|
187
247
|
if logSize is not None:
|
|
188
248
|
|
|
@@ -191,7 +251,7 @@ class Logger:
|
|
|
191
251
|
else:
|
|
192
252
|
|
|
193
253
|
self.maxLogSize = 3000000
|
|
194
|
-
logConf[self.MAX_LOG_SIZE] = 3
|
|
254
|
+
self.logConf[self.MAX_LOG_SIZE] = 3
|
|
195
255
|
|
|
196
256
|
except MapleLoggerException as ex:
|
|
197
257
|
|
|
@@ -203,64 +263,38 @@ class Logger:
|
|
|
203
263
|
print(f"{self.consoleColors.Red}Warning: Infinite log file size is not recommended. Using default value.{self.consoleColors.Reset}")
|
|
204
264
|
self.maxLogSize = 3000000
|
|
205
265
|
|
|
206
|
-
|
|
207
|
-
############################
|
|
208
|
-
# Set output log levels
|
|
266
|
+
def __setOutputLogLevels(self, cmdLogLevel: any, fileLogLevel: any) -> None:
|
|
209
267
|
|
|
210
|
-
self.consoleLogLevel =
|
|
211
|
-
self.fileLogLevel =
|
|
268
|
+
self.consoleLogLevel = self.__setLogLevel(self.CONSOLE_LOG_LEVEL, cmdLogLevel)
|
|
269
|
+
self.fileLogLevel = self.__setLogLevel(self.FILE_LOG_LEVEL, fileLogLevel)
|
|
212
270
|
|
|
213
|
-
|
|
271
|
+
def __setLogLevel(self, fileOrConsole, loglevel: any) -> LogLevel:
|
|
214
272
|
|
|
215
|
-
|
|
273
|
+
'''Set log level'''
|
|
216
274
|
|
|
217
|
-
|
|
275
|
+
if loglevel is not None:
|
|
218
276
|
|
|
277
|
+
tempLogLevel = loglevel
|
|
278
|
+
|
|
219
279
|
else:
|
|
220
280
|
|
|
221
|
-
|
|
281
|
+
tempLogLevel = self.logConf.get(fileOrConsole, "INFO")
|
|
222
282
|
|
|
223
|
-
if
|
|
283
|
+
if tempLogLevel is None:
|
|
224
284
|
|
|
225
|
-
|
|
226
|
-
logConf[
|
|
285
|
+
tempLogLevel = "INFO"
|
|
286
|
+
self.logConf[fileOrConsole] = tempLogLevel
|
|
227
287
|
|
|
228
288
|
try:
|
|
229
289
|
|
|
230
|
-
|
|
290
|
+
return self.toLogLevel(tempLogLevel)
|
|
231
291
|
|
|
232
292
|
except MapleInvalidLoggerLevelException as ex:
|
|
233
293
|
|
|
234
|
-
print(f"{self.consoleColors.Red}Warning: Invalid
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
# File log level
|
|
238
|
-
|
|
239
|
-
if fileLogLevel is not None:
|
|
240
|
-
|
|
241
|
-
self.fileLogLevel = self.isLogLevel(fileLogLevel)
|
|
242
|
-
|
|
243
|
-
else:
|
|
244
|
-
|
|
245
|
-
fileLogLevel = logConf.get(self.FILE_LOG_LEVEL, None)
|
|
246
|
-
|
|
247
|
-
if fileLogLevel is None:
|
|
294
|
+
print(f"{self.consoleColors.Red}Warning: Invalid {fileOrConsole} provided: [{tempLogLevel}]. Using default value.{self.consoleColors.Reset}")
|
|
295
|
+
return self.LogLevel.INFO
|
|
248
296
|
|
|
249
|
-
|
|
250
|
-
logConf[self.FILE_LOG_LEVEL] = fileLogLevel
|
|
251
|
-
|
|
252
|
-
try:
|
|
253
|
-
|
|
254
|
-
self.fileLogLevel = self.toLogLevel(fileLogLevel)
|
|
255
|
-
|
|
256
|
-
except MapleInvalidLoggerLevelException as ex:
|
|
257
|
-
|
|
258
|
-
print(f"{self.consoleColors.Red}Warning: Invalid file log level provided: [{fileLogLevel}]. Using default value.{self.consoleColors.Reset}")
|
|
259
|
-
self.fileLogLevel = self.LogLevel.INFO
|
|
260
|
-
|
|
261
|
-
#
|
|
262
|
-
############################
|
|
263
|
-
# Set file encoding
|
|
297
|
+
def __setFileEncoding(self, encoding: str) -> None:
|
|
264
298
|
|
|
265
299
|
if encoding is not None:
|
|
266
300
|
|
|
@@ -268,44 +302,19 @@ class Logger:
|
|
|
268
302
|
|
|
269
303
|
else:
|
|
270
304
|
|
|
271
|
-
fileEncoding = logConf.get(self.FILE_ENCODING, None)
|
|
305
|
+
fileEncoding = self.logConf.get(self.FILE_ENCODING, None)
|
|
272
306
|
|
|
273
307
|
if fileEncoding is None:
|
|
274
308
|
|
|
275
309
|
fileEncoding = "utf-8"
|
|
276
|
-
logConf[self.FILE_ENCODING] = fileEncoding
|
|
310
|
+
self.logConf[self.FILE_ENCODING] = fileEncoding
|
|
277
311
|
|
|
278
312
|
self.encoding = fileEncoding
|
|
279
313
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
if logConfInstance is not None:
|
|
283
|
-
|
|
284
|
-
try:
|
|
285
|
-
|
|
286
|
-
confJson[self.CONFIG_KEY] = logConf
|
|
287
|
-
logConfInstance.write(confJson)
|
|
288
|
-
|
|
289
|
-
except Exception as ex:
|
|
290
|
-
|
|
291
|
-
print(f"{self.consoleColors.Red}Warning: Failed to write logger config file: {ex}{self.consoleColors.Reset}")
|
|
292
|
-
|
|
293
|
-
#
|
|
294
|
-
#####################
|
|
295
|
-
# Set log level enum
|
|
296
|
-
|
|
297
|
-
class LogLevel(IntEnum):
|
|
298
|
-
|
|
299
|
-
TRACE = 0
|
|
300
|
-
DEBUG = 1
|
|
301
|
-
INFO = 2
|
|
302
|
-
WARN = 3
|
|
303
|
-
ERROR = 4
|
|
304
|
-
FATAL = 5
|
|
305
|
-
NONE = 6
|
|
314
|
+
# Class initialization ends here
|
|
315
|
+
#################################
|
|
306
316
|
|
|
307
|
-
|
|
308
|
-
#####################
|
|
317
|
+
#################################
|
|
309
318
|
# Getters and Setters
|
|
310
319
|
|
|
311
320
|
def getLogFile(self) -> str:
|
|
@@ -454,8 +463,12 @@ class Logger:
|
|
|
454
463
|
|
|
455
464
|
def isLogLevel(self, lLStr: str) -> LogLevel:
|
|
456
465
|
|
|
466
|
+
'''Check if string is a valid log level'''
|
|
467
|
+
|
|
468
|
+
logLevelStr = lLStr.upper()
|
|
469
|
+
|
|
457
470
|
for lLevel in self.LogLevel:
|
|
458
|
-
if
|
|
471
|
+
if logLevelStr == lLevel.name:
|
|
459
472
|
return lLevel
|
|
460
473
|
|
|
461
474
|
return -1
|
|
@@ -676,7 +689,7 @@ class Logger:
|
|
|
676
689
|
################################
|
|
677
690
|
# Save log settings
|
|
678
691
|
|
|
679
|
-
def saveLogSettings(self, configFile: str =
|
|
692
|
+
def saveLogSettings(self, configFile: str = None) -> None:
|
|
680
693
|
|
|
681
694
|
"""Save current log settings to config file"""
|
|
682
695
|
|
|
@@ -684,13 +697,11 @@ class Logger:
|
|
|
684
697
|
|
|
685
698
|
# Set config file path
|
|
686
699
|
|
|
687
|
-
if
|
|
700
|
+
if configFile is None:
|
|
688
701
|
|
|
689
|
-
|
|
702
|
+
configFile = self.configFile
|
|
690
703
|
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
configFilePath = path.join(os.getcwd(), configFile)
|
|
704
|
+
configFilePath = self.__checkFilePath(configFile)
|
|
694
705
|
|
|
695
706
|
# Try to read config file
|
|
696
707
|
|
|
@@ -748,6 +759,25 @@ def getLogger(name: str = "", **kwargs) -> Logger:
|
|
|
748
759
|
if name not in _loggers:
|
|
749
760
|
kwargs["getLogger"] = True
|
|
750
761
|
_loggers[name] = Logger(func=name, **kwargs)
|
|
762
|
+
|
|
763
|
+
return _loggers[name]
|
|
764
|
+
|
|
765
|
+
def getDailyLogger(name: str = "", **kwargs) -> Logger:
|
|
766
|
+
"""
|
|
767
|
+
Get or create a daily Logger instance.
|
|
768
|
+
|
|
769
|
+
Args:
|
|
770
|
+
name: Logger name (usually __name__ of the calling module)
|
|
771
|
+
**kwargs: Arguments to pass to Logger constructor if creating new instance
|
|
772
|
+
|
|
773
|
+
Returns:
|
|
774
|
+
Logger instance
|
|
775
|
+
"""
|
|
776
|
+
|
|
777
|
+
if name not in _loggers:
|
|
778
|
+
kwargs["getLogger"] = True
|
|
779
|
+
_loggers[name] = Logger(func=name, fileMode="daily", **kwargs)
|
|
780
|
+
|
|
751
781
|
return _loggers[name]
|
|
752
782
|
|
|
753
783
|
""" * * * * * * * * * * * * * """
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
# MapleJson Class
|
|
2
|
-
|
|
3
|
-
MapleJson class is a class library to manage the JSON formatted files.
|
|
4
|
-
|
|
5
|
-
- You can read a JSON file as a `dict` data.
|
|
6
|
-
- You can write the `dict` data into a file as a JSON formatted string
|
|
7
|
-
- You can save the data as an encrypted data string.
|
|
8
|
-
- You can decrypt the encrypted data.
|
|
9
|
-
|
|
10
|
-
## Class Initialization
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|