-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileReader.py
More file actions
executable file
·83 lines (67 loc) · 2.26 KB
/
fileReader.py
File metadata and controls
executable file
·83 lines (67 loc) · 2.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
#!/usr/bin/env python
# encoding: UTF-8
import re,os
class FileReader:
fileUri = ""
offsetLast = 0
offsetNew = 0
offsetNow = 0
timestampNew = ""
isHasChanged = False
def read(self, fileUriNow):
with open(fileUriNow) as text:
#print("fileReader, file is " + fileUriNow)
return text.readlines()
def getALine(self, fileLines):
return fileLines.pop()
def match(self, reg, strToMatch, timestamp):
match = re.compile(reg).search(strToMatch)
if match:
timestampNow = match.group()[1:(len(match.group()) - 1)].split(',')[3]
#print (timestampNow)
if not self.isHasChanged:
#print (self.offsetNow)
self.timestampNew = timestampNow
self.offsetNew = self.offsetNow
self.isHasChanged = True
return float(timestampNow) > timestamp
else:
if not self.isHasChanged:
#print("add!!!!")
self.offsetNow += 1
#print(self.offsetNow)
return True
def getNewFilePath(self, suffix):
return self.fileUri + '.' + str(suffix), suffix + 1
def chooseLinesInAFile(self, fileLines, reg, timestamp, strAdded, isTimeReached):
while len(fileLines):
line = self.getALine(fileLines)
#print ("line: " + line)
if self.match(reg, line, timestamp):
strAdded.insert(0, line)
else:
isTimeReached = True
break
return strAdded, isTimeReached
def chooseLines(self, timestamp, offsetL, path):
#self.fileUri = "/home/kun/GraduationProject/ServerClient0.11/test.txt"
self.fileUri = path
#print "file is:" + self.fileUri
self.offsetLast = offsetL
reg = "'iperf.*'"
isTimeReached = False
strAdded = []
fileUriNow = self.fileUri
suffix = 0
while (not isTimeReached and os.path.exists(fileUriNow)):
fileLines = self.read(fileUriNow)
strAdded, isTimeReached = self.chooseLinesInAFile(fileLines, reg, timestamp, strAdded, isTimeReached)
fileUriNow, suffix = self.getNewFilePath(suffix)
print ("isTimeReached: " + str(isTimeReached) + " file is: " + fileUriNow + " suffix is: " + str(suffix) + " is: " + str(os.path.exists(fileUriNow)))
#print (''.join(strAdded))
#print("Now offset is: " + str(self.offsetNew))
return ''.join(strAdded[self.offsetLast:]), self.timestampNew, str(self.offsetNew)
if __name__ == '__main__':
timestamp = 0
fileReader = FileReader()
fileReader.chooseLines(timestamp)