/
fsDataStore.py
executable file
·152 lines (107 loc) · 5.13 KB
/
fsDataStore.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
'''
This class is used to store/retrieve in files.
Author : Naresh Pratap Singh
E-Mail : MAIL2NARESH@GMAIL.COM
Date : Jan 21 2011
Project Advisor : Prof. Steven Skiena
'''
import debugTrace, errorStrings
import os
import siteConfig
import email
class fsDataStore:
'''
This class is used to store/retrieve in files.
'''
fsRoot = None
fsDataDir = None
longPathPrefix = '\\\\?\\'
debugEnabled = False
def __init__(self, fsRoot, debugEnabled = False):
self.fsRoot = os.path.abspath(fsRoot)
self.debugEnabled = debugEnabled
self.debugTraceInst = debugTrace.debugTrace(self.debugEnabled)
self.errorStringsInst = errorStrings.errorStrings()
def doConnect(self):
if os.path.exists(self.fsRoot) is False:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getInvalidConfigError())
return False
return True
def doDisconnect(self):
self.fsRoot = None
def doListStores(self):
if self.fsDataDir is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getEarlyRequestError())
return None
pathInfo = os.listdir(self.fsRoot)
dirList = []
for item in pathInfo:
if os.path.isdir(os.path.join(self.fsRoot, item)):
dirList.append(item)
return dirList
def doListItems(self):
if self.fsDataDir is None or self.fsDataDir is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getEarlyRequestError())
return None
storePath = os.path.join(self.fsRoot, self.fsDataDir)
fileList = []
if os.path.exists(storePath):
itemList = os.listdir(storePath)
for item in itemList:
if os.path.isdir(os.path.join(storePath, item)) is False:
fileList.append(item)
else:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getResourceNotFoundError())
return fileList
def doSelect(self, dataDir):
self.fsDataDir = dataDir
fullPath = os.path.join(self.fsRoot, self.fsDataDir)
'''curUid = os.geteuid()
os.seteuid(siteConfig.siteUid)
print os.geteuid()
'''
if os.path.exists(fullPath) is False:
os.mkdir(fullPath)
'''
os.seteuid(curUid)
print os.geteuid()
'''
def doFetch(self, fileName, delimitChar):
if self.fsDataDir is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getEarlyRequestError(), sys.exc_info()[2])
return None
filePath = os.path.join(self.fsRoot, self.fsDataDir, fileName)
if os.path.exists(filePath):
#Read File If Exists
fileHandle = open(filePath, 'r')
fileContents = email.message_from_file(fileHandle);
fileHandle.close()
return fileContents
else:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getResourceNotFoundError(), sys.exc_info()[2])
return None
def doSearch(self, pattern):
if self.fsDataDir is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getEarlyRequestError())
return None
def doUpdate(self, fileName, fileContents, delimitChar, shouldAppend = False):
if self.fsDataDir is None:
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getEarlyRequestError())
return None
filePath = os.path.join(self.fsRoot, self.fsDataDir, fileName)
fileHandle = None
#Enable for Windows
#filePath = self.longPathPrefix + filePath
try:
if shouldAppend is True:
#Open with append permissions
fileHandle = open(filePath, 'a')
else:
#Create New File
fileHandle = open(filePath, 'w')
for line in fileContents:
fileHandle.write(line)
except IOError, (errno, strerror):
self.debugTraceInst.doPrintTrace(self.errorStringsInst.getFailedRequestError()+'IOError: (%s): %s'%(errno, strerror))
fileHandle.close()
return None