-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
76 lines (63 loc) · 2.16 KB
/
db.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
from configparser import ConfigParser
from pymongo import MongoClient
class MongoDB:
default_config = 'sql.cfg'
def __init__(self, config=None):
if not config:
self.config = ConfigParser()
self.config.read(MongoDB.default_config)
self.connected = False
def open(self):
if not self.connected:
self.client = MongoClient(self.connection_string())
self.connected = True
def close(self):
if self.connected:
self.client.close()
self.connected = False
def get_host(self):
mongocfg = self.config['mongodb']
if 'Host' not in mongocfg:
raise KeyError
return mongocfg['Host']
def get_port(self):
mongocfg = self.config['mongodb']
if 'Port' not in mongocfg:
raise KeyError
return mongocfg['Port']
def set_host(self, host):
if 'mongodb' not in self.config:
self.config['mongodb'] = dict()
self.config['mongodb']['Host'] = host
def set_port(self, port):
if 'mongodb' not in self.config:
self.config['mongodb'] = dict()
self.config['mongodb']['Port'] = port
def set_config(self, section, key, value):
if not isinstance(section, str):
raise TypeError
if section not in self.config:
self.config[section] = dict
self.config[section][key] = value
def get_config(self, section, key):
if not isinstance(section, key) or not isinstance(key, str):
raise TypeError
if section not in self.config:
raise KeyError
sectioncfg = self.config[section]
if key not in sectioncfg:
return None
return sectioncfg[key]
def connection_string(self):
host = self.get_host()
port = self.get_port()
if not host or not port:
raise ValueError
return f'mongodb://{host}:{port}/'
def get_database(self, database):
if not isinstance(database, str):
raise TypeError
if self.connected:
return self.client[database]
else:
raise ConnectionError