-
Notifications
You must be signed in to change notification settings - Fork 0
/
repositoryIface.py
executable file
·160 lines (110 loc) · 6.79 KB
/
repositoryIface.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'''
This class takes care of fetching/storing data to/from a data provider.
To switch to a different data provider, include the provider module in this file
and add the entry in dataStoreDict. Make sure the relevant interfaces are exposed in
the new provider module.
Author : Naresh Pratap Singh
E-Mail : MAIL2NARESH@GMAIL.COM
Date : Jan 21 2011
Project Advisor : Prof. Steven Skiena
'''
import fsDataStore
import debugTrace, errorStrings
import eMailParser
class repositoryIface:
'''
This class takes care of fetching/storing data to/from a data provider.
'''
dataStoreInst = None
dataStoreType = None
dataStoreRoot = None
dataStoreName = None
delimitChar = '|'
dataStoreDict = {'FILE': [ fsDataStore, 'fsDataStore', {'CONNECT': 'doConnect', 'AUTH': 'doAuthenticate',
'LIST_STORES': 'doListStores', 'LIST_ITEMS': 'doListItems', 'SELECT': 'doSelect', 'SEARCH': 'doSearch',
'FETCH': 'doFetch', 'UPDATE': 'doUpdate', 'DISC': 'doDisconnect'} ]}
searchActionList = [ 'NEW', 'CONT' ]
currentDataIdList = None
debugEnabled = False
# debugTrace instance for trace logging.
debugTraceInst = None
# errorStrings instance for fetching error message strings.
errorStringsInst = None
def __init__(self, dataStoreType, dataStoreRoot, debugEnabled = False):
self.dataStoreType = dataStoreType
self.dataStoreRoot = dataStoreRoot
self.debugEnabled = debugEnabled
self.debugTraceInst = debugTrace.debugTrace(self.debugEnabled)
self.errorStringsInst = errorStrings.errorStrings()
def doConnectToDataStore(self):
if self.dataStoreInst is None:
if self.dataStoreDict.has_key(self.dataStoreType):
dataStoreMod = self.dataStoreDict[ self.dataStoreType ][ 0 ]
dataStoreClass = self.dataStoreDict[ self.dataStoreType ][ 1 ]
self.dataStoreInst = dataStoreMod.__getattribute__(dataStoreClass)(self.dataStoreRoot, self.debugEnabled)
connectMethod = self.dataStoreDict[ self.dataStoreType ][ 2 ][ 'CONNECT' ]
return getattr(self.dataStoreInst, connectMethod)()
else:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getUnsupportedProtocolError())
else:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getAlreadyConnectedError())
return False
def doDisconnect(self):
if self.dataStoreInst is not None:
discMethod = self.dataStoreDict[ self.dataStoreType ][ 2 ][ 'DISC' ]
getattr(self.dataStoreInst, discMethod)
self.dataStoreInst = None
else:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getNotConnectedError())
def doListStores(self):
if self.dataStoreInst is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getNotConnectedError())
return None
listMethod = self.dataStoreDict[ self.dataStoreType ][ 2 ][ 'LIST_STORES' ]
return getattr(self.dataStoreInst, listMethod)()
def doListItems(self):
if self.dataStoreInst is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getNotConnectedError())
return None
listMethod = self.dataStoreDict[ self.dataStoreType ][ 2 ][ 'LIST_ITEMS' ]
return getattr(self.dataStoreInst, listMethod)()
def doSelectDataStore(self, dataStoreName):
if self.dataStoreInst is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getNotConnectedError())
return None
self.dataStoreName = dataStoreName
selectMethod = self.dataStoreDict[ self.dataStoreType ][ 2 ][ 'SELECT' ]
return getattr(self.dataStoreInst, selectMethod)(dataStoreName)
def doFetchItems(self, itemList):
'''
Takes a list of items to fetch as input.
Returns a dictionary of item and their contents.
The name of items form the key.
'''
if self.dataStoreInst is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getNotConnectedError())
return None
fetchMethod = self.dataStoreDict[ self.dataStoreType ][ 2 ][ 'FETCH' ]
retDict = {}
for itemName in itemList:
retDict[ itemName ] = getattr(self.dataStoreInst, fetchMethod)(itemName, self.delimitChar)
return retDict
def doFetchItem(self, itemName):
if self.dataStoreInst is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getNotConnectedError())
return None
fetchMethod = self.dataStoreDict[ self.dataStoreType ][ 2 ][ 'FETCH' ]
return eMailParser.eMailParser().doConvertFileFormatToMessage(getattr(self.dataStoreInst, fetchMethod)(itemName, self.delimitChar))
def doSearchItems(self, pattern):
if self.dataStoreInst is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getNotConnectedError())
return None
searchMethod = self.dataStoreDict[ self.dataStoreType ][ 2 ][ 'SEARCH' ]
return getattr(self.dataStoreInst, searchMethod)(pattern)
def doUpdateItem(self, itemName, itemContents):
if self.dataStoreInst is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getNotConnectedError())
return None
updateMethod = self.dataStoreDict[ self.dataStoreType ][ 2 ][ 'UPDATE' ]
newContents = eMailParser.eMailParser().doConvertMessageToFileFormat(itemContents)
return getattr(self.dataStoreInst, updateMethod)(itemName, newContents, self.delimitChar)