-
Notifications
You must be signed in to change notification settings - Fork 1
/
cptoml.py
337 lines (305 loc) · 9.63 KB
/
cptoml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
def _prepareline(line) -> str:
"""
Remove comments from a line buffer.
Also removes misc chars.
"""
if (
(line.rfind("#") != -1)
and (
line.find("'") != -1
and line.find("'") != line.rfind("'")
and (line.find("'") > line.rfind("#") or line.rfind("'") < line.rfind("#"))
)
and (
line.find('"') != -1
and line.find('"') != line.rfind('"')
and (line.find('"') > line.rfind("#") or line.rfind('"') < line.rfind("#"))
)
): # Without the -1 check eats a char
line = line[: line.rfind("#")]
while line.endswith(" ") or line.endswith("\n"):
line = line[:-1]
while line.startswith(" "):
line = line[1:]
return line
def _dataformat(data):
"""
Prepares the data into a list.
"""
data = data.split("\n")
for i in range(len(data) - 1, 0, -1): # Go in reverse as to not break the index
if data[i].isspace() or not data[i]: # "" == False
data.pop(i)
return data
def _linevalue(line):
"""
Get the value out of a line.
"""
result = _prepareline(line)
result = result[result.find("=") + 1 :]
result = _prepareline(result) # for spaces
if not len(result):
return None
elif (result.startswith('"') and result.endswith('"')) or (
result.startswith("'") and result.endswith("'")
): # chars
result = result[1:-1]
elif result.isdigit() or (result[0] in ["-", "+"] and result[1:].isdigit()): # ints
result = int(result)
elif result[0] == "0" and result[1].isalpha(): # other numeric
if result[1] == "x": # hex
result = hex(int(result[2:], 16))
elif result[1] == "o": # octal
result = oct(int(result[2:], 8))
elif result[1] == "b": # binary
result = int(result, 2)
elif result[0].isdigit() and ("e" in result): # notation
exec(f"result = int({result})")
else:
del line, result
raise TypeError("Invalid value.")
elif result == "true": # bools
result = True
elif result == "false":
result = False
else:
del line, result
raise TypeError("Invalid value.")
del line
return result
def _linefind(buf, key, start=0) -> int:
"""
Find key buffer index.
Use start to place into the correct table.
Returns -1 when not found.
"""
result = -1
tm = start
q = True
while q:
try:
tml = _prepareline(buf[tm])
except IndexError:
q = False
if q and (tml.startswith(key + "=") or tml.startswith(key + " =")):
result = tm
q = False
elif q and ((len(buf) == tm + 1) or tml.startswith("[")):
q = False
del tml
tm += 1
if not q:
break
del buf, start, key, tm, q
return result
def _linemake(key, value, comment=None) -> str:
"""
Creates a new toml line with key and value.
Accepts comment.
"""
result = key + " = "
if isinstance(value, str):
result += '"' + value.replace("\n", "\\n") + '"' # make raw
elif isinstance(value, int) or isinstance(value, float):
if str(value) != "inf":
result += str(value)
else:
del result, key, value, comment
raise TypeError("Value infinite")
elif isinstance(value, bool):
result += str(value).lower()
else:
del result, key, value, comment
raise TypeError("Unsupported type for toml")
if comment is not None:
result += " # " + comment
del key, value, comment
return result
def _applyformatting(data) -> list:
"""
Apply formatting.
- Spaces it out subtables.
- Removes blank tables.
"""
bb = False # Back to back subtable decleration
el = False # no element in subtable
for i in range(len(data) - 1, 0, -1):
if data[i].startswith("["):
if (bb is False) and el:
bb = True
el = False
data.insert(i, "")
else: # The current subtable should be removed!
data.pop(i)
# Should remain true.
else:
if bb:
bb = False
if not el:
el = True
del bb, el
return data
def _tablefind(buf, subtable) -> int:
"""
Find subtable buffer index.
Returns -1 when not found.
"""
result = -1
tm = 0
q = True
while q:
tml = None
try:
tml = _prepareline(buf[tm])
except IndexError:
q = False
if q and tml.startswith("[") and (tml == f"[{subtable}]"):
result = tm
q = False
del tml
tm += 1
if not q:
break
del buf, subtable, tm, q
return result
def _getkeys(buf, start=0):
"""
Get the keys off a table.
"""
res = []
q = True
tm = start
while q:
tml = None
try:
tml = _prepareline(buf[tm])
except IndexError:
q = False
if q and "=" in tml and not tml.startswith("["):
tml = tml[: tml.find("=")]
while tml.endswith(" "):
tml = tml[:-1]
res.append(tml)
elif q and tml.startswith("["):
q = False
del tml
tm += 1
del buf, start, tm, q
return res
def keys(subtable=None, toml="/settings.toml"):
try:
with open(toml) as tomlf:
data = _dataformat(tomlf.read()) # load into list
result = []
if subtable is None: # Browse root table
result += _getkeys(data) # fetch keys
else:
start = _tablefind(data, subtable) # find table offset
if start != -1: # table found
result += _getkeys(data, start + 1) # fetch keys
del start
del data, subtable, toml
result2 = []
for i in result:
if i[0] != "#":
result2.append(i)
del result
return result2
except OSError:
del item, subtable, toml
raise OSError("Toml file not found")
def fetch(item, subtable=None, toml="/settings.toml"):
if not isinstance(item, str):
del item, subtable, toml
raise TypeError("Item should be str.")
if subtable is not None and not isinstance(subtable, str):
del item, subtable, toml
raise TypeError("Subtable should be str.")
try:
with open(toml) as tomlf:
data = _dataformat(tomlf.read()) # load into list
result = None
if subtable is None: # Browse root table
target = _linefind(data, item)
if target != -1:
result = _linevalue(data[target])
else:
start = _tablefind(data, subtable) # find table offset
if start != -1:
start += 1
if start != -1: # table found
tr = _linefind(data, item, start) # fetch item index
if tr != -1:
result = _linevalue(data[tr]) # load value
del tr
del start
del data, subtable, toml, item
return result
except OSError:
del item, subtable, toml
raise OSError("Toml file not found")
def put(item, value, subtable=None, toml="/settings.toml", comment=None) -> None:
"""
Store / Update a value. You can also place a comment.
The existing comment will be removed.
"""
data = None
ro = False
try:
with open(toml) as tomlr:
data = _dataformat(tomlr.read())
except OSError:
raise OSError("Toml file not found")
if data is not None:
# Find target line
start = 0
if subtable is not None:
start = _tablefind(data, subtable) # find table offset
if start == -1: # Need to create new subtable
data.append(f"[{subtable}]")
data.append(_linemake(item, value, comment))
else: # Whatever start says
if start:
start += 1
tr = _linefind(data, item, start) # fetch item index
if tr == -1: # New key
data.insert(start, _linemake(item, value, comment))
else: # Existing key
data[tr] = _linemake(item, value, comment)
del start
# Reapply formatting
data = _applyformatting(data)
# Write to file
with open(toml, "w") as tomlw:
for line in data:
tomlw.write(f"{line}\n")
del line
del item, value, subtable, toml, comment, data, ro
def delete(item, subtable=None, toml="/settings.toml") -> None:
"""
Delete an entry on the toml file.
"""
data = None
try:
with open(toml) as tomlr:
data = _dataformat(tomlr.read())
except OSError:
raise OSError("Toml file not found")
if data is not None:
# Find target line
start = 0
if subtable is not None:
start = _tablefind(data, subtable) # find table offset
if start != -1:
tr = _linefind(data, item, start + 1) # fetch item index
if tr != -1:
data.pop(tr)
del start
# Reapply formatting
data = _applyformatting(data)
# Write to file
with open(toml, "w") as tomlw:
for line in data:
tomlw.write(f"{line}\n")
del line
del item, subtable, toml, data