reykit 1.1.1__py3-none-any.whl → 1.1.2__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.
- reykit/__init__.py +0 -6
- reykit-1.1.2.dist-info/METADATA +44 -0
- {reykit-1.1.1.dist-info → reykit-1.1.2.dist-info}/RECORD +5 -13
- {reykit-1.1.1.dist-info → reykit-1.1.2.dist-info}/WHEEL +1 -2
- reykit-1.1.2.dist-info/licenses/LICENSE +7 -0
- reydb/__init__.py +0 -25
- reydb/rall.py +0 -17
- reydb/rbuild.py +0 -1235
- reydb/rconnection.py +0 -2276
- reydb/rexecute.py +0 -350
- reydb/rfile.py +0 -416
- reydb/rinformation.py +0 -503
- reydb/rparameter.py +0 -243
- reykit-1.1.1.dist-info/METADATA +0 -29
- reykit-1.1.1.dist-info/top_level.txt +0 -1
reydb/rfile.py
DELETED
@@ -1,416 +0,0 @@
|
|
1
|
-
# !/usr/bin/env python
|
2
|
-
# -*- coding: utf-8 -*-
|
3
|
-
|
4
|
-
"""
|
5
|
-
@Time : 2023-10-29 20:01:25
|
6
|
-
@Author : Rey
|
7
|
-
@Contact : reyxbo@163.com
|
8
|
-
@Explain : Database file methods.
|
9
|
-
"""
|
10
|
-
|
11
|
-
|
12
|
-
from typing import TypedDict, overload
|
13
|
-
from os.path import join as os_join
|
14
|
-
from datetime import datetime
|
15
|
-
from reykit.ros import RFile, RFolder, get_md5
|
16
|
-
|
17
|
-
from .rconnection import RDatabase, RDBConnection
|
18
|
-
|
19
|
-
|
20
|
-
__all__ = (
|
21
|
-
'RDBFile',
|
22
|
-
)
|
23
|
-
|
24
|
-
|
25
|
-
FileInfo = TypedDict('FileInfo', {'create_time': datetime, 'md5': str, 'name': str | None, 'size': int, 'note': str | None})
|
26
|
-
|
27
|
-
|
28
|
-
class RDBFile(object):
|
29
|
-
"""
|
30
|
-
Rey's `database file` type.
|
31
|
-
"""
|
32
|
-
|
33
|
-
|
34
|
-
def __init__(
|
35
|
-
self,
|
36
|
-
rdatabase: RDatabase | RDBConnection
|
37
|
-
) -> None:
|
38
|
-
"""
|
39
|
-
Build `database file` attributes.
|
40
|
-
|
41
|
-
Parameters
|
42
|
-
----------
|
43
|
-
rdatabase : RDatabase or RDBConnection instance.
|
44
|
-
"""
|
45
|
-
|
46
|
-
# Set attribute.
|
47
|
-
self.rdatabase = rdatabase
|
48
|
-
|
49
|
-
|
50
|
-
def build(self) -> None:
|
51
|
-
"""
|
52
|
-
Check and build all standard databases and tables.
|
53
|
-
"""
|
54
|
-
|
55
|
-
# Set parameter.
|
56
|
-
|
57
|
-
## Database.
|
58
|
-
databases = [
|
59
|
-
{
|
60
|
-
'database': 'file'
|
61
|
-
}
|
62
|
-
]
|
63
|
-
|
64
|
-
## Table.
|
65
|
-
tables = [
|
66
|
-
|
67
|
-
### 'information'.
|
68
|
-
{
|
69
|
-
'path': ('file', 'information'),
|
70
|
-
'fields': [
|
71
|
-
{
|
72
|
-
'name': 'create_time',
|
73
|
-
'type': 'datetime',
|
74
|
-
'constraint': 'NOT NULL DEFAULT CURRENT_TIMESTAMP',
|
75
|
-
'comment': 'Record create time.'
|
76
|
-
},
|
77
|
-
{
|
78
|
-
'name': 'file_id',
|
79
|
-
'type': 'mediumint unsigned',
|
80
|
-
'constraint': 'NOT NULL AUTO_INCREMENT',
|
81
|
-
'comment': 'File self increase ID.'
|
82
|
-
},
|
83
|
-
{
|
84
|
-
'name': 'md5',
|
85
|
-
'type': 'char(32)',
|
86
|
-
'constraint': 'NOT NULL',
|
87
|
-
'comment': 'File MD5.'
|
88
|
-
},
|
89
|
-
{
|
90
|
-
'name': 'name',
|
91
|
-
'type': 'varchar(260)',
|
92
|
-
'constraint': 'DEFAULT NULL',
|
93
|
-
'comment': 'File name.'
|
94
|
-
},
|
95
|
-
{
|
96
|
-
'name': 'note',
|
97
|
-
'type': 'varchar(500)',
|
98
|
-
'constraint': 'DEFAULT NULL',
|
99
|
-
'comment': 'File note.'
|
100
|
-
}
|
101
|
-
],
|
102
|
-
'primary': 'file_id',
|
103
|
-
'indexes': [
|
104
|
-
{
|
105
|
-
'name': 'n_md5',
|
106
|
-
'fields': 'md5',
|
107
|
-
'type': 'noraml',
|
108
|
-
'comment': 'File MD5 normal index.'
|
109
|
-
},
|
110
|
-
{
|
111
|
-
'name': 'n_name',
|
112
|
-
'fields': 'name',
|
113
|
-
'type': 'noraml',
|
114
|
-
'comment': 'File name normal index.'
|
115
|
-
}
|
116
|
-
],
|
117
|
-
'comment': 'File information table.'
|
118
|
-
},
|
119
|
-
|
120
|
-
### 'data'.
|
121
|
-
{
|
122
|
-
'path': ('file', 'data'),
|
123
|
-
'fields': [
|
124
|
-
{
|
125
|
-
'name': 'md5',
|
126
|
-
'type': 'char(32)',
|
127
|
-
'constraint': 'NOT NULL',
|
128
|
-
'comment': 'File MD5.'
|
129
|
-
},
|
130
|
-
{
|
131
|
-
'name': 'size',
|
132
|
-
'type': 'int unsigned',
|
133
|
-
'constraint': 'NOT NULL',
|
134
|
-
'comment': 'File byte size.'
|
135
|
-
},
|
136
|
-
{
|
137
|
-
'name': 'bytes',
|
138
|
-
'type': 'longblob',
|
139
|
-
'constraint': 'NOT NULL',
|
140
|
-
'comment': 'File bytes.'
|
141
|
-
}
|
142
|
-
],
|
143
|
-
'primary': 'md5',
|
144
|
-
'comment': 'File data table.'
|
145
|
-
}
|
146
|
-
]
|
147
|
-
|
148
|
-
## View stats.
|
149
|
-
views_stats = [
|
150
|
-
|
151
|
-
### 'stats'.
|
152
|
-
{
|
153
|
-
'path': ('file', 'stats'),
|
154
|
-
'items': [
|
155
|
-
{
|
156
|
-
'name': 'count',
|
157
|
-
'select': (
|
158
|
-
'SELECT COUNT(1)\n'
|
159
|
-
'FROM `file`.`information`'
|
160
|
-
),
|
161
|
-
'comment': 'File information count.'
|
162
|
-
},
|
163
|
-
{
|
164
|
-
'name': 'count_data',
|
165
|
-
'select': (
|
166
|
-
'SELECT COUNT(1)\n'
|
167
|
-
'FROM `file`.`data`'
|
168
|
-
),
|
169
|
-
'comment': 'File data unique count.'
|
170
|
-
},
|
171
|
-
{
|
172
|
-
'name': 'size_avg',
|
173
|
-
'select': (
|
174
|
-
'SELECT CONCAT(\n'
|
175
|
-
' ROUND(AVG(`size`) / 1024),\n'
|
176
|
-
" ' KB'\n"
|
177
|
-
')\n'
|
178
|
-
'FROM `file`.`data`\n'
|
179
|
-
),
|
180
|
-
'comment': 'File average size.'
|
181
|
-
},
|
182
|
-
{
|
183
|
-
'name': 'size_max',
|
184
|
-
'select': (
|
185
|
-
'SELECT CONCAT(\n'
|
186
|
-
' ROUND(MAX(`size`) / 1024),\n'
|
187
|
-
" ' KB'\n"
|
188
|
-
')\n'
|
189
|
-
'FROM `file`.`data`\n'
|
190
|
-
),
|
191
|
-
'comment': 'File maximum size.'
|
192
|
-
},
|
193
|
-
{
|
194
|
-
'name': 'last_time',
|
195
|
-
'select': (
|
196
|
-
'SELECT MAX(`create_time`)\n'
|
197
|
-
'FROM `file`.`information`'
|
198
|
-
),
|
199
|
-
'comment': 'File last record create time.'
|
200
|
-
}
|
201
|
-
]
|
202
|
-
}
|
203
|
-
]
|
204
|
-
|
205
|
-
# Build.
|
206
|
-
self.rdatabase.build.build(databases, tables, views_stats=views_stats)
|
207
|
-
|
208
|
-
|
209
|
-
def upload(
|
210
|
-
self,
|
211
|
-
file: str | bytes,
|
212
|
-
name: str | None = None,
|
213
|
-
note: str | None = None
|
214
|
-
) -> int:
|
215
|
-
"""
|
216
|
-
Upload file.
|
217
|
-
|
218
|
-
Parameters
|
219
|
-
----------
|
220
|
-
file : File path or file bytes.
|
221
|
-
name : File name.
|
222
|
-
- `None`: Automatic set.
|
223
|
-
`parameter 'file' is 'str'`: Use path file name.
|
224
|
-
`parameter 'file' is 'bytes'`: No name.
|
225
|
-
- `str`: Use this name.
|
226
|
-
note : File note.
|
227
|
-
|
228
|
-
Returns
|
229
|
-
-------
|
230
|
-
File ID.
|
231
|
-
"""
|
232
|
-
|
233
|
-
# Get parameter.
|
234
|
-
conn = self.rdatabase.connect()
|
235
|
-
match file:
|
236
|
-
|
237
|
-
## File path.
|
238
|
-
case str():
|
239
|
-
rfile = RFile(file)
|
240
|
-
file_bytes = rfile.bytes
|
241
|
-
file_md5 = get_md5(file_bytes)
|
242
|
-
file_name = rfile.name_suffix
|
243
|
-
|
244
|
-
## File bytes.
|
245
|
-
case bytes() | bytearray():
|
246
|
-
if file.__class__ == bytearray:
|
247
|
-
file = bytes(file)
|
248
|
-
file_bytes = file
|
249
|
-
file_md5 = get_md5(file_bytes)
|
250
|
-
file_name = None
|
251
|
-
|
252
|
-
## File name.
|
253
|
-
if name is not None:
|
254
|
-
file_name = name
|
255
|
-
|
256
|
-
## File size.
|
257
|
-
file_size = len(file_bytes)
|
258
|
-
|
259
|
-
# Exist.
|
260
|
-
exist = conn.execute_exist(
|
261
|
-
('file', 'data'),
|
262
|
-
'`md5` = :file_md5',
|
263
|
-
file_md5=file_md5
|
264
|
-
)
|
265
|
-
|
266
|
-
# Upload.
|
267
|
-
|
268
|
-
## Data.
|
269
|
-
if not exist:
|
270
|
-
data = {
|
271
|
-
'md5': file_md5,
|
272
|
-
'size': file_size,
|
273
|
-
'bytes': file_bytes
|
274
|
-
}
|
275
|
-
conn.execute_insert(
|
276
|
-
('file', 'data'),
|
277
|
-
data,
|
278
|
-
'ignore'
|
279
|
-
)
|
280
|
-
|
281
|
-
## Information.
|
282
|
-
data = {
|
283
|
-
'md5': file_md5,
|
284
|
-
'name': file_name,
|
285
|
-
'note': note
|
286
|
-
}
|
287
|
-
conn.execute_insert(
|
288
|
-
('file', 'information'),
|
289
|
-
data
|
290
|
-
)
|
291
|
-
|
292
|
-
# Get ID.
|
293
|
-
file_id = conn.variables['identity']
|
294
|
-
|
295
|
-
# Commit.
|
296
|
-
conn.commit()
|
297
|
-
|
298
|
-
return file_id
|
299
|
-
|
300
|
-
|
301
|
-
@overload
|
302
|
-
def download(
|
303
|
-
self,
|
304
|
-
file_id: int,
|
305
|
-
path: None = None
|
306
|
-
) -> bytes: ...
|
307
|
-
|
308
|
-
@overload
|
309
|
-
def download(
|
310
|
-
self,
|
311
|
-
file_id: int,
|
312
|
-
path: str = None
|
313
|
-
) -> str: ...
|
314
|
-
|
315
|
-
def download(
|
316
|
-
self,
|
317
|
-
file_id: int,
|
318
|
-
path: str | None = None
|
319
|
-
) -> bytes | str:
|
320
|
-
"""
|
321
|
-
Download file.
|
322
|
-
|
323
|
-
Parameters
|
324
|
-
----------
|
325
|
-
file_id : File ID.
|
326
|
-
path : File save path.
|
327
|
-
- `None`: Not save and return file bytes.
|
328
|
-
- `str`: Save and return file path.
|
329
|
-
`File path`: Use this file path.
|
330
|
-
`Folder path`: Use this folder path and original name.
|
331
|
-
|
332
|
-
Returns
|
333
|
-
-------
|
334
|
-
File bytes or file path.
|
335
|
-
"""
|
336
|
-
|
337
|
-
# Generate SQL.
|
338
|
-
sql = (
|
339
|
-
'SELECT `name`, (\n'
|
340
|
-
' SELECT `bytes`\n'
|
341
|
-
' FROM `file`.`data`\n'
|
342
|
-
' WHERE `md5` = `information`.`md5`\n'
|
343
|
-
' LIMIT 1\n'
|
344
|
-
') AS `bytes`\n'
|
345
|
-
'FROM `file`.`information`\n'
|
346
|
-
'WHERE `file_id` = :file_id\n'
|
347
|
-
'LIMIT 1'
|
348
|
-
)
|
349
|
-
|
350
|
-
# Execute SQL.
|
351
|
-
result = self.rdatabase(sql, file_id=file_id)
|
352
|
-
|
353
|
-
# Check.
|
354
|
-
if result.empty:
|
355
|
-
text = "file ID '%s' not exist or no data" % file_id
|
356
|
-
raise ValueError(text)
|
357
|
-
file_name, file_bytes = result.first()
|
358
|
-
|
359
|
-
# Not save.
|
360
|
-
if path is None:
|
361
|
-
return file_bytes
|
362
|
-
|
363
|
-
# Save.
|
364
|
-
else:
|
365
|
-
rfolder = RFolder(path)
|
366
|
-
if rfolder:
|
367
|
-
path = os_join(path, file_name)
|
368
|
-
rfile = RFile(path)
|
369
|
-
rfile(file_bytes)
|
370
|
-
return rfile.path
|
371
|
-
|
372
|
-
|
373
|
-
def query(
|
374
|
-
self,
|
375
|
-
file_id: int
|
376
|
-
) -> FileInfo:
|
377
|
-
"""
|
378
|
-
Query file information.
|
379
|
-
|
380
|
-
Parameters
|
381
|
-
----------
|
382
|
-
file_id : File ID.
|
383
|
-
|
384
|
-
Returns
|
385
|
-
-------
|
386
|
-
File information.
|
387
|
-
"""
|
388
|
-
|
389
|
-
# Generate SQL.
|
390
|
-
sql = (
|
391
|
-
'SELECT `create_time`, `md5`, `name`, `note`, (\n'
|
392
|
-
' SELECT `size`\n'
|
393
|
-
' FROM `file`.`data`\n'
|
394
|
-
' WHERE `md5` = `a`.`md5`\n'
|
395
|
-
' LIMIT 1\n'
|
396
|
-
') AS `size`\n'
|
397
|
-
'FROM `file`.`information` AS `a`\n'
|
398
|
-
'WHERE `file_id` = :file_id\n'
|
399
|
-
'LIMIT 1'
|
400
|
-
)
|
401
|
-
|
402
|
-
# Execute SQL.
|
403
|
-
result = self.rdatabase.execute(sql, file_id=file_id)
|
404
|
-
|
405
|
-
# Check.
|
406
|
-
if result.empty:
|
407
|
-
raise AssertionError('file ID does not exist')
|
408
|
-
|
409
|
-
# Convert.
|
410
|
-
table = result.fetch_table()
|
411
|
-
info = table[0]
|
412
|
-
|
413
|
-
return info
|
414
|
-
|
415
|
-
|
416
|
-
__call__ = build
|