|
8 | 8 |
|
9 | 9 |
|
10 | 10 | 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) |
12 | 13 | from zarr.hierarchy import open_group, group as _create_group, Group
|
13 | 14 | from zarr.storage import contains_array, contains_group
|
14 | 15 | from zarr.errors import err_path_not_found, CopyError
|
15 | 16 | from zarr.util import normalize_storage_path, TreeViewer, buffer_size
|
| 17 | +from zarr.compat import PY2, text_type |
16 | 18 |
|
17 | 19 |
|
18 | 20 | # noinspection PyShadowingBuiltins
|
@@ -353,45 +355,6 @@ def load(store):
|
353 | 355 | return LazyLoader(grp)
|
354 | 356 |
|
355 | 357 |
|
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 |
| - |
395 | 358 | def tree(grp, expand=False, level=None):
|
396 | 359 | """Provide a ``print``-able display of the hierarchy. This function is provided
|
397 | 360 | 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):
|
443 | 406 | return TreeViewer(grp, expand=expand, level=level)
|
444 | 407 |
|
445 | 408 |
|
| 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 | + |
446 | 451 | def _log_copy_summary(log, dry_run, n_copied, n_skipped, n_bytes_copied):
|
447 | 452 | # log a final message with a summary of what happened
|
448 | 453 | if dry_run:
|
|
0 commit comments