-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysclass.py
57 lines (42 loc) · 1.36 KB
/
sysclass.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
from pymongo import *
import os.path
from os.path import isfile,join
import env_config
"""For config and db classes"""
class Singleton(object):
instance = None
def __setattr__(self, attr, value):
return setattr(self.instance, attr, value) #self.instance.attr = value
def __getattr__(self, attr):
return getattr(self.instance, attr) # = None if not has attr || = value if has attr
def setAttributes(self, attributeDict):
for attribute, attributeValue in attributeDict.iteritems():
setattr(self, attribute, attributeValue)
class Config(Singleton):
instance = None
def __init__(self):
if Config.instance is None:
Config.instance = Config.__Singleton()
class __Singleton:
def __init__(self):
config = env_config.config()
self.dbhost = config["mongo"]["host"]
self.dbname = config["mongo"]["dbname"]
self.username = config["mongo"]["username"]
self.password = config["mongo"]["password"]
self.dbport = config["mongo"]["port"]
self.env = config["environment"]
class DBConnection(Singleton):
instance = None
def __init__(self):
if DBConnection.instance is None:
DBConnection.instance = DBConnection.__Singleton()
class __Singleton:
def __init__(self):
settings = Config()
self.connection = MongoClient(
settings.dbhost,
settings.dbport
)
self.db = self.connection[settings.dbname]
"""End***************"""