-
Notifications
You must be signed in to change notification settings - Fork 34
/
gDriveDB.py
47 lines (33 loc) · 1.19 KB
/
gDriveDB.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
import pickle
import threading
from sqlalchemy import Column, Integer, String, LargeBinary
from bot.helpers.sql_helper import BASE, SESSION
class gDriveCreds(BASE):
__tablename__ = "gDrive"
chat_id = Column(Integer, primary_key=True)
credential_string = Column(LargeBinary)
def __init__(self, chat_id):
self.chat_id = chat_id
gDriveCreds.__table__.create(checkfirst=True)
INSERTION_LOCK = threading.RLock()
def _set(chat_id, credential_string):
with INSERTION_LOCK:
saved_cred = SESSION.query(gDriveCreds).get(chat_id)
if not saved_cred:
saved_cred = gDriveCreds(chat_id)
saved_cred.credential_string = pickle.dumps(credential_string)
SESSION.add(saved_cred)
SESSION.commit()
def search(chat_id):
with INSERTION_LOCK:
saved_cred = SESSION.query(gDriveCreds).get(chat_id)
creds = None
if saved_cred is not None:
creds = pickle.loads(saved_cred.credential_string)
return creds
def _clear(chat_id):
with INSERTION_LOCK:
saved_cred = SESSION.query(gDriveCreds).get(chat_id)
if saved_cred:
SESSION.delete(saved_cred)
SESSION.commit()