-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupload.py
More file actions
67 lines (52 loc) · 1.84 KB
/
upload.py
File metadata and controls
67 lines (52 loc) · 1.84 KB
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
import logging
import os
from io import BufferedReader
from typing import Optional
from sqlitecloud.datatypes import SQLiteCloudConnect
from sqlitecloud.driver import Driver
def xCallback(fd: BufferedReader, blen: int, ntot: int, nprogress: int) -> bytes:
"""
Callback function used for uploading data.
Args:
fd (BufferedReader): The file descriptor to read data from.
blen (int): The length of the buffer to read.
ntot (int): The total number of bytes to be uploaded.
nprogress (int): The number of bytes already uploaded.
Returns:
bytes: The buffer containing the read data.
"""
buffer = fd.read(blen)
nread = len(buffer)
if nread == 0:
logging.log(logging.DEBUG, "UPLOAD COMPLETE\n\n")
else:
logging.log(logging.DEBUG, f"{(nprogress + nread) / ntot * 100:.2f}%")
return buffer
def upload_db(
connection: SQLiteCloudConnect, dbname: str, key: Optional[str], filename: str
) -> None:
"""
Uploads a SQLite database to the SQLite Cloud node using the provided connection.
Args:
connection (SQLiteCloudConnect): The connection object used to connect to the node.
dbname (str): The name of the database in SQLite Cloud.
key (Optional[str]): The encryption key for the database. If None, no encryption is used.
filename (str): The path to the SQLite database file to be uploaded.
Raises:
SQLiteCloudException: If an error occurs while uploading the database.
"""
# Create a driver object
driver = Driver()
with open(filename, "rb") as fd:
dbsize = os.path.getsize(filename)
driver.upload_database(
connection,
dbname,
key,
False,
0,
False,
fd,
dbsize,
xCallback,
)