34
34
from .volumeutils import array_from_file , apply_read_scaling
35
35
from .fileslice import fileslice
36
36
from .keywordonly import kw_only_meth
37
- from .openers import ImageOpener , HAVE_INDEXED_GZIP
37
+ from . import openers
38
38
39
39
40
40
"""This flag controls whether a new file handle is created every time an image
43
43
``True``, ``False``, or ``'auto'``.
44
44
45
45
If ``True``, a single file handle is created and used. If ``False``, a new
46
- file handle is created every time the image is accessed. If ``'auto'``, and
47
- the optional ``indexed_gzip`` dependency is present, a single file handle is
48
- created and persisted. If ``indexed_gzip`` is not available, behaviour is the
49
- same as if ``keep_file_open is False``.
46
+ file handle is created every time the image is accessed. For gzip files, if
47
+ ``'auto'``, and the optional ``indexed_gzip`` dependency is present, a single
48
+ file handle is created and persisted. If ``indexed_gzip`` is not available,
49
+ behaviour is the same as if ``keep_file_open is False``.
50
50
51
51
If this is set to any other value, attempts to create an ``ArrayProxy`` without
52
52
specifying the ``keep_file_open`` flag will result in a ``ValueError`` being
53
53
raised.
54
+
55
+ .. warning:: Setting this flag to a value of ``'auto'`` will become deprecated
56
+ behaviour in version 2.4.0. Support for ``'auto'`` will be removed
57
+ in version 3.0.0.
54
58
"""
55
59
KEEP_FILE_OPEN_DEFAULT = False
56
60
@@ -187,9 +191,9 @@ def _should_keep_file_open(self, file_like, keep_file_open):
187
191
188
192
- If ``file_like`` is a file(-like) object, ``False`` is returned.
189
193
Otherwise, ``file_like`` is assumed to be a file name.
190
- - if ``file_like `` ends with ``'gz' ``, and the ``indexed_gzip``
191
- library is available, ``True `` is returned.
192
- - Otherwise, ``False `` is returned.
194
+ - If ``keep_file_open `` is ``auto ``, and ``indexed_gzip`` is
195
+ not available, ``False `` is returned.
196
+ - Otherwise, the value of ``keep_file_open `` is returned unchanged .
193
197
194
198
Parameters
195
199
----------
@@ -203,23 +207,21 @@ def _should_keep_file_open(self, file_like, keep_file_open):
203
207
-------
204
208
205
209
The value of ``keep_file_open`` that will be used by this
206
- ``ArrayProxy``.
210
+ ``ArrayProxy``, and passed through to ``ImageOpener`` instances .
207
211
"""
208
212
if keep_file_open is None :
209
213
keep_file_open = KEEP_FILE_OPEN_DEFAULT
210
- # if keep_file_open is True/False, we do what the user wants us to do
211
- if isinstance (keep_file_open , bool ):
212
- return keep_file_open
213
- if keep_file_open != 'auto' :
214
+ if keep_file_open not in ('auto' , True , False ):
214
215
raise ValueError ('keep_file_open should be one of {None, '
215
216
'\' auto\' , True, False}' )
216
-
217
217
# file_like is a handle - keep_file_open is irrelevant
218
218
if hasattr (file_like , 'read' ) and hasattr (file_like , 'seek' ):
219
219
return False
220
- # Otherwise, if file_like is gzipped, and we have_indexed_gzip, we set
221
- # keep_file_open to True, else we set it to False
222
- return HAVE_INDEXED_GZIP and file_like .endswith ('gz' )
220
+ # don't have indexed_gzip - auto -> False
221
+ if keep_file_open == 'auto' and not (openers .HAVE_INDEXED_GZIP and
222
+ file_like .endswith ('.gz' )):
223
+ return False
224
+ return keep_file_open
223
225
224
226
@property
225
227
@deprecate_with_version ('ArrayProxy.header deprecated' , '2.2' , '3.0' )
@@ -265,10 +267,11 @@ def _get_fileobj(self):
265
267
"""
266
268
if self ._keep_file_open :
267
269
if not hasattr (self , '_opener' ):
268
- self ._opener = ImageOpener (self .file_like , keep_open = True )
270
+ self ._opener = openers .ImageOpener (
271
+ self .file_like , keep_open = self ._keep_file_open )
269
272
yield self ._opener
270
273
else :
271
- with ImageOpener (self .file_like , keep_open = False ) as opener :
274
+ with openers . ImageOpener (self .file_like ) as opener :
272
275
yield opener
273
276
274
277
def get_unscaled (self ):
0 commit comments