-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.py
248 lines (216 loc) · 9.25 KB
/
project.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import xml.etree.ElementTree as ET
import os
from link import Link
from tim import TIM, Type
from document import Document
class Project :
def __init__(self):
self.name = ""
self.documents = {}
self.documentsOrder = []
self.location = ""
self.imported_files = {}
self.consolidationSettings = {}
self.TIM = None
def getConsolidationSettings(self):
return self.consolidationSettings
def setConsolidationSettings(self, settings):
self.consolidationSettings = settings
def getType2Clauses(self):
type2Clauses = {}
for type in self.TIM.getTypesList():
type2Clauses[type] = []
clausesList = self.getAllClauses().values()
for clause in clausesList :
typeName = clause.getType().getName()
type2Clauses[typeName].append(clause)
return type2Clauses
def loadXML(self, filename):
self.location = os.path.dirname(filename) + '/'
tree = ET.parse(filename)
XML = tree.getroot()
self.name = XML.get("name")
TIMNode= XML.find('tim')
self.loadTIM(TIMNode)
self.documents = {}
self.loadDocuments(XML)
importedFilesNode = XML.find('imported_files')
self.loadImportedFiles(importedFilesNode)
consolidationNode = XML.find('consolidation')
if consolidationNode is not None :
self.loadConsolidation(consolidationNode)
linksList = self.getAllLinks()
for link in linksList :
link.consolidateLink(self)
def loadDocuments(self, documentsNode):
for document_node in documentsNode.findall("document") :
document = Document()
document_name = document_node.get("name")
document.loadXML(self, document_name +".xml")
self.documents[document_name] = document
for clauseId in document.getClausesList():
clause = document.getClause(clauseId)
clauseType = clause.getType()
clause.setType(self.TIM.getType(clauseType))
order = documentsNode.find("order").text
if order is not None :
self.documentsOrder = order.split(";")
else :
self.documentsOrder = []
def loadImportedFiles(self, importedFilesNode):
for fileNode in importedFilesNode.findall('file'):
self.imported_files[fileNode.get('name')] = fileNode.text
def loadConsolidation(self, consolidationNode):
if consolidationNode.get('docPrefix') == 'yes' :
self.consolidationSettings['docPrefix'] = True
else :
self.consolidationSettings['docPrefix'] = False
if consolidationNode.get('typePrefix') == 'yes' :
self.consolidationSettings['typePrefix'] = True
else :
self.consolidationSettings['typePrefix'] = False
if consolidationNode.get('unifyDocuments', 'no') == 'yes' :
self.consolidationSettings['unifyDocuments'] = True
else :
self.consolidationSettings['unifyDocuments'] = False
self.consolidationSettings['order'] = consolidationNode.get('order')
documents = consolidationNode.find('documents')
self.consolidationSettings['documents'] = documents.text.split(';')
documents = consolidationNode.find('types')
self.consolidationSettings['types'] = documents.text.split(';')
limitsNode = consolidationNode.find('limits')
if (limitsNode is not None) :
limits = {}
prefixNodes = limitsNode.findall('prefix')
for prefixNode in prefixNodes :
limits[prefixNode.get('prefix')] = int(prefixNode.get('limit'))
self.consolidationSettings['limits'] = limits
def loadTIM(self, TIMNode):
self.TIM = TIM()
self.TIM.loadXML(TIMNode)
def moveDocument(self, document, step):
index = self.documentsOrder.index(document)
newIndex = index+step
if ((newIndex >= 0) and (newIndex<len(self.documentsOrder))) :
self.documentsOrder.insert(newIndex, self.documentsOrder.pop(index))
def saveDocuments(self, documentsNode):
for documentName in self.getDocumentsList():
documentNode = ET.SubElement(documentsNode, 'document')
documentNode.set('name', documentName)
orderNode = ET.SubElement(documentsNode, 'order')
orderText = ""
for document in self.documentsOrder :
orderText += document + ";"
orderNode.text = orderText.strip(";")
def saveImportedFiles(self, importedFilesNode):
for file in self.imported_files.keys() :
fileNode= ET.SubElement(importedFilesNode , 'file')
fileNode.set('name', file)
fileNode.text = self.imported_files[file]
def saveConsolidation(self, consolidationsNode):
docPrefix = ""
if (self.consolidationSettings['docPrefix']):
docPrefix = 'yes'
else :
docPrefix = 'no'
typePrefix = ""
if (self.consolidationSettings['typePrefix']):
typePrefix = 'yes'
else:
typePrefix = 'no'
if (self.consolidationSettings['unifyDocuments']):
unifyDocuments = 'yes'
else:
unifyDocuments = 'no'
consolidationsNode.set('order', self.consolidationSettings['order'])
consolidationsNode.set('docPrefix', docPrefix)
consolidationsNode.set('typePrefix', typePrefix)
consolidationsNode.set('unifyDocuments', unifyDocuments)
documentsNode = ET.SubElement(consolidationsNode, 'documents')
documentsNode.text = ';'.join(self.consolidationSettings['documents'])
typesNode = ET.SubElement(consolidationsNode, 'types')
typesNode.text = ';'.join(self.consolidationSettings['types'])
limitsNode = ET.SubElement(consolidationsNode, 'limits')
if 'limits' in self.consolidationSettings.keys() :
limits = self.consolidationSettings['limits']
for limit in limits :
limitNode = ET.SubElement(limitsNode, 'prefix')
limitNode.set('prefix', limit)
limitNode.set('limit', str(limits[limit]))
def save(self):
projectNode = ET.Element('project')
projectNode.set('name', self.name)
self.saveDocuments(projectNode)
importedFilesNode = ET.SubElement(projectNode, 'imported_files')
self.saveImportedFiles(importedFilesNode)
if (self.consolidationSettings != {}) :
consolidationsNode = ET.SubElement(projectNode, 'consolidation')
self.saveConsolidation(consolidationsNode)
TIMNode= ET.SubElement(projectNode, 'tim')
self.TIM.save(TIMNode)
projectNode.tail = "\n"
tree = ET.ElementTree(element=projectNode)
tree.write(self.getLocation()+ self.getName() + ".prj")
def saveAll(self):
self.save()
for document_name in self.documents :
self.documents[document_name].save()
def getDocument(self, name):
return self.documents[name]
def getAllClauses(self):
list = {}
for documentName in self.getDocumentsList() :
documentObj = self.getDocument(documentName)
for clause in documentObj.getClausesList():
list[clause] = documentObj.getClause(clause)
return list
def getDocumentsList(self):
return self.documentsOrder
def removeDocument(self, documentName):
document = self.documents[documentName]
document.destroy()
del self.documents[documentName]
self.documentsOrder.remove(documentName)
def getName(self):
return self.name
def setName(self, name):
self.name = name
def getLocation(self):
return self.location
def setLocation(self, location):
os.mkdir(location + 'imported')
self.location = location
def addDocument(self, document):
self.documents[document.getName()] = document
self.documentsOrder.append(document.getName())
def getAllLinks(self):
linksList = []
for documentName in self.getDocumentsList() :
documentObj = self.getDocument(documentName)
for clauseName in documentObj.getClausesList() :
clauseObj = documentObj.getClause(clauseName)
linksList += clauseObj.getChildLinksList()
return linksList
def getDocument2ClausesDict(self):
list = {}
for documentName in self.getDocumentsList() :
documentObj = self.getDocument(documentName)
list[documentName] = documentObj.getClausesList()
return list
def setTIM(self, TIM):
self.TIM = TIM
def getTIM(self):
return self.TIM
def getImportedFilesList(self):
return sorted(set(self.imported_files.keys()))
def addImportedFile(self, file, description):
self.imported_files[file] = description
def getImportedFileDescription(self, file):
if file in self.imported_files:
return self.imported_files[file]
else:
return ""
def setImportedFileDescription(self, file, description):
self.imported_files[unicode(file)] = description