MapleX 2.2.0.dev2__py3-none-any.whl → 3.0.0.dev2__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.
- maplex/__init__.py +11 -2
- maplex/mapleColors.py +1 -0
- maplex/mapleExceptions.py +35 -0
- maplex/mapleJson.py +162 -0
- maplex/mapleLogger.py +428 -63
- maplex/mapleTreeEditor.py +344 -27
- maplex-3.0.0.dev2.dist-info/METADATA +197 -0
- maplex-3.0.0.dev2.dist-info/RECORD +12 -0
- {maplex-2.2.0.dev2.dist-info → maplex-3.0.0.dev2.dist-info}/WHEEL +1 -1
- {maplex-2.2.0.dev2.dist-info → maplex-3.0.0.dev2.dist-info}/licenses/LICENSE +1 -1
- maplex-2.2.0.dev2.dist-info/METADATA +0 -921
- maplex-2.2.0.dev2.dist-info/RECORD +0 -11
- {maplex-2.2.0.dev2.dist-info → maplex-3.0.0.dev2.dist-info}/top_level.txt +0 -0
maplex/__init__.py
CHANGED
|
@@ -4,17 +4,21 @@ Logger: A simple logging utility for tracking events and debugging.
|
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
from .mapleColors import ConsoleColors
|
|
7
|
-
from .
|
|
7
|
+
from .mapleJson import MapleJson
|
|
8
|
+
from .mapleLogger import Logger, getLogger
|
|
8
9
|
from .mapleExceptions import (
|
|
9
10
|
InvalidMapleFileFormatException,
|
|
10
11
|
KeyEmptyException,
|
|
11
12
|
MapleDataNotFoundException,
|
|
12
13
|
MapleException,
|
|
14
|
+
MapleEncryptionNotEnabledException,
|
|
13
15
|
MapleFileEmptyException,
|
|
14
16
|
MapleFileLockedException,
|
|
15
17
|
MapleFileNotFoundException,
|
|
16
18
|
MapleHeaderNotFoundException,
|
|
19
|
+
MapleSyntaxException,
|
|
17
20
|
MapleTagNotFoundException,
|
|
21
|
+
MapleTypeException,
|
|
18
22
|
NotAMapleFileException
|
|
19
23
|
)
|
|
20
24
|
from .mapleTreeEditor import MapleTree
|
|
@@ -22,15 +26,20 @@ from .utils import winHide, winUnHide
|
|
|
22
26
|
|
|
23
27
|
__all__ = [
|
|
24
28
|
'ConsoleColors',
|
|
29
|
+
'getLogger',
|
|
25
30
|
'InvalidMapleFileFormatException',
|
|
26
31
|
'KeyEmptyException',
|
|
27
32
|
'MapleDataNotFoundException',
|
|
33
|
+
'MapleEncryptionNotEnabledException',
|
|
28
34
|
'MapleException',
|
|
29
35
|
'MapleFileEmptyException',
|
|
30
36
|
'MapleFileLockedException',
|
|
31
37
|
'MapleFileNotFoundException',
|
|
32
38
|
'MapleHeaderNotFoundException',
|
|
39
|
+
'MapleJson',
|
|
40
|
+
'MapleSyntaxException',
|
|
33
41
|
'MapleTagNotFoundException',
|
|
42
|
+
'MapleTypeException',
|
|
34
43
|
'NotAMapleFileException',
|
|
35
44
|
'MapleTree',
|
|
36
45
|
'Logger',
|
|
@@ -38,6 +47,6 @@ __all__ = [
|
|
|
38
47
|
'winUnHide'
|
|
39
48
|
]
|
|
40
49
|
|
|
41
|
-
__version__ = "
|
|
50
|
+
__version__ = "3.0.0.dev2"
|
|
42
51
|
__author__ = "Ryuji Hazama"
|
|
43
52
|
__license__ = "MIT"
|
maplex/mapleColors.py
CHANGED
maplex/mapleExceptions.py
CHANGED
|
@@ -54,6 +54,13 @@ class MapleDataNotFoundException(MapleException):
|
|
|
54
54
|
|
|
55
55
|
super().__init__(self.message)
|
|
56
56
|
|
|
57
|
+
class MapleEncryptionNotEnabledException(MapleException):
|
|
58
|
+
|
|
59
|
+
def __init__(self, mapleFile: str = "", message: str = "File encryption is not enabled"):
|
|
60
|
+
|
|
61
|
+
self.message = f"{message}: {mapleFile}"
|
|
62
|
+
super().__init__(self.message)
|
|
63
|
+
|
|
57
64
|
class MapleHeaderNotFoundException(MapleDataNotFoundException):
|
|
58
65
|
|
|
59
66
|
def __init__(self, fileName = "", header: str = "", preHeader: str = "", message = ""):
|
|
@@ -137,3 +144,31 @@ class MapleTypeException(MapleSyntaxException):
|
|
|
137
144
|
self.message = message
|
|
138
145
|
|
|
139
146
|
super().__init__(self.message)
|
|
147
|
+
|
|
148
|
+
class MapleValueException(MapleException):
|
|
149
|
+
|
|
150
|
+
def __init__(self, message: str = "Maple value error"):
|
|
151
|
+
|
|
152
|
+
self.message = message
|
|
153
|
+
super().__init__(self.message)
|
|
154
|
+
|
|
155
|
+
class MapleLoggerException(MapleException):
|
|
156
|
+
|
|
157
|
+
def __init__(self, message: str = "Maple logger error"):
|
|
158
|
+
|
|
159
|
+
self.message = message
|
|
160
|
+
super().__init__(self.message)
|
|
161
|
+
|
|
162
|
+
class MapleInvalidLoggerLevelException(MapleLoggerException):
|
|
163
|
+
|
|
164
|
+
def __init__(self, loggerLevel: str = "", message: str = ""):
|
|
165
|
+
|
|
166
|
+
if message == "":
|
|
167
|
+
|
|
168
|
+
self.message = f"Invalid logger level [{loggerLevel}]"
|
|
169
|
+
|
|
170
|
+
else:
|
|
171
|
+
|
|
172
|
+
self.message = f"{message}: Caused by invalid logger level [{loggerLevel}]"
|
|
173
|
+
|
|
174
|
+
super().__init__(self.message)
|
maplex/mapleJson.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import base64
|
|
4
|
+
from cryptography.fernet import Fernet
|
|
5
|
+
from . import mapleExceptions as mExc
|
|
6
|
+
|
|
7
|
+
class MapleJson:
|
|
8
|
+
|
|
9
|
+
def __init__(self, filePath: str, fileEncoding: str = 'utf-8', indent: int = 4, ensure_ascii: bool = True, encrypt: bool = False, key: bytes = None):
|
|
10
|
+
|
|
11
|
+
self.filePath = filePath
|
|
12
|
+
self.fileEncoding = fileEncoding
|
|
13
|
+
self.indent = indent
|
|
14
|
+
self.ensure_ascii = ensure_ascii
|
|
15
|
+
self.encrypt = encrypt
|
|
16
|
+
self.key = key
|
|
17
|
+
self.fernet = Fernet(key) if encrypt and key else None
|
|
18
|
+
|
|
19
|
+
#
|
|
20
|
+
#####################
|
|
21
|
+
# Getter / Setter
|
|
22
|
+
|
|
23
|
+
def getFilePath(self) -> str:
|
|
24
|
+
|
|
25
|
+
return self.filePath
|
|
26
|
+
|
|
27
|
+
def setFilePath(self, filePath: str) -> None:
|
|
28
|
+
|
|
29
|
+
self.filePath = filePath
|
|
30
|
+
|
|
31
|
+
def getFileEncoding(self) -> str:
|
|
32
|
+
|
|
33
|
+
return self.fileEncoding
|
|
34
|
+
|
|
35
|
+
def setFileEncoding(self, fileEncoding: str) -> None:
|
|
36
|
+
|
|
37
|
+
self.fileEncoding = fileEncoding
|
|
38
|
+
|
|
39
|
+
def getIndent(self) -> int:
|
|
40
|
+
|
|
41
|
+
return self.indent
|
|
42
|
+
|
|
43
|
+
def setIndent(self, indent: int) -> None:
|
|
44
|
+
|
|
45
|
+
self.indent = indent
|
|
46
|
+
|
|
47
|
+
def getEnsureAscii(self) -> bool:
|
|
48
|
+
|
|
49
|
+
return self.ensure_ascii
|
|
50
|
+
|
|
51
|
+
def setEnsureAscii(self, ensure_ascii: bool) -> None:
|
|
52
|
+
|
|
53
|
+
self.ensure_ascii = ensure_ascii
|
|
54
|
+
|
|
55
|
+
def isEncrypted(self) -> bool:
|
|
56
|
+
|
|
57
|
+
return self.encrypt
|
|
58
|
+
|
|
59
|
+
def setEncryption(self, encrypt: bool, key=None) -> None:
|
|
60
|
+
|
|
61
|
+
self.encrypt = encrypt
|
|
62
|
+
|
|
63
|
+
if encrypt and not key:
|
|
64
|
+
|
|
65
|
+
raise mExc.KeyEmptyException(self.filePath)
|
|
66
|
+
|
|
67
|
+
self.key = key
|
|
68
|
+
self.fernet = Fernet(key) if encrypt and key else None
|
|
69
|
+
|
|
70
|
+
def getKey(self) -> bytes:
|
|
71
|
+
|
|
72
|
+
return self.key
|
|
73
|
+
|
|
74
|
+
def setKey(self, key: bytes) -> None:
|
|
75
|
+
|
|
76
|
+
self.key = key
|
|
77
|
+
self.fernet = Fernet(key) if self.encrypt and key else None
|
|
78
|
+
|
|
79
|
+
#
|
|
80
|
+
#####################
|
|
81
|
+
# Basic File Operations
|
|
82
|
+
|
|
83
|
+
def read(self) -> dict:
|
|
84
|
+
|
|
85
|
+
try:
|
|
86
|
+
|
|
87
|
+
with open(self.filePath, 'rb') as file:
|
|
88
|
+
|
|
89
|
+
data = file.read()
|
|
90
|
+
|
|
91
|
+
if self.encrypt and self.fernet:
|
|
92
|
+
|
|
93
|
+
decryptedData = self.fernet.decrypt(data)
|
|
94
|
+
return json.loads(decryptedData.decode(self.fileEncoding))
|
|
95
|
+
|
|
96
|
+
else:
|
|
97
|
+
|
|
98
|
+
return json.loads(data.decode(self.fileEncoding))
|
|
99
|
+
|
|
100
|
+
except FileNotFoundError:
|
|
101
|
+
|
|
102
|
+
raise mExc.MapleFileNotFoundException(self.filePath)
|
|
103
|
+
|
|
104
|
+
except Exception as e:
|
|
105
|
+
|
|
106
|
+
raise mExc.MapleException(f"Error reading JSON file: {e}")
|
|
107
|
+
|
|
108
|
+
def write(self, data: dict) -> None:
|
|
109
|
+
|
|
110
|
+
try:
|
|
111
|
+
|
|
112
|
+
if type(data) is not dict:
|
|
113
|
+
|
|
114
|
+
raise mExc.MapleTypeException(self.filePath, "Data to write must be a dictionary")
|
|
115
|
+
|
|
116
|
+
jsonData = json.dumps(data, indent=self.indent, ensure_ascii=self.ensure_ascii).encode(self.fileEncoding)
|
|
117
|
+
|
|
118
|
+
if self.encrypt and self.fernet:
|
|
119
|
+
|
|
120
|
+
encryptedData = self.fernet.encrypt(jsonData)
|
|
121
|
+
|
|
122
|
+
with open(self.filePath, 'wb') as file:
|
|
123
|
+
|
|
124
|
+
file.write(encryptedData)
|
|
125
|
+
|
|
126
|
+
else:
|
|
127
|
+
|
|
128
|
+
with open(self.filePath, 'wb') as file:
|
|
129
|
+
|
|
130
|
+
file.write(jsonData)
|
|
131
|
+
|
|
132
|
+
except Exception as e:
|
|
133
|
+
|
|
134
|
+
raise mExc.MapleException(f"Error writing JSON file: {e}")
|
|
135
|
+
|
|
136
|
+
#
|
|
137
|
+
#####################
|
|
138
|
+
# Utility Methods
|
|
139
|
+
|
|
140
|
+
#
|
|
141
|
+
#####################
|
|
142
|
+
# Generate Encryption Key
|
|
143
|
+
|
|
144
|
+
def generateKey(self, setAsCurrent: bool = False) -> bytes:
|
|
145
|
+
|
|
146
|
+
"""
|
|
147
|
+
Generates a new Fernet encryption key.
|
|
148
|
+
Args:
|
|
149
|
+
setAsCurrent (bool): If True, sets the generated key as the current key for the instance.
|
|
150
|
+
Returns:
|
|
151
|
+
bytes: The generated encryption key.
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
key = Fernet.generate_key()
|
|
155
|
+
|
|
156
|
+
if setAsCurrent:
|
|
157
|
+
|
|
158
|
+
self.key = key
|
|
159
|
+
self.fernet = Fernet(key)
|
|
160
|
+
self.encrypt = True
|
|
161
|
+
|
|
162
|
+
return key
|