Repository navigation
Replace clobber parameter with overwrite #5171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a435c6f
d2a86a8
4d6038d
8d22bc5
171b64d
eec39ca
8d4aa82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,8 +22,8 @@ | |
| _array_to_file, _write_string) | ||
| from ...extern.six import b, string_types | ||
| from ...utils.data import download_file, _is_url | ||
| from ...utils.decorators import classproperty | ||
| from ...utils.exceptions import AstropyUserWarning | ||
| from ...utils.decorators import classproperty, deprecated_renamed_argument | ||
| from ...utils.exceptions import AstropyUserWarning, AstropyDeprecationWarning | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unused import. |
||
|
|
||
|
|
||
| # Maps PyFITS-specific file mode names to the appropriate file modes to use | ||
|
|
@@ -87,7 +87,8 @@ class _File(object): | |
| Represents a FITS file on disk (or in some other file-like object). | ||
| """ | ||
|
|
||
| def __init__(self, fileobj=None, mode=None, memmap=None, clobber=False, | ||
| @deprecated_renamed_argument('clobber', 'overwrite', '1.3') | ||
| def __init__(self, fileobj=None, mode=None, memmap=None, overwrite=False, | ||
| cache=True): | ||
| self.strict_memmap = bool(memmap) | ||
| memmap = True if memmap is None else memmap | ||
|
|
@@ -146,11 +147,11 @@ def __init__(self, fileobj=None, mode=None, memmap=None, clobber=False, | |
|
|
||
| # Initialize the internal self._file object | ||
| if _is_random_access_file_backed(fileobj): | ||
| self._open_fileobj(fileobj, mode, clobber) | ||
| self._open_fileobj(fileobj, mode, overwrite) | ||
| elif isinstance(fileobj, string_types): | ||
| self._open_filename(fileobj, mode, clobber) | ||
| self._open_filename(fileobj, mode, overwrite) | ||
| else: | ||
| self._open_filelike(fileobj, mode, clobber) | ||
| self._open_filelike(fileobj, mode, overwrite) | ||
|
|
||
| self.fileobj_mode = fileobj_mode(self._file) | ||
|
|
||
|
|
@@ -375,8 +376,8 @@ def _maybe_close_mmap(self, refcount_delta=0): | |
| self._mmap.close() | ||
| self._mmap = None | ||
|
|
||
| def _overwrite_existing(self, clobber, fileobj, closed): | ||
| """Overwrite an existing file if ``clobber`` is ``True``, otherwise | ||
| def _overwrite_existing(self, overwrite, fileobj, closed): | ||
| """Overwrite an existing file if ``overwrite`` is ``True``, otherwise | ||
| raise an IOError. The exact behavior of this method depends on the | ||
| _File object state and is only meant for use within the ``_open_*`` | ||
| internal methods. | ||
|
|
@@ -385,7 +386,7 @@ def _overwrite_existing(self, clobber, fileobj, closed): | |
| # The file will be overwritten... | ||
| if ((self.file_like and hasattr(fileobj, 'len') and fileobj.len > 0) or | ||
| (os.path.exists(self.name) and os.path.getsize(self.name) != 0)): | ||
| if clobber: | ||
| if overwrite: | ||
| if self.file_like and hasattr(fileobj, 'truncate'): | ||
| fileobj.truncate(0) | ||
| else: | ||
|
|
@@ -395,14 +396,14 @@ def _overwrite_existing(self, clobber, fileobj, closed): | |
| else: | ||
| raise IOError("File {!r} already exists.".format(self.name)) | ||
|
|
||
| def _open_fileobj(self, fileobj, mode, clobber): | ||
| def _open_fileobj(self, fileobj, mode, overwrite): | ||
| """Open a FITS file from a file object or a GzipFile object.""" | ||
|
|
||
| closed = fileobj_closed(fileobj) | ||
| fmode = fileobj_mode(fileobj) or PYFITS_MODES[mode] | ||
|
|
||
| if mode == 'ostream': | ||
| self._overwrite_existing(clobber, fileobj, closed) | ||
| self._overwrite_existing(overwrite, fileobj, closed) | ||
|
|
||
| if not closed: | ||
| # Although we have a specific mapping in PYFITS_MODES from our | ||
|
|
@@ -429,7 +430,7 @@ def _open_fileobj(self, fileobj, mode, clobber): | |
| # append mode the file pointer is at the end of the file | ||
| self._file.seek(0) | ||
|
|
||
| def _open_filelike(self, fileobj, mode, clobber): | ||
| def _open_filelike(self, fileobj, mode, overwrite): | ||
| """Open a FITS file from a file-like object, i.e. one that has | ||
| read and/or write methods. | ||
| """ | ||
|
|
@@ -455,7 +456,7 @@ def _open_filelike(self, fileobj, mode, clobber): | |
| self.mode = mode = 'ostream' | ||
|
|
||
| if mode == 'ostream': | ||
| self._overwrite_existing(clobber, fileobj, False) | ||
| self._overwrite_existing(overwrite, fileobj, False) | ||
|
|
||
| # Any "writeable" mode requires a write() method on the file object | ||
| if (self.mode in ('update', 'append', 'ostream') and | ||
|
|
@@ -468,11 +469,11 @@ def _open_filelike(self, fileobj, mode, clobber): | |
| raise IOError("File-like object does not have a 'read' " | ||
| "method, required for mode {!r}.".format(self.mode)) | ||
|
|
||
| def _open_filename(self, filename, mode, clobber): | ||
| def _open_filename(self, filename, mode, overwrite): | ||
| """Open a FITS file from a filename string.""" | ||
|
|
||
| if mode == 'ostream': | ||
| self._overwrite_existing(clobber, None, True) | ||
| self._overwrite_existing(overwrite, None, True) | ||
|
|
||
| if os.path.exists(self.name): | ||
| with fileobj_open(self.name, 'rb') as f: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| from ....utils.compat import suppress | ||
| from ....utils.compat.funcsigs import signature, Parameter | ||
| from ....utils.exceptions import AstropyUserWarning | ||
| from ....utils.decorators import deprecated_renamed_argument | ||
|
|
||
|
|
||
| class _Delayed(object): | ||
|
|
@@ -332,10 +333,11 @@ def readfrom(cls, fileobj, checksum=False, ignore_missing_end=False, | |
| fileobj.seek(hdu._data_offset + hdu._data_size, os.SEEK_SET) | ||
| return hdu | ||
|
|
||
| def writeto(self, name, output_verify='exception', clobber=False, | ||
| @deprecated_renamed_argument('clobber', 'overwrite', '1.3') | ||
| def writeto(self, name, output_verify='exception', overwrite=False, | ||
| checksum=False): | ||
| """ | ||
| Write the HDU to a new file. This is a convenience method to | ||
| Write the HDU to a new file. This is a convenience method to | ||
| provide a user easier output interface if only one HDU needs | ||
| to be written to a file. | ||
|
|
||
|
|
@@ -352,8 +354,13 @@ def writeto(self, name, output_verify='exception', clobber=False, | |
| ``"silentfix"`` with ``"+ignore"``, ``+warn``, or ``+exception" | ||
| (e.g. ``"fix+warn"``). See :ref:`verify` for more info. | ||
|
|
||
| clobber : bool | ||
| Overwrite the output file if exists. | ||
| overwrite : bool, optional | ||
| If ``True``, overwrite the output file if it exists. Raises an | ||
| ``OSError`` (``IOError`` for Python 2) if ``False`` and the | ||
| output file exists. Default is ``False``. | ||
|
|
||
| .. versionchanged:: 1.3 | ||
| ``overwrite`` replaces the deprecated ``clobber`` argument. | ||
|
|
||
| checksum : bool | ||
| When `True` adds both ``DATASUM`` and ``CHECKSUM`` cards | ||
|
|
@@ -363,7 +370,7 @@ def writeto(self, name, output_verify='exception', clobber=False, | |
| from .hdulist import HDUList | ||
|
|
||
| hdulist = HDUList([self]) | ||
| hdulist.writeto(name, output_verify, clobber=clobber, | ||
| hdulist.writeto(name, output_verify, overwrite=overwrite, | ||
| checksum=checksum) | ||
|
|
||
| @classmethod | ||
|
|
@@ -1565,19 +1572,23 @@ def match_header(cls, header): | |
|
|
||
| raise NotImplementedError | ||
|
|
||
| def writeto(self, name, output_verify='exception', clobber=False, | ||
| @deprecated_renamed_argument('clobber', 'overwrite', '1.3') | ||
| def writeto(self, name, output_verify='exception', overwrite=False, | ||
| checksum=False): | ||
| """ | ||
| Works similarly to the normal writeto(), but prepends a default | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is adressed below. |
||
| `PrimaryHDU` are required by extension HDUs (which cannot stand on | ||
| their own). | ||
|
|
||
| .. versionchanged:: 1.3 | ||
| ``overwrite`` replaces the deprecated ``clobber`` argument. | ||
| """ | ||
|
|
||
| from .hdulist import HDUList | ||
| from .image import PrimaryHDU | ||
|
|
||
| hdulist = HDUList([PrimaryHDU(), self]) | ||
| hdulist.writeto(name, output_verify, clobber=clobber, | ||
| hdulist.writeto(name, output_verify, overwrite=overwrite, | ||
| checksum=checksum) | ||
|
|
||
| def _verify(self, option='warn'): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sphinx has a
versionaddedanddeprecated-directive. I haven't seen it regularly used but I think it could make sense here.For
clobberthis would be something likeand for overwrite:
.. versionadded:: 1.3