Commit 45d38bfff7c7e7d2dc2e4b22002273ec0bf11e93

Authored by tfmoraes
1 parent 4bef21e3

ENH: reduced the number of temp files needed to save an InVesalius 3 project wha…

…t make it a little faster
Showing 1 changed file with 26 additions and 14 deletions   Show diff stats
invesalius/project.py
@@ -205,6 +205,7 @@ class Project(object): @@ -205,6 +205,7 @@ class Project(object):
205 def SavePlistProject(self, dir_, filename): 205 def SavePlistProject(self, dir_, filename):
206 dir_temp = tempfile.mkdtemp() 206 dir_temp = tempfile.mkdtemp()
207 filename_tmp = os.path.join(dir_temp, 'matrix.dat') 207 filename_tmp = os.path.join(dir_temp, 'matrix.dat')
  208 + filelist = {}
208 209
209 project = { 210 project = {
210 # Format info 211 # Format info
@@ -228,40 +229,51 @@ class Project(object): @@ -228,40 +229,51 @@ class Project(object):
228 'dtype': self.matrix_dtype, 229 'dtype': self.matrix_dtype,
229 } 230 }
230 project['matrix'] = matrix 231 project['matrix'] = matrix
231 - shutil.copyfile(self.matrix_filename, filename_tmp) 232 + filelist[self.matrix_filename] = 'matrix.dat'
  233 + #shutil.copyfile(self.matrix_filename, filename_tmp)
232 234
233 # Saving the masks 235 # Saving the masks
234 masks = {} 236 masks = {}
235 for index in self.mask_dict: 237 for index in self.mask_dict:
236 - masks[str(index)] = self.mask_dict[index].SavePlist(dir_temp) 238 + masks[str(index)] = self.mask_dict[index].SavePlist(dir_temp,
  239 + filelist)
237 project['masks'] = masks 240 project['masks'] = masks
238 241
239 # Saving the surfaces 242 # Saving the surfaces
240 surfaces = {} 243 surfaces = {}
241 for index in self.surface_dict: 244 for index in self.surface_dict:
242 - surfaces[str(index)] = self.surface_dict[index].SavePlist(dir_temp) 245 + surfaces[str(index)] = self.surface_dict[index].SavePlist(dir_temp,
  246 + filelist)
243 project['surfaces'] = surfaces 247 project['surfaces'] = surfaces
244 248
245 # Saving the measurements 249 # Saving the measurements
246 measurements = self.GetMeasuresDict() 250 measurements = self.GetMeasuresDict()
247 measurements_filename = 'measurements.plist' 251 measurements_filename = 'measurements.plist'
  252 + temp_mplist = tempfile.mktemp()
248 plistlib.writePlist(measurements, 253 plistlib.writePlist(measurements,
249 - os.path.join(dir_temp, measurements_filename)) 254 + temp_mplist)
  255 + filelist[temp_mplist] = measurements_filename
250 project['measurements'] = measurements_filename 256 project['measurements'] = measurements_filename
251 257
252 # Saving the annotations (empty in this version) 258 # Saving the annotations (empty in this version)
253 project['annotations'] = {} 259 project['annotations'] = {}
254 260
255 # Saving the main plist 261 # Saving the main plist
256 - plistlib.writePlist(project, os.path.join(dir_temp, 'main.plist')) 262 + temp_plist = tempfile.mktemp()
  263 + plistlib.writePlist(project, temp_plist)
  264 + filelist[temp_plist] = 'main.plist'
257 265
258 # Compressing and generating the .inv3 file 266 # Compressing and generating the .inv3 file
259 path = os.path.join(dir_,filename) 267 path = os.path.join(dir_,filename)
260 - Compress(dir_temp, path) 268 + Compress(dir_temp, path, filelist)
261 269
262 # Removing the temp folder. 270 # Removing the temp folder.
263 shutil.rmtree(dir_temp) 271 shutil.rmtree(dir_temp)
264 272
  273 + for f in filelist:
  274 + if filelist[f].endswith('.plist'):
  275 + print f
  276 + os.remove(f)
265 277
266 def OpenPlistProject(self, filename): 278 def OpenPlistProject(self, filename):
267 import data.measures as ms 279 import data.measures as ms
@@ -322,18 +334,18 @@ class Project(object): @@ -322,18 +334,18 @@ class Project(object):
322 measure.Load(measurements[index]) 334 measure.Load(measurements[index])
323 self.measurement_dict[int(index)] = measure 335 self.measurement_dict[int(index)] = measure
324 336
325 -def Compress(folder, filename): 337 +def Compress(folder, filename, filelist):
326 tmpdir, tmpdir_ = os.path.split(folder) 338 tmpdir, tmpdir_ = os.path.split(folder)
327 current_dir = os.path.abspath(".") 339 current_dir = os.path.abspath(".")
328 - os.chdir(tmpdir)  
329 - file_list = glob.glob(os.path.join(tmpdir_,"*")) 340 + #os.chdir(tmpdir)
  341 + #file_list = glob.glob(os.path.join(tmpdir_,"*"))
330 tar_filename = tmpdir_ + ".inv3" 342 tar_filename = tmpdir_ + ".inv3"
331 - tar = tarfile.open(tar_filename.encode(wx.GetDefaultPyEncoding()), "w:gz")  
332 - for name in file_list:  
333 - tar.add(name) 343 + tar = tarfile.open(filename.encode(wx.GetDefaultPyEncoding()), "w:gz")
  344 + for name in filelist:
  345 + tar.add(name, arcname=os.path.join(tmpdir_, filelist[name]))
334 tar.close() 346 tar.close()
335 - shutil.move(tmpdir_+ ".inv3", filename)  
336 - os.chdir(current_dir) 347 + #shutil.move(tmpdir_+ ".inv3", filename)
  348 + #os.chdir(current_dir)
337 349
338 def Extract(filename, folder): 350 def Extract(filename, folder):
339 tar = tarfile.open(filename, "r:gz") 351 tar = tarfile.open(filename, "r:gz")