-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.py
132 lines (110 loc) · 3.73 KB
/
Parser.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
'''
Created on 6 sept. 2016
@author: SL
'''
import os
from Document import Document
class Parser(object):
'''
classdocs
'''
def __init__(self, begin, end=""):
'''
Constructor
'''
self.begin=begin
self.end=end
self.file=None
def initFile(self,filename):
self.file=open(filename,"rb")
def __del__(self):
if(self.file is not None):
#print self.file.closed
self.file.close()
#print str(self.file.closed)
def nextDocument(self):
#if((self.file is None) or (self.file.closed())):
# return None
d=None
ligne=""
ok=False;
while(not ok):
st=""
read=False;
start=0;
nbBytes=0;
while(True):
curOff=self.file.tell();
ligne=self.file.readline();
#self.file.seek(curOff,0)
#print ligne
if(len(ligne)==0):
if((len(self.end)==0) and read):
#print ok
nbBytes=curOff-start;
read=False
ok=True
break
if(ligne.startswith(self.begin)):
if((len(self.end)==0) and read):
nbBytes=curOff-start;
read=False;
ok=True;
self.file.seek(curOff)
break;
else:
read=True
start=curOff
if(read):
st+=(ligne)
if((len(self.end)>0) and (ligne.startswith(self.end))):
read=False
ok=True;
nbBytes=self.file.tell()-start;
break
if (ok):
source=os.path.abspath(self.file.name)+";"+str(start)+";"+str(nbBytes)
#print source
d=self.getDocument(st);
d.set("from", source);
else:
self.file.close();
return None
return d
def getDocument(self,text):
raise NotImplementedError
#==============================================================================
# Format of input files :
# <Document id=<id>>
# <Text>
# </Document>
#==============================================================================
class ParserSimple(Parser):
def __init__(self):
'''
Constructor
'''
Parser.__init__(self,"<Document","</Document")
def getDocument(self,text):
other={};
st=text.split("\n");
sti=st[0].split("=");
x=sti[1][0:sti[1].find(">")];
l=[st[i] for i in range(1,len(st)-2)]
text=" ".join(l)+"\n";
doc=Document(x,text,other);
return doc;
#===============================================================================
#===============================================================================
# a=ParserSimple()
# a.initFile("../../data/testPourParserSimple")
# x1=a.nextDocument()
# print x1
# x2=a.nextDocument()
# print x2
# x3=a.nextDocument()
# print x3
# x4=a.nextDocument()
# print x4
#===============================================================================
#===============================================================================