-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathVersionModel.py
330 lines (260 loc) · 10.7 KB
/
VersionModel.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# ***********************************************************************
# * *
# * Copyright (c) 2023 Ondsel *
# * *
# ***********************************************************************
from PySide.QtCore import Qt, QAbstractListModel, QModelIndex, QFileSystemWatcher
from tzlocal import get_localzone
import datetime
import os
import xml.etree.ElementTree as ET
import zipfile
import Utils
logger = Utils.getLogger(__name__)
CONVERT_TO_LOCAL_TZ = True
class VersionModel(QAbstractListModel):
def __init__(self, parent=None):
super().__init__(parent)
self.versions = []
@staticmethod
def getVersionDateTime(version):
"""
Return the updated and created date given a server version
The updatedDate may not be available in which case the createdDate is
used.
"""
createdDate = version["createdAt"]
updatedDate = version["additionalData"].get("fileUpdatedAt", createdDate)
return updatedDate, createdDate
def refreshModel(self):
pass # Implemented in subclasses
def clearModel(self):
self.beginResetModel()
self.beginRemoveRows(QModelIndex(), 0, self.rowCount() - 1)
self.versions = []
self.endResetModel()
def data(self, index, role):
pass # Implemented in subclasses
def convertTime(self, time, convertToLocalTZ=False):
"""
This converts a time string to the user's local timezone using tzlocal
and outputs it in a friendly format
"""
# Get the user's local timezone
user_timezone = get_localzone()
try:
# Convert the time string to a datetime object
if isinstance(time, int):
time_obj = datetime.datetime.fromtimestamp(time)
else:
time_obj = datetime.datetime.strptime(time, "%Y-%m-%dT%H:%M:%SZ")
if convertToLocalTZ:
# Convert the time to the user's local timezone
time_obj = time_obj.replace(tzinfo=datetime.timezone.utc).astimezone(
user_timezone
)
# Format the local time as a friendly string
time_str = time_obj.strftime("%Y-%m-%d %H:%M:%S")
return time_str
except ValueError:
# Handle invalid time string format
return "Invalid time format"
def dump(self):
"""
useful for debugging. This will return the contents in a printable form
"""
data = []
for row in range(self.rowCount()):
index = self.index(row, QModelIndex())
value = self.data(index, Qt.DisplayRole)
data.append(value)
print(data)
def addNewVersion(self, filename):
pass # Implemented in subclasses
def rowCount(self, index=QModelIndex()):
return len(self.versions)
class LocalVersionModel(VersionModel):
def __init__(self, filename, parent=None):
"""
expects a filename ending in .FCStd.
"""
super().__init__(parent)
if not os.path.isfile(filename):
raise FileNotFoundError(f"The specified file: {filename} doesn't exist")
self.filename = filename
self.path = os.path.dirname(self.filename)
file = os.path.basename(self.filename)
base, extension = os.path.splitext(file)
self.watcher = QFileSystemWatcher()
self.watcher.fileChanged.connect(self.refreshModel)
self.watcher.directoryChanged.connect(self.refreshModel)
self.watcher.addPath(self.path)
self.refreshModel()
def refreshModel(self):
self.clearModel()
for fname in os.listdir(self.path):
self.addNewVersion(fname)
def _getFCFileInfo(self, filename):
"""
Extracts the CreationDate from the Document.xml so we aren't
relying on filesystem.
Also returns the FreeCAD version used to create the file.
"""
result = {}
# Open the ZIP file
with zipfile.ZipFile(filename, "r") as zip_ref:
# Extract the Document.xml file from the ZIP
with zip_ref.open("Document.xml") as xml_file:
# Read the XML content
xml_content = xml_file.read()
# Parse the XML
root = ET.fromstring(xml_content)
# Extract CreationDate and ProgramVersion values
lastModifiedDate = root.find(
".//Property[@name='LastModifiedDate']/String"
).get("value")
program_version = root.get("ProgramVersion")
# Add the values to the result dictionary
result["CreationDate"] = self.convertTime(
lastModifiedDate, CONVERT_TO_LOCAL_TZ
)
result["ProgramVersion"] = program_version
return result
def _isBackupFile(self, candidate):
"""
returns a boolean whether the filename represents a backup of the model
filename
"""
base, extension = os.path.splitext(os.path.basename(self.filename))
candidatebase, candidateext = os.path.splitext(os.path.basename(candidate))
if base not in candidatebase:
return False
if not (
("FCBak" in candidateext)
or ("FCStd" in candidateext and candidateext[-1].isdigit())
):
return False
return True
def addNewVersion(self, filename):
"""
evaluates a filename. Adds a version if it's a backup file to the
document file
"""
if not self._isBackupFile(filename) and filename != os.path.basename(
self.filename
):
return
resource = f"{self.path}/{filename}"
fileInfo = self._getFCFileInfo(resource)
version = {
"created": fileInfo["CreationDate"],
"uniqueName": filename,
"resource": resource,
}
row = len(self.versions)
# Add the new item to the versions list
self.beginInsertRows(QModelIndex(), row, row)
self.versions.insert(0, version)
self.endInsertRows()
def data(self, index, role):
row = index.row()
version = self.versions[row]
if role == Qt.DisplayRole:
return version["created"]
# return version["uniqueName"]
# Additional role for accessing the full filename
if role == Qt.UserRole:
return version["resource"]
return None
class OndselVersionModel(VersionModel):
def __init__(self, model_id, apiClient, fileItem, parent=None):
super().__init__(parent)
self.model_id = model_id
self.apiClient = apiClient
# The version model belongs to a specific fileId
self.fileItem = fileItem
# the version that is on disk
self.onDiskVersionId = None
self.refreshModel(fileItem)
# def sortVersions(self, fileDict):
# """Sort the versions"
# The versions acquired from the API are reversed and the currentVersion
# (the active one is set to be the top one.
# """
# currentVersionId = fileDict['currentVersionId']
# versions = fileDict["versions"][::-1]
# indexCurrentVersionId = [v["_id"] for v in versions].index(currentVersionId)
# versions[indexCurrentVersionId], versions[0] = \
# versions[0], versions[indexCurrentVersionId]
# return versions
def getOnDiskVersionId(self, fileItem):
"""Retrieve the id of a version of a file if it is on disk.
This code checks whether a file on disk is a specific version by
checking the updated times. If a file on disk is indeed a specific
version on the server we return the versionId, otherwise None.
"""
path = fileItem.getPath()
if os.path.isfile(path):
updatedAtDisk = Utils.getFileUpdatedAt(path)
for version in self.versions:
updatedAt, createdAt = VersionModel.getVersionDateTime(version)
if updatedAt == updatedAtDisk or createdAt == updatedAtDisk:
return version["_id"]
return None
def refreshModel(self, fileItem):
# raises an APIClientException
self.clearModel()
model = self.apiClient.getModel(self.model_id)
fileDict = model["file"]
self.beginResetModel()
self.versions = fileDict["versions"][::-1]
self.namesUsers = {
user["_id"]: user["name"] for user in fileDict["relatedUserDetails"]
}
# The version that is active on the server
self.currentVersionId = fileDict["currentVersionId"]
self.onDiskVersionId = self.getOnDiskVersionId(fileItem)
self.endResetModel()
def canBeMadeActive(self):
"""Whether the version on disk can be made active"""
return (
self.onDiskVersionId is not None
and self.onDiskVersionId != self.currentVersionId
)
def getCurrentVersionId(self):
"""Get the current version.
In this context, the current version is the one on disk and if there is
no file on disk, it is the active version on the server.
"""
versionId = self.currentVersionId
if self.onDiskVersionId:
versionId = self.onDiskVersionId
return versionId
def getCurrentIndex(self):
"""Get the index of the current version.
In this context, the current version is the one on disk and if there is
no file on disk, it is the active version on the server.
"""
versionId = self.getCurrentVersionId()
return [v["_id"] for v in self.versions].index(versionId)
def getFileId(self):
"Get the file id of the versions"
return self.fileItem.serverFileDict["_id"]
def data(self, index, role):
row = index.row()
version = self.versions[row]
if role == Qt.DisplayRole:
return (self.convertTime(version["createdAt"] // 1000)) + (
" ✔️" if version["_id"] == self.currentVersionId else ""
)
elif role == Qt.ToolTipRole:
nameUser = self.namesUsers.get(version["userId"])
if nameUser:
nameUser = f" - {nameUser}"
else:
nameUser = ""
return f"{version['message']}{nameUser}"
elif role == Qt.UserRole:
# Additional role for accessing the unique filename and the version Id
return version
return None