Skip to content

Commit 7d3b38c

Browse files
committed
windows and py2 compat
1 parent fd1e5df commit 7d3b38c

File tree

2 files changed

+51
-44
lines changed

2 files changed

+51
-44
lines changed

zarr/convenience.py

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99

1010
from zarr.core import Array
11-
from zarr.creation import open_array, normalize_store_arg, array as _create_array
11+
from zarr.creation import (open_array, normalize_store_arg,
12+
array as _create_array)
1213
from zarr.hierarchy import open_group, group as _create_group, Group
1314
from zarr.storage import contains_array, contains_group
1415
from zarr.errors import err_path_not_found, CopyError
1516
from zarr.util import normalize_storage_path, TreeViewer, buffer_size
17+
from zarr.compat import PY2, text_type
1618

1719

1820
# noinspection PyShadowingBuiltins
@@ -353,45 +355,6 @@ def load(store):
353355
return LazyLoader(grp)
354356

355357

356-
class _LogWriter(object):
357-
358-
def __init__(self, log):
359-
self.log_func = None
360-
self.log_file = None
361-
self.needs_closing = False
362-
if log is None:
363-
# don't do any logging
364-
pass
365-
elif callable(log):
366-
self.log_func = log
367-
elif isinstance(log, str):
368-
self.log_file = io.open(log, mode='w')
369-
self.needs_closing = True
370-
else:
371-
if not hasattr(log, 'write'):
372-
raise TypeError('log must be a callable function, file path or '
373-
'file-like object, found %r' % log)
374-
self.log_file = log
375-
self.needs_closing = False
376-
377-
def __enter__(self):
378-
return self
379-
380-
def __exit__(self, *args):
381-
if self.log_file is not None and self.needs_closing:
382-
self.log_file.close()
383-
384-
def __call__(self, *args, **kwargs):
385-
if self.log_file is not None:
386-
kwargs['file'] = self.log_file
387-
print(*args, **kwargs)
388-
if hasattr(self.log_file, 'flush'):
389-
# get immediate feedback
390-
self.log_file.flush()
391-
elif self.log_func is not None:
392-
self.log_func(*args, **kwargs)
393-
394-
395358
def tree(grp, expand=False, level=None):
396359
"""Provide a ``print``-able display of the hierarchy. This function is provided
397360
mainly as a convenience for obtaining a tree view of an h5py group - zarr groups
@@ -443,6 +406,48 @@ def tree(grp, expand=False, level=None):
443406
return TreeViewer(grp, expand=expand, level=level)
444407

445408

409+
class _LogWriter(object):
410+
411+
def __init__(self, log):
412+
self.log_func = None
413+
self.log_file = None
414+
self.needs_closing = False
415+
if log is None:
416+
# don't do any logging
417+
pass
418+
elif callable(log):
419+
self.log_func = log
420+
elif isinstance(log, str):
421+
self.log_file = io.open(log, mode='w')
422+
self.needs_closing = True
423+
else:
424+
if not hasattr(log, 'write'):
425+
raise TypeError('log must be a callable function, file path or '
426+
'file-like object, found %r' % log)
427+
self.log_file = log
428+
self.needs_closing = False
429+
430+
def __enter__(self):
431+
return self
432+
433+
def __exit__(self, *args):
434+
if self.log_file is not None and self.needs_closing:
435+
self.log_file.close()
436+
437+
def __call__(self, *args, **kwargs):
438+
if self.log_file is not None:
439+
kwargs['file'] = self.log_file
440+
if PY2: # pragma: py3 no cover
441+
# expect file opened in text mode, need to adapt message
442+
args = [text_type(a) for a in args]
443+
print(*args, **kwargs)
444+
if hasattr(self.log_file, 'flush'):
445+
# get immediate feedback
446+
self.log_file.flush()
447+
elif self.log_func is not None:
448+
self.log_func(*args, **kwargs)
449+
450+
446451
def _log_copy_summary(log, dry_run, n_copied, n_skipped, n_bytes_copied):
447452
# log a final message with a summary of what happened
448453
if dry_run:

zarr/tests/test_convenience.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ def test_if_exists(self):
218218
copy_store(source, dest, if_exists='foobar')
219219

220220

221-
def check_copied_array(original, copied, without_attrs=False, expect_props=None):
221+
def check_copied_array(original, copied, without_attrs=False,
222+
expect_props=None):
222223

223224
# setup
224225
source_h5py = original.__module__.startswith('h5py.')
@@ -544,11 +545,12 @@ def test_logging(self):
544545
copy(source['foo'], dest, dry_run=True, log=print)
545546

546547
# file name
547-
with tempfile.NamedTemporaryFile() as f:
548-
copy(source['foo'], dest, dry_run=True, log=f.name)
548+
fn = tempfile.mktemp()
549+
atexit.register(os.remove, fn)
550+
copy(source['foo'], dest, dry_run=True, log=fn)
549551

550552
# file
551-
with tempfile.NamedTemporaryFile(mode='w') as f:
553+
with tempfile.TemporaryFile(mode='w') as f:
552554
copy(source['foo'], dest, dry_run=True, log=f)
553555

554556
# bad option

0 commit comments

Comments
 (0)