Skip to content

Commit 3745b2e

Browse files
author
Alexey Pechnikov
committed
Code cleanup to simplify internal files naming
1 parent 3ec15b8 commit 3745b2e

File tree

6 files changed

+62
-342
lines changed

6 files changed

+62
-342
lines changed

pygmtsar/pygmtsar/PRM.py

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -403,21 +403,12 @@ def to_file(self, prm):
403403
self.filename = prm
404404
return self
405405

406-
#def update(self):
407-
# if self.filename is None:
408-
# raise Exception('PRM is not created from file, use to_file() method instead')
409-
# return self._to_io(self.filename)
410-
def update(self, name=None, safe=False, debug=False):
406+
def update(self, debug=False):
411407
"""
412-
Save PRM file to disk and rename all 3 linked files together: input, LED, SLC if "name" defined.
413-
If safe=True, save old (small) PRM and LED files and move only (large) SLC file.
408+
Save PRM file to disk.
414409
415410
Parameters
416411
----------
417-
name : str, optional
418-
The new name for the PRM file. Default is None.
419-
safe : bool, optional
420-
Whether to use safe mode. Default is False.
421412
debug : bool, optional
422413
Whether to enable debug mode. Default is False.
423414
@@ -426,74 +417,11 @@ def update(self, name=None, safe=False, debug=False):
426417
PRM
427418
The updated PRM object.
428419
"""
429-
import shutil
430-
import os
431-
432420
if self.filename is None:
433421
raise Exception('PRM is not created from file, use to_file() method instead')
434422

435423
if debug:
436-
print ('Debug mode: only print expected operations but do not perform the actual job')
437-
438-
# rename linked files
439-
if name is not None:
440-
# define current files directory
441-
dirname0 = os.path.dirname(self.filename)
442-
# define new files basename
443-
basename = os.path.splitext(name)[0]
444-
shortname = os.path.split(basename)[-1]
445-
446-
# rename PRM file
447-
if not safe:
448-
print (f'Remove old PRM file {self.filename} and save new one {name}')
449-
if not debug:
450-
os.remove(self.filename)
451-
#else:
452-
# print (f'Safe mode: remain old PRM file {self.filename} and save new one {name}')
453-
# will be saved later
454-
self.filename = name
455-
456-
input_file0 = os.path.join(dirname0, os.path.split(self.get('input_file'))[-1])
457-
input_ext = os.path.splitext(input_file0)[1]
458-
input_file = basename + input_ext
459-
#print (f'PRM change input_file attribute {input_file0} -> {input_file}')
460-
self.set(input_file = f'{shortname}{input_ext}')
461-
if os.path.isfile(input_file0) and not input_file == input_file0:
462-
if not safe:
463-
#print (f'Rename input_file {input_file0} -> {input_file}')
464-
if not debug:
465-
os.rename(input_file0, input_file)
466-
else:
467-
#print (f'Safe mode: copy input_file {input_file0} -> {input_file}')
468-
if not debug:
469-
shutil.copy2(input_file0, input_file, follow_symlinks=True)
470-
471-
SLC_file0 = os.path.join(dirname0, os.path.split(self.get('SLC_file'))[-1])
472-
SLC_file = basename + '.SLC'
473-
#print (f'PRM change SLC_file attribute {SLC_file0} -> {SLC_file}')
474-
self.set(SLC_file = f'{shortname}.SLC')
475-
if os.path.isfile(SLC_file0) and not SLC_file == SLC_file0:
476-
#print (f'Rename SLC_file {SLC_file0} -> {SLC_file}')
477-
if not debug:
478-
os.rename(SLC_file0, SLC_file)
479-
480-
led_file0 = os.path.join(dirname0, os.path.split(self.get('led_file'))[-1])
481-
led_file = basename + '.LED'
482-
#print (f'PRM change LED_file attribute {led_file0} -> {led_file}')
483-
self.set(led_file = f'{shortname}.LED')
484-
if os.path.isfile(led_file0) and not led_file == led_file0:
485-
if not safe:
486-
#print (f'Rename LED_file {led_file0} -> {led_file}')
487-
if not debug:
488-
os.rename(led_file0, led_file)
489-
else:
490-
#print (f'Safe mode: copy LED_file {led_file0} -> {led_file}')
491-
if not debug:
492-
shutil.copy2(led_file0, led_file)
493-
494-
if debug:
495-
return self
496-
#.sel('input_file','SLC_file','led_file')
424+
print ('DEBUG:', self)
497425

498426
return self.to_file(self.filename)
499427

0 commit comments

Comments
 (0)