Commits

Kazuhiko Shimada authored bbb7f92a3f6
CAS-13520: added EraseableFolderRegister class.

refs #2274

casatasks/src/private/task_imbaseline.py

Modified
44 44 self.has_file = True
45 45
46 46 @abstractmethod
47 47 def erase(self) -> None:
48 48 raise RuntimeError('Not implemented')
49 49
50 50
51 51 class EraseableFolder(AbstractFolder):
52 52 """Image/MeasurementSet file path class. The file path is permitted to erase."""
53 53
54 + def __init__(self, file: str=None) -> None:
55 + super().__init__(file)
56 + eraseable_folder_register.register(self)
57 +
54 58 def erase(self, dry_run: bool=True) -> None:
55 59 if self.has_file:
56 60 if dry_run:
57 61 casalog.post(f'[DRY RUN] erase file: {self.path}', 'DEBUG2')
58 62 else:
59 63 casalog.post(f'erase file: {self.path}', 'DEBUG2')
60 64 if os.path.exists(self.path):
61 65 shutil.rmtree(self.path)
62 66 if not os.path.exists(self.path):
63 67 self.has_file = False
64 68 else:
65 69 casalog.post(f'not found the file to erase: {self.path}', 'WARN')
66 70
67 71
68 72 class UnerasableFolder(AbstractFolder):
69 73 """Image/MeasurementSet file path class. The file path is NOT permitted to erase."""
70 74
71 75 def erase(self, dry_run: bool=True) -> None:
72 76 casalog.post(f'un-erase file: {self.path}', 'DEBUG2')
73 77
74 78
79 +class EraseableFolderRegister():
80 + """Class of the register of folders that need to be erased."""
81 +
82 + _register = []
83 +
84 + def register(self, folder: EraseableFolder):
85 + if isinstance(folder, EraseableFolder):
86 + self._register.append(folder)
87 + else:
88 + raise ValueError('Irregal folder would be appended', 'SEVERE')
89 +
90 + def clear(self, dry_run: bool=True):
91 + for folder in self._register:
92 + if not folder.has_file:
93 + raise RuntimeError('Invalid code execution state', 'SEVERE')
94 + elif not os.path.exists(folder.path):
95 + raise RuntimeError(f'File not found: {folder.path}', 'SEVERE')
96 + else:
97 + folder.erase(dry_run)
98 +
99 +
100 +eraseable_folder_register = EraseableFolderRegister()
101 +
102 +
75 103 class AbstractFileStack:
76 104 """CasaImage/MeasurementSet file path stack to be processed by tasks in imbaseline.
77 105
78 106 The paths of CasaImage or MeasurementSet are wrapped by AbstractFolder class.
79 107 Implementation classes of AbstractFileStack are EraseableFolder/UneraseableFolder, the EraseableFolder class erases the path
80 108 holden by a property 'path' when execute cleaning process, and the UneraseableFolder class doesn't erase it.
81 109 If this class is used to stack a path of CasaImage, the bottom of it must be the input image(an argument 'imagename').
82 110 """
83 111
84 112 def __init__(self, top: AbstractFolder=None, max_height=None) -> None:
128 156 """Return a pointer of the bottom of the stack."""
129 157 if len(self.stack) > 0:
130 158 picked = self.stack[0]
131 159 casalog.post(f'pick from bottom of the stack: {picked.path}', 'DEBUG2')
132 160 return self.stack[0]
133 161 else:
134 162 raise RuntimeError('the stack has not have enough stuff')
135 163
136 164 def clear(self, dry_run: bool=True) -> None:
137 165 """Do erase method of all of the stack and clear the stack."""
138 - for file in self.stack:
139 - file.erase(dry_run=dry_run)
166 + # for file in self.stack:
167 + # file.erase(dry_run=dry_run)
140 168 self.stack.clear()
141 169
142 170 def height(self) -> int:
143 171 return len(self.stack)
144 172
145 173
146 174 class CasaImageStack(AbstractFileStack):
147 175 """FileStack for CasaImage."""
148 176
149 177 def __init__(self, top: AbstractFolder=None) -> None:
345 373 base_image = image_stack.bottom().path
346 374 output_image = os.path.basename(base_image) + '.cont'
347 375 __copy_image_file(base_image, output_image)
348 376
349 377 subtract_image = image_stack.peak().path
350 378 __subtract_image(output_image, subtract_image)
351 379
352 380
353 381 def do_post_processing(outfile) -> None:
354 382 """Execute some post-processes of imbaseline."""
383 + eraseable_folder_register.clear(dry_run=False)
355 384 __write_image_history(outfile)
356 385
357 386
358 387 def __subtract_image(operand_a: str=None, operand_b: str=None) -> None:
359 388 """Subtract image chunk."""
360 389 image_array = None
361 390 with tool_manager(operand_b, image) as ia:
362 391 image_array = ia.getchunk()
363 392
364 393 with tool_manager(operand_a, image) as ia:

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut