-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBCFclass.py
More file actions
304 lines (280 loc) · 10.4 KB
/
BCFclass.py
File metadata and controls
304 lines (280 loc) · 10.4 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
'''
Collection of classes for BCF file reader.
The main class is BCFfile that reads a *.bcfzip file content.
Emmanuel Maschas - November 2020
'''
import os
import zipfile
import xml.etree.ElementTree as XML
from datetime import datetime
def getXMLtext(node, name):
"""
Read an XML node text and returns it or "-" if the node does not exist
"""
try: res = node.find(name).text
except: res = "-"
if res == None: res = "-"
return res
def getXMLdate(node, name):
"""
Read an XML node text containing an ISO date and returns it or None if the node does not exist
"""
try: res = datetime.fromisoformat(node.find(name).text[:19])
except: res = None
return res
def getXMLattr(node, name):
"""
Read an XML node attribute and returns it or "-" if the attribute does not exist
"""
try: res = node.get(name)
except: res = "-"
if res == None: res = "-"
return res
class BCFcomment:
def __init__(self):
self.Guid = ""
self.Date = None
self.Author = ""
self.Comment = ""
self.Viewpoint = ""
self.ModifiedDate = None
self.ModifiedAuthor = ""
def __repr__(self):
txt = "COMMENT :\n========="
#txt += "\nGuid : " + self.Guid
if self.Date != None:
txt += "\nDate : " + self.Date.strftime("%d-%m-%Y")
else:
txt += "\nDate : -"
txt += "\nAuthor : " + self.Author
if self.ModifiedDate != None and (self.ModifiedDate != self.Date or self.ModifiedAuthor != self.Author) :
txt += "\nModifiedDate : " + self.ModifiedDate.strftime("%d-%m-%Y")
txt += "\nModifiedAuthor : " + self.ModifiedAuthor
txt += "\nComment : " + self.Comment
if self.Viewpoint!="-": txt += "\nViewpoint : " + self.Viewpoint
return txt
def index(self):
"""
Function that returns the Date of the comment as a sort key
"""
try: res = int(self.Date.strftime("%Y%m%d%H%M%S"))
except: res = "0000"
return res
def read(self, node):
"""
Read BCF Comment from an XML node
"""
self.Guid = getXMLattr(node, "Guid")
self.Date = getXMLdate(node, "Date")
self.Author = getXMLtext(node, "Author")
self.Comment = getXMLtext(node, "Comment")
self.ModifiedDate = getXMLdate(node, "ModifiedDate")
self.ModifiedAuthor = getXMLtext(node, "ModifiedAuthor")
self.Viewpoint = getXMLattr(node.find("Viewpoint"), "Guid")
class BCFtopic:
def __init__(self):
self.Guid = ""
self.TopicType = ""
self.TopicStatus = ""
self.Title = ""
self.Priority = ""
self.Index = ""
self.Labels = ""
self.CreationAuthor = ""
self.CreationDate = None
self.ModifiedAuthor = ""
self.ModifiedDate = None
self.DueDate = None
self.AssignedTo = ""
self.Description = ""
self.Stage = ""
self.ReferenceLink = ""
self.Comments = []
self.Viewpoints = []
def __repr__(self):
txt = "TOPIC :\n======="
#txt += "\nGuid : " + self.Guid
txt += "\nTopicType : " + self.TopicType
txt += "\nTopicStatus : " + self.TopicStatus
txt += "\nTitle : " + self.Title
txt += "\nDescription : " + self.Description
if self.Priority != "-": txt += "\nPriority : " + self.Priority
txt += "\nIndex : " + self.Index
if self.CreationDate != None:
txt += "\nCreationDate : " + self.CreationDate.strftime("%d-%m-%Y")
else: # Should not happen...
txt += "\nCreationDate : -"
txt += "\nCreationAuthor : " + self.CreationAuthor
if self.ModifiedDate != None and (self.ModifiedDate != self.CreationDate or self.ModifiedAuthor != self.CreationAuthor) :
txt += "\nModifiedDate : " + self.ModifiedDate.strftime("%d-%m-%Y")
txt += "\nModifiedAuthor : " + self.ModifiedAuthor
if self.AssignedTo != "-": txt += "\nAssignedTo : " + self.AssignedTo
if self.DueDate != None: txt += "\nDueDate : " + self.DueDate
if self.Stage != "-": txt += "\nStage : " + self.Stage
return txt
def index(self):
"""
Function that returns the Index of the comment as a sort key
"""
try: res = int(self.Index)
except: res = 0
return res
def addComment(self, comment):
"""
Append the "BCFcomment" to the topic's Comments list
"""
self.Comments.append(comment)
def addViewpoint(self, viewpoint):
"""
Append the "BCFviewpoint" to the topic's Viewpoints list
"""
self.Viewpoints.append(viewpoint)
def read(self, node):
"""
Read a BCF topic from an XML node.
Includes Comments and Viewpoints.
"""
self.Guid = getXMLattr(node, "Guid")
self.TopicType = getXMLattr(node, "TopicType")
self.TopicStatus = getXMLattr(node, "TopicStatus")
self.Title = getXMLtext(node, "Title")
self.Priority = getXMLtext(node, "Priority")
self.Index = getXMLtext(node, "Index")
self.Labels = getXMLtext(node, "Labels")
self.CreationAuthor = getXMLtext(node, "CreationAuthor")
self.CreationDate = getXMLdate(node, "CreationDate")
self.ModifiedAuthor = getXMLtext(node, "ModifiedAuthor")
self.ModifiedDate = getXMLdate(node, "ModifiedDate")
self.DueDate = getXMLdate(node, "DueDate")
self.AssignedTo = getXMLtext(node, "AssignedTo")
self.Description = getXMLtext(node, "Description")
self.Stage = getXMLtext(node, "Stage")
self.ReferenceLink = getXMLtext(node, "ReferenceLink")
class BCFviewpoint:
def __init__(self):
self.Guid = ""
self.Viewpoint = ""
self.Snapshot = ""
self.ViewIndex = ""
self.CameraViewPoint = (0.0, 0.0, 0.0)
self.CameraDirection = (0.0, 1.0, 0.0)
self.CameraUpVector = (0.0, 0.0, 1.0)
self.FieldOfView = 60.0
def __repr__(self):
txt = "VIEWPOINT :\n==========="
#txt += "\nGuid : " + self.Guid
txt += "\nViewpoint : " + self.Viewpoint
txt += "\nSnapshot : " + self.Snapshot
txt += "\nIndex : " + self.ViewIndex
txt += "\nCameraViewPoint : X=%.3f Y=%.3f Z=%.3f" % self.CameraViewPoint
txt += "\nCameraDirection : X=%.3f Y=%.3f Z=%.3f" % self.CameraDirection
txt += "\nCameraUpVector : X=%.3f Y=%.3f Z=%.3f" % self.CameraUpVector
return txt
def index(self):
"""
Function that returns the Viepoint Index of the comment as a sort key
"""
try: res = int(self.ViewIndex)
except: res = 0
return res
def read(self, node, bcfzip, directory):
"""
Read BCF viewpoint from an XML node and the associated .bcfv file
"""
if node != None :
self.Guid = getXMLattr(node, "Guid")
self.Viewpoint = getXMLtext(node, "Viewpoint")
self.Snapshot = getXMLtext(node, "Snapshot")
self.ViewIndex = getXMLtext(node, "Index")
else : # no node => try "viewpoint.bcfv" file
self.Guid = "-"
self.Viewpoint = "viewpoint.bcfv"
self.Snapshot = "snapshot.png"
self.ViewIndex = "-"
try:
visualisationbcfv = bcfzip.open(directory + self.Viewpoint)
except:
self.Guid = None # Empty Viewpoint : file note found...
else:
visualisations = XML.fromstring(visualisationbcfv.read())
camera = visualisations.find("PerspectiveCamera")
if camera:
point = camera.find("CameraViewPoint")
X = point.find("X").text
Y = point.find("Y").text
Z = point.find("Z").text
self.CameraViewPoint = (float(X), float(Y),float(Z))
point = camera.find("CameraDirection")
X = point.find("X").text
Y = point.find("Y").text
Z = point.find("Z").text
self.CameraDirection = (float(X), float(Y),float(Z))
point = camera.find("CameraUpVector")
X = point.find("X").text
Y = point.find("Y").text
Z = point.find("Z").text
self.FieldOfView = float(camera.find("FieldOfView").text)
else:
self.CameraViewPoint = (0.0, 0.0, 0.0)
self.CameraDirection = (0.0, 1.0, 0.0)
self.CameraUpVector = (0.0, 0.0, 1.0)
self.FieldOfView = 60.0
visualisationbcfv.close()
class BCFfile:
def __init__(self, filename=""):
self.filename = filename
self.Topics = []
self.bcfzip = None
if os.path.exists(filename):
if os.path.isfile(filename):
self.bcfzip = zipfile.ZipFile(filename)
self.read()
def __repr__(self):
txt = "BCF FILE\n========"
txt += "\nFile name : " + self.filename
for topic in self.Topics:
txt += "\n\n" + str(topic)
for comment in topic.Comments:
txt += "\n\n" + str(comment)
txt += "\n\n--------"
return txt
def read(self):
"""
Read the active BCF file
"""
self.Topics.clear()
for fi in self.bcfzip.filelist:
if fi.filename[-10:]=="markup.bcf":
#print("Markup : ", fi.filename, "\n")
markupbcf = self.bcfzip.open(fi.filename)
markup = XML.fromstring(markupbcf.read())
tnode = markup.find("Topic")
topic = BCFtopic()
topic.read(tnode)
for cnode in markup.findall("Comment"):
comment = BCFcomment()
comment.read(cnode)
topic.addComment(comment)
topic.Comments.sort(key=BCFcomment.index)
for vnode in markup.findall("Viewpoints"):
viewpoint = BCFviewpoint()
viewpoint.read(vnode, self.bcfzip, fi.filename[:-10])
if viewpoint.Guid != None : topic.addViewpoint(viewpoint)
if len(topic.Viewpoints) == 0: # No Viewpoint ? try "viewpoint.bcfv"
viewpoint = BCFviewpoint()
viewpoint.read(None, self.bcfzip, fi.filename[:-10])
if viewpoint.Guid != None : topic.addViewpoint(viewpoint)
topic.Viewpoints.sort(key=BCFviewpoint.index)
self.Topics.append(topic)
markupbcf.close()
#endif fi is markup.bcf
#next fi
self.Topics.sort(key=BCFtopic.index)
def getImage(self, filename):
"""
Returns the image data of a snapshot file from the active BCF file
"""
data = None
with self.bcfzip.open(filename) as fi:
data = fi.read()
return data