-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageManager.py
More file actions
79 lines (55 loc) · 2.21 KB
/
StorageManager.py
File metadata and controls
79 lines (55 loc) · 2.21 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
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 5 14:13:54 2020
@author: jidou
"""
import pymongo
from diskcache import Cache
#current collections: companiesName, ....., LogInfo
class CDataWrapper:
def __init__(self,attrSet = {'preInfo','data','postInfo'},currentEmpty:list=list()):
self.attrSet = attrSet
self.currentEmpty = currentEmpty
def __call__(self,*args,**kwargs):
return self._getDict(*args,**kwargs)
def _getDict(self,*attrListUser,**attrUser):
userDict = None
attrListUser = attrListUser[0]
if(type(attrListUser) == dict):
userDict = attrListUser
elif(attrUser != None):
userDict = attrUser
else:
raise ValueError("the input should be a dict or several atttibutes with keys indicated")
ans = dict()
for attr in self.attrSet:
temp = userDict.get(attr)
if(temp != None):
ans[attr] = temp
elif attr in self.currentEmpty:
ans[attr] = ''
else:
raise ValueError("lose data for the key %s indicated in attributes set" % attr)
return ans
class CStorage:
def __init__(self,name,path):
self.name = name #+ '_storage'
self.path = path
class CStorageMongoDB(CStorage):
def __init__(self,name,path):
super().__init__(name,path)
print(path)
self.client = pymongo.MongoClient(self.path)
self.dbName = self.name + '_db'
self.db = self.client[self.name + '_db']
def storeData(self,collectionName,wrapper:CDataWrapper,*args,**kwargs):
collection = self.db[collectionName]
document = wrapper(*args,**kwargs)
return collection.insert_one(document)
def checkExist(self,collectionName=None):
if self.dbName not in self.client.database_names():
return False
if collectionName is not None:
if collectionName not in self.db.collection_names():
return False
return True