-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_base.py
executable file
·92 lines (69 loc) · 2.61 KB
/
data_base.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
#!/usr/bin/env python3
import redis
import logging as log
from data.card import Card
class DataBase(object):
singleton_instance = None
#all object's keys look like type:id
#and have dict type
def __init__(self):
self.conn = redis.Redis('localhost', decode_responses=True)
@classmethod
def instance(cls):
if not cls.singleton_instance:
cls.singleton_instance = DataBase()
return cls.singleton_instance
def get_card(self, card_id):
log.debug('DataBase:get_card:card_id: %s' % card_id)
card_dict = self.conn.hgetall('card:%s' % card_id)
log.debug('DataBase:get_card:card_dict: %s' % card_dict)
card = Card.card_from_dict(card_dict)
return card
def save_card(self, card):
if not card.card_id: card_id = self._get_id('card')
else: card_id = card.card_id
card.set_card_id(card_id)
self.conn.hmset('card:%s' % card_id, card.get_card_dict())
return card_id
def delete_card(self, card_id):
self.conn.delete('card:%s' % card_id)
log.info('{} card deleted'.format(card_id))
def add_user(self, name):
user_id = self._get_id('user')
self.conn.hmset('user:%s' % user_id, name)
return user_id
def del_user(self, user_id):
self.conn.delete('user:%s' % user_id)
def _get_id(self, obj_type):
obj_list = self.conn.keys(pattern='%s:*' % obj_type)
if not obj_list:
obj_id = 0
else:
obj_id = max([int(i.split(':')[1]) for i in obj_list]) + 1
return obj_id
def get_message(self, courier_id):
return self.conn.get('courier:message:{}'.format(courier_id))
def set_message(self, courier_id, msg):
self.conn.set('courier:message:{}'.format(courier_id), msg)
def get_timetable(self, courier_id):
return self.conn.get('courier:timetable:{}'.format(courier_id))
def set_timetable(self, courier_id, tt):
self.conn.set('courier:timetable:{}'.format(courier_id), tt)
def process_creds(self, login, password):
return password == self.conn.get('user:{}'.format(login, password))
def add_account(self, name, password):
self.conn.set('user:{}'.format(name), password)
def del_account(self, name):
self.conn.delete('user:{}'.format(name))
if __name__ == '__main__':
#simple test
import sys
d = DataBase.instance()
method = sys.argv[1]
if len(sys.argv) > 2:
arg = sys.argv[2:]
ret = getattr(d, method)(*arg)
print(ret)
sys.exit(0)
ret = getattr(d, method)()
print(ret)