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 205 def SavePlistProject(self, dir_, filename):
206 206 dir_temp = tempfile.mkdtemp()
207 207 filename_tmp = os.path.join(dir_temp, 'matrix.dat')
  208 + filelist = {}
208 209  
209 210 project = {
210 211 # Format info
... ... @@ -228,40 +229,51 @@ class Project(object):
228 229 'dtype': self.matrix_dtype,
229 230 }
230 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 235 # Saving the masks
234 236 masks = {}
235 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 240 project['masks'] = masks
238 241  
239 242 # Saving the surfaces
240 243 surfaces = {}
241 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 247 project['surfaces'] = surfaces
244 248  
245 249 # Saving the measurements
246 250 measurements = self.GetMeasuresDict()
247 251 measurements_filename = 'measurements.plist'
  252 + temp_mplist = tempfile.mktemp()
248 253 plistlib.writePlist(measurements,
249   - os.path.join(dir_temp, measurements_filename))
  254 + temp_mplist)
  255 + filelist[temp_mplist] = measurements_filename
250 256 project['measurements'] = measurements_filename
251 257  
252 258 # Saving the annotations (empty in this version)
253 259 project['annotations'] = {}
254 260  
255 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 266 # Compressing and generating the .inv3 file
259 267 path = os.path.join(dir_,filename)
260   - Compress(dir_temp, path)
  268 + Compress(dir_temp, path, filelist)
261 269  
262 270 # Removing the temp folder.
263 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 278 def OpenPlistProject(self, filename):
267 279 import data.measures as ms
... ... @@ -322,18 +334,18 @@ class Project(object):
322 334 measure.Load(measurements[index])
323 335 self.measurement_dict[int(index)] = measure
324 336  
325   -def Compress(folder, filename):
  337 +def Compress(folder, filename, filelist):
326 338 tmpdir, tmpdir_ = os.path.split(folder)
327 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 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 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 350 def Extract(filename, folder):
339 351 tar = tarfile.open(filename, "r:gz")
... ...