-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileIO.py
More file actions
208 lines (197 loc) · 5.26 KB
/
fileIO.py
File metadata and controls
208 lines (197 loc) · 5.26 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
### Helper class to read and write to files
import cPickle as pickle
import os
import bisect
import sys
import random
class AlertJob(object):
def __init__(self,chatId, messageId, user, due, message):
self.chatId = chatId
self.messageId = messageId
self.user = user
self.due = due
self.message = message
class Quote(object):
def __init__(self, quoteId, message, user):
self.quoteId = quoteId
self.message = message
self.user = user
def writeAlertJob(WriteTo, chatID, messageid, user, due, message):
myJob = AlertJob(chatID, messageid, user, due, message)
with open(WriteTo, "ab") as myfile:
pickle.dump(myJob, myfile, -1)
del myJob
def readJobs(readFrom):
myJobs = []
try:
f = open(readFrom, 'r+b')
while True:
try:
myJobs.append(pickle.load(f))
except EOFError:
os.remove(readFrom)
break
return myJobs
except IOError:
print "File: {} can not be opened for reading.".format(readFrom)
return 0
def getQuote(readFrom, quoteID):
quotes = []
readFrom = readFrom + "/" + readFrom
try:
f = open(readFrom, 'r+b')
while True:
try:
quotes.append(pickle.load(f))
except EOFError:
f.close()
break
for quote in quotes:
if str(quote.quoteId) == str(quoteID):
return quote.message
return "No quote with that ID found."
except (OSError, IOError):
return "No quotes made in this chat."
except:
return "No quote found or there was an error"
def getRandQuote(readFrom):
quotes = []
readFrom = readFrom + "/" + readFrom
if not os.path.exists(os.path.dirname(readFrom)):
try:
os.makedirs(os.path.dirname(readFrom))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
try:
f = open(readFrom, 'r+b')
while True:
try:
quotes.append(pickle.load(f))
except EOFError:
f.close()
break
if len(quotes) != 0:
quote = random.choice(quotes)
prefix = "Quote: " + str(quote.quoteId) + "\n"
return prefix + quote.message
else:
return "No quotes made in this chat"
except (OSError, IOError):
return "No quotes made in this chat."
except:
print "error happned in getRandQuote ", sys.exc_info()[0]
return "No quote found or there was an error"
def removeQuote(readFrom, quoteID, user):
quotes = []
qutefound = 0
readFrom = readFrom + "/" + readFrom
try:
f = open(readFrom, 'r+b')
while True:
try:
quotes.append(pickle.load(f))
except EOFError:
f.close()
break
for quote in quotes:
if str(quote.quoteId) == str(quoteID) and str(quote.user) == str(user):
quotes.remove(quote)
os.remove(readFrom)
quotefound = 1
break
if quote.quoteId == quoteID:
return "You didn't make that quote!"
if quotefound != 1:
return "No quote with that id found."
except (OSError, IOError):
return "No quotes made in this chat."
except:
return "No quote found"
try:
with open(readFrom, "ab") as myfile:
for quote in quotes:
pickle.dump(quote, myfile, -1)
freeUpKey(readFrom, quoteID)
return "Quote removed"
except (OSError, IOError):
return "Something bad happend related to fileIO, all your quotes are gone, I should probably start using a database."
except:
return "Something bad happend, all your quotes are gone, I should probably start using a database."
def freeUpKey(readfrom, quoteId):
try:
myfile = str(readfrom) + "quoteIds"
f = open(myfile, "r+b")
d = f.read().splitlines()
f.seek(0)
for i in d:
if str(i) != str(quoteId):
f.write("%s\n" % i)
f.truncate()
f.close()
except:
print "error happned in free up key ", sys.exc_info()[0]
def getQuoteId(writeTo):
if not os.path.exists(os.path.dirname(writeTo)):
try:
os.makedirs(os.path.dirname(writeTo))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
myfile = str(writeTo) + "quoteIds"
ids = []
newid = 1
try:
with open(myfile, "rb") as myFile:
ids = [line.rstrip('\n') for line in myFile]
ids = map(int, ids)
ids = sorted(ids)
os.remove(myfile)
except:
print "error opening and getting ids"
try:
uniqueId = 0
if len(ids) == 0:
ids.append(uniqueId)
with open(myfile, "w+b") as myFile:
for item in ids:
myFile.write("%s\n" % item)
return uniqueId
while uniqueId < len(ids):
if uniqueId in ids:
uniqueId = uniqueId+1
else:
ids.append(uniqueId)
break
ids.append(uniqueId)
with open(myfile, "w+b") as myFile:
for item in ids:
myFile.write("%s\n" % item)
return str(uniqueId)
except:
print "Error found getting id " + str(sys.exc_info())
def WriteAQuote(WriteTo, quote, user):
WriteTo = WriteTo + "/" + WriteTo
if not os.path.exists(os.path.dirname(WriteTo)):
try:
os.makedirs(os.path.dirname(WriteTo))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
quoteID = getQuoteId(WriteTo)
myQuote = Quote(quoteID, quote, user)
with open(WriteTo, "ab") as myfile:
pickle.dump(myQuote, myfile, -1)
del myQuote
return "Quote %s Added." %(quoteID)
def main():
print "hello"
WriteAQuote("0123", "quote", "123")
WriteAQuote("0123", "quote 2", "123")
getQuote("0123", "1")
getRandQuote("0123")
removeQuote("0123", "1", "123")
getRandQuote("0123")
if __name__ == '__main__':
main()