|
1 | 1 | """Miscellaneous definitions.""" |
2 | 2 |
|
3 | 3 | from inspect import getdoc |
4 | | -import pathlib |
5 | | -import shutil |
6 | | -import tempfile |
7 | 4 |
|
8 | 5 | import matplotlib.pyplot as plt |
9 | 6 |
|
@@ -152,82 +149,3 @@ def __get__(self, instance, _): |
152 | 149 | def __set__(self, instance, _): |
153 | 150 | raise AttributeError( |
154 | 151 | f'Cannot set {self._name} property of {instance!r}') |
155 | | - |
156 | | - |
157 | | -class InchoateFiles: |
158 | | - """Context manager handling files whose names are not known yet. |
159 | | -
|
160 | | - Example: |
161 | | - InchoateFiles is used here to manage three files:: |
162 | | -
|
163 | | - with InchoateFiles(3) as incho: |
164 | | - # for convenience, incho[x] is the same as incho.fids[x] |
165 | | - incho[0].write('First file') |
166 | | - incho[1].write('Second file') |
167 | | - incho[2].write('Third file') |
168 | | -
|
169 | | - # the three files will be named 'tata', 'titi' and 'toto' |
170 | | - incho.fnames = ['tata', 'titi', 'toto'] |
171 | | -
|
172 | | - Args: |
173 | | - nfiles (int): number of files. Defaults to 1. |
174 | | - tmp_prefix (str): prefix name of temporary files. Use this |
175 | | - parameter if you want to easily track down the temporary files |
176 | | - created by the manager. |
177 | | - """ |
178 | | - |
179 | | - def __init__(self, nfiles=1, tmp_prefix=None): |
180 | | - self._fnames = [f'inchoate{i}' for i in range(nfiles)] |
181 | | - self._tmpprefix = tmp_prefix |
182 | | - self._fids = [] |
183 | | - |
184 | | - @property |
185 | | - def fids(self): |
186 | | - """List of files id. |
187 | | -
|
188 | | - Use this to perform operations on files when the context manager is |
189 | | - used. :meth:`InchoateFiles.__getitem__` is implemented in order to |
190 | | - provide direct access to this property content (``self[x]`` is the |
191 | | - same as ``self.fids[x]``). |
192 | | - """ |
193 | | - return self._fids |
194 | | - |
195 | | - @property |
196 | | - def fnames(self): |
197 | | - """List of filenames. |
198 | | -
|
199 | | - Set this to the list of final filenames before exiting the context |
200 | | - manager. If this list is not set by the user, the produced files will |
201 | | - be named ``'inchoateN'`` with ``N`` the index of the file. If the list |
202 | | - of names you set is too long, it will be truncated. If it is too short, |
203 | | - extra files will be named ``'inchoateN'``. |
204 | | - """ |
205 | | - return self._fnames |
206 | | - |
207 | | - @fnames.setter |
208 | | - def fnames(self, names): |
209 | | - """Ensure constant size of fnames.""" |
210 | | - names = list(names[:len(self._fnames)]) |
211 | | - self._fnames = names + self._fnames[len(names):] |
212 | | - |
213 | | - def __getitem__(self, idx): |
214 | | - return self._fids[idx] |
215 | | - |
216 | | - def __enter__(self): |
217 | | - """Create temporary files.""" |
218 | | - for fname in self.fnames: |
219 | | - pfx = fname if self._tmpprefix is None else self._tmpprefix |
220 | | - self._fids.append( |
221 | | - tempfile.NamedTemporaryFile( |
222 | | - mode='w', prefix=pfx, delete=False)) |
223 | | - return self |
224 | | - |
225 | | - def __exit__(self, *exc_info): |
226 | | - """Give temporary files their final names.""" |
227 | | - for tmp in self._fids: |
228 | | - tmp.close() |
229 | | - if exc_info[0] is None: |
230 | | - for fname, tmp in zip(self.fnames, self._fids): |
231 | | - shutil.copyfile(tmp.name, fname) |
232 | | - for tmp in self._fids: |
233 | | - pathlib.Path(tmp.name).unlink() |
0 commit comments