3434from .volumeutils import array_from_file , apply_read_scaling
3535from .fileslice import fileslice
3636from .keywordonly import kw_only_meth
37- from .openers import ImageOpener , HAVE_INDEXED_GZIP
37+ from . import openers
3838
3939
4040"""This flag controls whether a new file handle is created every time an image
4343``True``, ``False``, or ``'auto'``.
4444
4545If ``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``.
5050
5151If this is set to any other value, attempts to create an ``ArrayProxy`` without
5252specifying the ``keep_file_open`` flag will result in a ``ValueError`` being
5353raised.
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.
5458"""
5559KEEP_FILE_OPEN_DEFAULT = False
5660
@@ -187,9 +191,9 @@ def _should_keep_file_open(self, file_like, keep_file_open):
187191
188192 - If ``file_like`` is a file(-like) object, ``False`` is returned.
189193 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 .
193197
194198 Parameters
195199 ----------
@@ -203,23 +207,21 @@ def _should_keep_file_open(self, file_like, keep_file_open):
203207 -------
204208
205209 The value of ``keep_file_open`` that will be used by this
206- ``ArrayProxy``.
210+ ``ArrayProxy``, and passed through to ``ImageOpener`` instances .
207211 """
208212 if keep_file_open is None :
209213 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 ):
214215 raise ValueError ('keep_file_open should be one of {None, '
215216 '\' auto\' , True, False}' )
216-
217217 # file_like is a handle - keep_file_open is irrelevant
218218 if hasattr (file_like , 'read' ) and hasattr (file_like , 'seek' ):
219219 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
223225
224226 @property
225227 @deprecate_with_version ('ArrayProxy.header deprecated' , '2.2' , '3.0' )
@@ -265,10 +267,11 @@ def _get_fileobj(self):
265267 """
266268 if self ._keep_file_open :
267269 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 )
269272 yield self ._opener
270273 else :
271- with ImageOpener (self .file_like , keep_open = False ) as opener :
274+ with openers . ImageOpener (self .file_like ) as opener :
272275 yield opener
273276
274277 def get_unscaled (self ):
0 commit comments