reydb 1.1.35__py3-none-any.whl → 1.1.36__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.
reydb/rconfig.py
CHANGED
@@ -9,8 +9,15 @@
|
|
9
9
|
"""
|
10
10
|
|
11
11
|
|
12
|
-
from typing import Any
|
13
|
-
|
12
|
+
from typing import Any, TypedDict, TypeVar
|
13
|
+
import datetime
|
14
|
+
from datetime import (
|
15
|
+
datetime as Datetime,
|
16
|
+
date as Date,
|
17
|
+
time as Time,
|
18
|
+
timedelta as Timedelta
|
19
|
+
)
|
20
|
+
from reykit.rbase import throw
|
14
21
|
|
15
22
|
from .rconn import DatabaseConnection
|
16
23
|
from .rdb import Database
|
@@ -21,10 +28,24 @@ __all__ = (
|
|
21
28
|
)
|
22
29
|
|
23
30
|
|
31
|
+
type ConfigValue = bool | str | int | float | list | tuple | dict | set | Datetime | Date | Time | Timedelta | None
|
32
|
+
ConfigRow = TypedDict('ConfigRow', {'key': str, 'value': ConfigValue, 'type': str, 'note': str | None})
|
33
|
+
type ConfigTable = list[ConfigRow]
|
34
|
+
ConfigValueT = TypeVar('T', bound=ConfigValue) # Any.
|
35
|
+
|
36
|
+
|
24
37
|
class DatabaseConfig(object):
|
25
38
|
"""
|
26
39
|
Database config type.
|
27
40
|
Can create database used `self.build` method.
|
41
|
+
|
42
|
+
Examples
|
43
|
+
--------
|
44
|
+
>>> config = DatabaseConfig()
|
45
|
+
>>> config['key1'] = 1
|
46
|
+
>>> config['key2', 'note'] = 2
|
47
|
+
>>> config['key1'], config['key2']
|
48
|
+
(1, 2)
|
28
49
|
"""
|
29
50
|
|
30
51
|
|
@@ -90,16 +111,24 @@ class DatabaseConfig(object):
|
|
90
111
|
},
|
91
112
|
{
|
92
113
|
'name': 'value',
|
93
|
-
'type': '
|
114
|
+
'type': 'text',
|
94
115
|
'constraint': 'NOT NULL',
|
95
116
|
'comment': 'Config value.'
|
96
117
|
},
|
118
|
+
{
|
119
|
+
'name': 'type',
|
120
|
+
'type': 'varchar(50)',
|
121
|
+
'constraint': 'NOT NULL',
|
122
|
+
'comment': 'Config value type.'
|
123
|
+
},
|
97
124
|
{
|
98
125
|
'name': 'note',
|
99
126
|
'type': 'varchar(500)',
|
100
127
|
'comment': 'Config note.'
|
101
128
|
}
|
102
|
-
]
|
129
|
+
],
|
130
|
+
'primary': 'key',
|
131
|
+
'comment': 'Config data table.'
|
103
132
|
}
|
104
133
|
|
105
134
|
]
|
@@ -145,7 +174,38 @@ class DatabaseConfig(object):
|
|
145
174
|
self.database.build.build(databases, tables, views_stats=views_stats)
|
146
175
|
|
147
176
|
|
148
|
-
|
177
|
+
@property
|
178
|
+
def data(self) -> ConfigTable:
|
179
|
+
"""
|
180
|
+
Get config data table.
|
181
|
+
|
182
|
+
Returns
|
183
|
+
-------
|
184
|
+
Config data table.
|
185
|
+
"""
|
186
|
+
|
187
|
+
# Get.
|
188
|
+
result = self.database.execute_select(
|
189
|
+
(self.db_names['base'], self.db_names['base.config']),
|
190
|
+
['key', 'value', 'type', 'note'],
|
191
|
+
order='IFNULL(`update_time`, `create_time`) DESC'
|
192
|
+
)
|
193
|
+
|
194
|
+
# Convert.
|
195
|
+
global_dict = {'datetime': datetime}
|
196
|
+
result = [
|
197
|
+
{
|
198
|
+
'key': row['key'],
|
199
|
+
'value': eval(row['value'], global_dict),
|
200
|
+
'note': row['note']
|
201
|
+
}
|
202
|
+
for row in result
|
203
|
+
]
|
204
|
+
|
205
|
+
return result
|
206
|
+
|
207
|
+
|
208
|
+
def get(self, key: str, default: ConfigValueT | None = None) -> ConfigValue | ConfigValueT:
|
149
209
|
"""
|
150
210
|
Get config value, when not exist, then return default value.
|
151
211
|
|
@@ -163,7 +223,7 @@ class DatabaseConfig(object):
|
|
163
223
|
where = '`key` = :key'
|
164
224
|
result = self.database.execute_select(
|
165
225
|
(self.db_names['base'], self.db_names['base.config']),
|
166
|
-
'value',
|
226
|
+
'`value`',
|
167
227
|
where,
|
168
228
|
limit=1,
|
169
229
|
key=key
|
@@ -173,11 +233,19 @@ class DatabaseConfig(object):
|
|
173
233
|
# Default.
|
174
234
|
if value is None:
|
175
235
|
value = default
|
236
|
+
else:
|
237
|
+
global_dict = {'datetime': datetime}
|
238
|
+
value = eval(value, global_dict)
|
176
239
|
|
177
240
|
return value
|
178
241
|
|
179
242
|
|
180
|
-
def setdefault(
|
243
|
+
def setdefault(
|
244
|
+
self,
|
245
|
+
key: str,
|
246
|
+
default: ConfigValueT | None = None,
|
247
|
+
default_note: str | None = None
|
248
|
+
) -> ConfigValue | ConfigValueT:
|
181
249
|
"""
|
182
250
|
Set config value.
|
183
251
|
|
@@ -185,6 +253,7 @@ class DatabaseConfig(object):
|
|
185
253
|
----------
|
186
254
|
key : Config key.
|
187
255
|
default : Config default value.
|
256
|
+
default_note : Config default note.
|
188
257
|
|
189
258
|
Returns
|
190
259
|
-------
|
@@ -192,7 +261,12 @@ class DatabaseConfig(object):
|
|
192
261
|
"""
|
193
262
|
|
194
263
|
# Set.
|
195
|
-
data = {
|
264
|
+
data = {
|
265
|
+
'key': key,
|
266
|
+
'value': repr(default),
|
267
|
+
'type': type(default).__name__,
|
268
|
+
'note': default_note
|
269
|
+
}
|
196
270
|
result = self.database.execute_insert(
|
197
271
|
(self.db_names['base'], self.db_names['base.config']),
|
198
272
|
data,
|
@@ -201,30 +275,39 @@ class DatabaseConfig(object):
|
|
201
275
|
|
202
276
|
# Get.
|
203
277
|
if result.rowcount == 0:
|
204
|
-
|
205
|
-
else:
|
206
|
-
result = default
|
278
|
+
default = self.get(key)
|
207
279
|
|
208
|
-
return
|
280
|
+
return default
|
209
281
|
|
210
282
|
|
211
|
-
def update(self, data: dict[str,
|
283
|
+
def update(self, data: dict[str, ConfigValue] | ConfigTable) -> None:
|
212
284
|
"""
|
213
285
|
Update config values.
|
214
286
|
|
215
287
|
Parameters
|
216
288
|
----------
|
217
289
|
data : Config update data.
|
290
|
+
- `dict[str, Any]`: Config key and value.
|
291
|
+
- `ConfigTable`: Config key and value and note.
|
218
292
|
"""
|
219
293
|
|
294
|
+
# Handle parameter.
|
295
|
+
if type(data) == dict:
|
296
|
+
data = [
|
297
|
+
{
|
298
|
+
'key': key,
|
299
|
+
'value': repr(value),
|
300
|
+
'type': type(value).__name__
|
301
|
+
}
|
302
|
+
for key, value in data.items()
|
303
|
+
]
|
304
|
+
else:
|
305
|
+
data = data.copy()
|
306
|
+
for row in data:
|
307
|
+
row['value'] = repr(row['value'])
|
308
|
+
row['type'] = type(row['value']).__name__
|
309
|
+
|
220
310
|
# Update.
|
221
|
-
data = [
|
222
|
-
{
|
223
|
-
'key': key,
|
224
|
-
'value': value
|
225
|
-
}
|
226
|
-
for key, value in data.items
|
227
|
-
]
|
228
311
|
self.database.execute_insert(
|
229
312
|
(self.db_names['base'], self.db_names['base.config']),
|
230
313
|
data,
|
@@ -232,21 +315,26 @@ class DatabaseConfig(object):
|
|
232
315
|
)
|
233
316
|
|
234
317
|
|
235
|
-
def remove(self, key: str) -> None:
|
318
|
+
def remove(self, key: str | list[str]) -> None:
|
236
319
|
"""
|
237
320
|
Remove config.
|
238
321
|
|
239
322
|
Parameters
|
240
323
|
----------
|
241
|
-
key : Config key.
|
324
|
+
key : Config key or key list.
|
242
325
|
"""
|
243
326
|
|
244
327
|
# Remove.
|
245
|
-
|
328
|
+
if type(key) == str:
|
329
|
+
where = '`key` = :key'
|
330
|
+
limit = 1
|
331
|
+
else:
|
332
|
+
where = '`key` in :key'
|
333
|
+
limit = None
|
246
334
|
result = self.database.execute_delete(
|
247
335
|
(self.db_names['base'], self.db_names['base.config']),
|
248
336
|
where,
|
249
|
-
limit=
|
337
|
+
limit=limit,
|
250
338
|
key=key
|
251
339
|
)
|
252
340
|
|
@@ -255,7 +343,7 @@ class DatabaseConfig(object):
|
|
255
343
|
throw(KeyError, key)
|
256
344
|
|
257
345
|
|
258
|
-
def items(self) -> dict:
|
346
|
+
def items(self) -> dict[str, ConfigValue]:
|
259
347
|
"""
|
260
348
|
Get all config keys and values.
|
261
349
|
|
@@ -271,7 +359,12 @@ class DatabaseConfig(object):
|
|
271
359
|
)
|
272
360
|
|
273
361
|
# Convert.
|
362
|
+
global_dict = {'datetime': datetime}
|
274
363
|
result = result.to_dict('key', 'value')
|
364
|
+
result = {
|
365
|
+
key: eval(value, global_dict)
|
366
|
+
for key, value in result.items()
|
367
|
+
}
|
275
368
|
|
276
369
|
return result
|
277
370
|
|
@@ -288,16 +381,20 @@ class DatabaseConfig(object):
|
|
288
381
|
# Get.
|
289
382
|
result = self.database.execute_select(
|
290
383
|
(self.db_names['base'], self.db_names['base.config']),
|
291
|
-
'key'
|
384
|
+
'`key`'
|
292
385
|
)
|
293
386
|
|
294
387
|
# Convert.
|
295
|
-
|
388
|
+
global_dict = {'datetime': datetime}
|
389
|
+
result = [
|
390
|
+
eval(value, global_dict)
|
391
|
+
for value in result
|
392
|
+
]
|
296
393
|
|
297
394
|
return result
|
298
395
|
|
299
396
|
|
300
|
-
def values(self) -> list[
|
397
|
+
def values(self) -> list[ConfigValue]:
|
301
398
|
"""
|
302
399
|
Get all config value.
|
303
400
|
|
@@ -309,16 +406,20 @@ class DatabaseConfig(object):
|
|
309
406
|
# Get.
|
310
407
|
result = self.database.execute_select(
|
311
408
|
(self.db_names['base'], self.db_names['base.config']),
|
312
|
-
'value'
|
409
|
+
'`value`'
|
313
410
|
)
|
314
411
|
|
315
412
|
# Convert.
|
316
|
-
|
413
|
+
global_dict = {'datetime': datetime}
|
414
|
+
result = [
|
415
|
+
eval(value, global_dict)
|
416
|
+
for value in result
|
417
|
+
]
|
317
418
|
|
318
419
|
return result
|
319
420
|
|
320
421
|
|
321
|
-
def __getitem__(self, key: str) ->
|
422
|
+
def __getitem__(self, key: str) -> ConfigValue:
|
322
423
|
"""
|
323
424
|
Get config value.
|
324
425
|
|
@@ -341,18 +442,30 @@ class DatabaseConfig(object):
|
|
341
442
|
return value
|
342
443
|
|
343
444
|
|
344
|
-
def __setitem__(self,
|
445
|
+
def __setitem__(self, key_note: str | tuple[str, str], value: ConfigValue) -> None:
|
345
446
|
"""
|
346
447
|
Set config value.
|
347
448
|
|
348
449
|
Parameters
|
349
450
|
----------
|
350
|
-
|
451
|
+
key_note : Config key and note.
|
351
452
|
value : Config value.
|
352
453
|
"""
|
353
454
|
|
455
|
+
# Handle parameter.
|
456
|
+
if type(key_note) != str:
|
457
|
+
key, note = key_note
|
458
|
+
else:
|
459
|
+
key = key_note
|
460
|
+
note = None
|
461
|
+
|
354
462
|
# Set.
|
355
|
-
data = {
|
463
|
+
data = {
|
464
|
+
'key': key,
|
465
|
+
'value': repr(value),
|
466
|
+
'type': type(value).__name__,
|
467
|
+
'note': note
|
468
|
+
}
|
356
469
|
self.database.execute_insert(
|
357
470
|
(self.db_names['base'], self.db_names['base.config']),
|
358
471
|
data,
|
@@ -2,7 +2,7 @@ reydb/__init__.py,sha256=SqjJEBMiUMnKkNfmOvw_jprZcj9Edi0jyBKt67xeYUE,544
|
|
2
2
|
reydb/rall.py,sha256=UWnbtl4oG4YqXyqTMN_5uqE-QqD5nb_-dvarotlTUeU,388
|
3
3
|
reydb/rbase.py,sha256=X8bDBmKyIiLXGvOVnTh5nD0ltkhOPWU6g4BebifOZYY,312
|
4
4
|
reydb/rbuild.py,sha256=s4lLVQONfL5a9B6mw5D7bfbPyJoYNIqBxlas9rCM1Y8,32458
|
5
|
-
reydb/rconfig.py,sha256=
|
5
|
+
reydb/rconfig.py,sha256=x9NXZ_Z_LjEZH7xHm7mUFv_1Y93Ky50Vx72FRAXu08w,12303
|
6
6
|
reydb/rconn.py,sha256=kdw2xQb0JqSUXM-BD7XfJ-ZqaWHxYkeT8psRhOK43mA,6211
|
7
7
|
reydb/rdb.py,sha256=OQ3ztC4G53usAbocqGwTqo1ry1AIGjoWTN7pn8Pn3Uk,62184
|
8
8
|
reydb/rerror.py,sha256=xHLvRxu8KBvUdfVS7mD906jx9XTQPGwEl7psBLGEZ5w,9966
|
@@ -10,7 +10,7 @@ reydb/rexec.py,sha256=dGdRkG1XR0Z66T0r4nPCSdQzSRWc_Q3t6TPSSrDTIxY,9042
|
|
10
10
|
reydb/rfile.py,sha256=7g6hPBz33p-mkGFc6LEmL2hpFes-LM-AWQ0SxgJe2BI,15254
|
11
11
|
reydb/rinfo.py,sha256=KXTkcpTGAD3p9RVKKcnmc_FjJtiKRPk-K5ZepPOnphQ,15253
|
12
12
|
reydb/rparam.py,sha256=3BGDBD8QshOf2J70ZJ6LJ9PiH-1ZU3ruZwoE0bN6OOw,7017
|
13
|
-
reydb-1.1.
|
14
|
-
reydb-1.1.
|
15
|
-
reydb-1.1.
|
16
|
-
reydb-1.1.
|
13
|
+
reydb-1.1.36.dist-info/METADATA,sha256=At9eApGRgD86nYSVG-ZWBL8ndc55uPRwc4K0Ex5c7gs,1550
|
14
|
+
reydb-1.1.36.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
15
|
+
reydb-1.1.36.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
|
16
|
+
reydb-1.1.36.dist-info/RECORD,,
|
File without changes
|
File without changes
|