-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
executable file
·214 lines (175 loc) · 5.49 KB
/
utils.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
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
209
210
211
212
213
######################################################################
######################################################################
# Copyright Tsung-Hsien Wen, Cambridge Dialogue Systems Group, 2017 #
######################################################################
######################################################################
#! /usr/bin/python
# -*- coding: utf-8 -*-
__author__="Filip Jurcicek"
__date__ ="$08-Mar-2010 13:45:34$"
import pickle
import time
import httplib
import urllib
import urlparse
import fcntl
tokenFileName = './mylogdir/tokens.pkl'
workerFileName = './mylogdir/workers.pkl'
lockFile = 0
def lock():
global lockFile
lockFile = open('./mylogdir/lock.file', 'w')
fcntl.lockf(lockFile, fcntl.LOCK_EX)
def unlock():
fcntl.lockf(lockFile, fcntl.LOCK_UN)
lockFile.close()
def httpPost(url, data):
# data = {'spam': 1, 'eggs': 2, 'bacon': 0}
#print url,'\n'
#print data,'\n'
url = urlparse.urlsplit(url)
params = urllib.urlencode(data)
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn = httplib.HTTPConnection(url[1])
conn.request("POST", url[2]+"?"+url[3], params, headers)
response = conn.getresponse()
print response.status, response.reason
resp = response.read()
conn.close()
return resp
def findTokenTuple(token):
try:
lock()
tm = time.time()
# load the old pickle
try:
pkl_file = open(tokenFileName, 'rb')
tokens = pickle.load(pkl_file)
except (IOError, EOFError):
tokens = []
try:
pkl_file.close()
except:
pass
finally:
unlock()
# find the token
for each in tokens:
if token == "voipheslo" and each[0] == token:
return each
# allow only tokens which are not older than 10 minutes
if each[0] == token:# and each[2] > (tm - 600):
return each
return None
def removeToken(tokenTuple):
try:
lock()
tm = time.time()
# load the old pickle
try:
pkl_file = open(tokenFileName, 'rwb')
tokens = pickle.load(pkl_file)
except (IOError, EOFError):
return
# filter out tokens older than 30 minutes
tokens = [x for x in tokens if x[2] > tm - 1800]
# remove the requested token
print "Removing:", tokenTuple
print "Tokens before:", tokens
tokens = [x for x in tokens if x != tokenTuple]
print "Tokens after:", tokens
# save the updated pickle
try:
pkl_file.close()
except:
pass
pkl_file = open(tokenFileName, 'wb')
pickle.dump(tokens, pkl_file)
pkl_file.close()
finally:
unlock()
def saveToken(token, dialogueID):
try:
lock()
tm = time.time()
# load the old pickle
try:
pkl_file = open(tokenFileName, 'rwb')
tokens = pickle.load(pkl_file)
except (IOError, EOFError):
tokens = []
# update the pickle
tokens.append((token,dialogueID,tm))
# filter out tokens older than 10 minutes
tokens = [x for x in tokens if x[2] > tm - 1800]
tokens = [x for x in tokens if x[0] != 'voipheslo']
tokens.append(('voipheslo','/data/dial/gm/Mar12Evaluation/voipheslo',tm))
# save the updated pickle
try:
pkl_file.close()
except:
pass
pkl_file = open(tokenFileName, 'wb')
pickle.dump(tokens, pkl_file)
pkl_file.close()
finally:
unlock()
def saveWorker(workerID, phone):
try:
lock()
tm = time.time()
# load the old pickle
try:
pkl_file = open(workerFileName, 'rwb')
workers = pickle.load(pkl_file)
except (IOError, EOFError):
workers = []
# update the pickle
workers.append((workerID,tm,phone))
# filter out workers older than 4 weeks
workers = [x for x in workers if x[1] > tm - 60*60*24*7*4]
# save the updated pickle
try:
pkl_file.close()
except:
pass
pkl_file = open(workerFileName, 'wb')
pickle.dump(workers, pkl_file)
pkl_file.close()
finally:
unlock()
def verifyWorker(workerID):
try:
lock()
tm = time.time()
# load the old pickle
try:
pkl_file = open(workerFileName, 'rb')
workers = pickle.load(pkl_file)
except (IOError, EOFError):
workers = []
try:
pkl_file.close()
except:
pass
finally:
unlock()
# find the phones of a particular worker
phones = [x[2] for x in workers if x[0] == workerID]
# find workers using the previous phones
phonesWorkers = [x[0] for x in workers if x[2] in phones]
numberSubmitions24h = 0
numberSubmitions4w = 0
for each in workers:
# allow only tokens which are not older than 24 hours
if each[0] in phonesWorkers and each[1] > (tm - 60*60*24):
numberSubmitions24h += 1
# allow only tokens which are not older than 4 weekss
if each[0] in phonesWorkers and each[1] > (tm - 60*60*24*7*1):
numberSubmitions4w += 1
if numberSubmitions24h > 20:
return ">20in24h"
# elif numberSubmitions4w > 50:
# return ">40in4w"
return "OK"