Skip to content

Commit abfc3e1

Browse files
committed
add _maybe_copy and _should_copy
to `notebook_compat._compat.nbextensions`, as the logger kwarg was added after notebook 4.1
1 parent dbc9173 commit abfc3e1

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ Common utilities for jupyter-contrib projects. Includes:
1616
Changes
1717
=======
1818

19+
0.3.0
20+
-----
21+
22+
* Add `_maybe_copy` and `_should_copy` to nbextensions private API, since the
23+
`logger` keyword arg was only added after notebook 4.1
24+
1925
0.2.0
2026
-----
2127

src/jupyter_contrib_core/notebook_compat/_compat/nbextensions.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
jupyter_config_dir, jupyter_data_dir,
3434
)
3535
from notebook.nbextensions import (
36-
ArgumentConflict, __version__, _maybe_copy, _safe_is_tarfile,
36+
ArgumentConflict, __version__, _safe_is_tarfile,
3737
)
3838
from tornado.log import LogFormatter
3939
from traitlets import Bool
@@ -413,6 +413,29 @@ def _log_format_default(self):
413413
# -----------------------------------------------------------------------------
414414

415415

416+
def _should_copy(src, dest, logger=None):
417+
"""Return whether a file should be copied."""
418+
if not os.path.exists(dest):
419+
return True
420+
if os.stat(src).st_mtime - os.stat(dest).st_mtime > 1e-6:
421+
# we add a fudge factor to work around a bug in python 2.x
422+
# that was fixed in python 3.x: http://bugs.python.org/issue12904
423+
if logger:
424+
logger.warn("Out of date: %s" % dest)
425+
return True
426+
if logger:
427+
logger.info("Up to date: %s" % dest)
428+
return False
429+
430+
431+
def _maybe_copy(src, dest, logger=None):
432+
"""Copy a file if it needs updating."""
433+
if _should_copy(src, dest, logger=logger):
434+
if logger:
435+
logger.info("Copying: %s -> %s" % (src, dest))
436+
shutil.copy2(src, dest)
437+
438+
416439
def _get_nbextension_dir(user=False, sys_prefix=False, prefix=None,
417440
nbextensions_dir=None):
418441
"""Return the nbextension directory specified."""

0 commit comments

Comments
 (0)