##// END OF EJS Templates
Merge pull request #5708 from minrk/checkpoints-path...
Thomas Kluyver -
r16462:a7c5cada merge
parent child Browse files
Show More
@@ -60,31 +60,15 b' class FileNotebookManager(NotebookManager):'
60 return
60 return
61 if not os.path.exists(new) or not os.path.isdir(new):
61 if not os.path.exists(new) or not os.path.isdir(new):
62 raise TraitError("notebook dir %r is not a directory" % new)
62 raise TraitError("notebook dir %r is not a directory" % new)
63
63
64 checkpoint_dir = Unicode(config=True,
64 checkpoint_dir = Unicode('.ipynb_checkpoints', config=True,
65 help="""The location in which to keep notebook checkpoints
65 help="""The directory name in which to keep notebook checkpoints
66
67 This is a path relative to the notebook's own directory.
66
68
67 By default, it is notebook-dir/.ipynb_checkpoints
69 By default, it is .ipynb_checkpoints
68 """
70 """
69 )
71 )
70 def _checkpoint_dir_default(self):
71 return os.path.join(self.notebook_dir, '.ipynb_checkpoints')
72
73 def _checkpoint_dir_changed(self, name, old, new):
74 """do a bit of validation of the checkpoint dir"""
75 if not os.path.isabs(new):
76 # If we receive a non-absolute path, make it absolute.
77 abs_new = os.path.abspath(new)
78 self.checkpoint_dir = abs_new
79 return
80 if os.path.exists(new) and not os.path.isdir(new):
81 raise TraitError("checkpoint dir %r is not a directory" % new)
82 if not os.path.exists(new):
83 self.log.info("Creating checkpoint dir %s", new)
84 try:
85 os.mkdir(new)
86 except:
87 raise TraitError("Couldn't create checkpoint dir %r" % new)
88
72
89 def _copy(self, src, dest):
73 def _copy(self, src, dest):
90 """copy src to dest
74 """copy src to dest
@@ -416,7 +400,11 b' class FileNotebookManager(NotebookManager):'
416 checkpoint_id=checkpoint_id,
400 checkpoint_id=checkpoint_id,
417 ext=self.filename_ext,
401 ext=self.filename_ext,
418 )
402 )
419 cp_path = os.path.join(path, self.checkpoint_dir, filename)
403 os_path = self._get_os_path(path=path)
404 cp_dir = os.path.join(os_path, self.checkpoint_dir)
405 if not os.path.exists(cp_dir):
406 os.mkdir(cp_dir)
407 cp_path = os.path.join(cp_dir, filename)
420 return cp_path
408 return cp_path
421
409
422 def get_checkpoint_model(self, checkpoint_id, name, path=''):
410 def get_checkpoint_model(self, checkpoint_id, name, path=''):
@@ -455,8 +443,8 b' class FileNotebookManager(NotebookManager):'
455 """
443 """
456 path = path.strip('/')
444 path = path.strip('/')
457 checkpoint_id = "checkpoint"
445 checkpoint_id = "checkpoint"
458 path = self.get_checkpoint_path(checkpoint_id, name, path)
446 os_path = self.get_checkpoint_path(checkpoint_id, name, path)
459 if not os.path.exists(path):
447 if not os.path.exists(os_path):
460 return []
448 return []
461 else:
449 else:
462 return [self.get_checkpoint_model(checkpoint_id, name, path)]
450 return [self.get_checkpoint_model(checkpoint_id, name, path)]
@@ -55,6 +55,20 b' class TestFileNotebookManager(TestCase):'
55 path = fm._get_os_path('test.ipynb', '////')
55 path = fm._get_os_path('test.ipynb', '////')
56 fs_path = os.path.join(fm.notebook_dir, 'test.ipynb')
56 fs_path = os.path.join(fm.notebook_dir, 'test.ipynb')
57 self.assertEqual(path, fs_path)
57 self.assertEqual(path, fs_path)
58
59 def test_checkpoint_subdir(self):
60 subd = u'sub ∂ir'
61 cp_name = 'test-cp.ipynb'
62 with TemporaryDirectory() as td:
63 nbdir = td
64 os.mkdir(os.path.join(td, subd))
65 fm = FileNotebookManager(notebook_dir=nbdir)
66 cp_dir = fm.get_checkpoint_path('cp', 'test.ipynb', '/')
67 cp_subdir = fm.get_checkpoint_path('cp', 'test.ipynb', '/%s/' % subd)
68 self.assertNotEqual(cp_dir, cp_subdir)
69 self.assertEqual(cp_dir, os.path.join(nbdir, fm.checkpoint_dir, cp_name))
70 self.assertEqual(cp_subdir, os.path.join(nbdir, subd, fm.checkpoint_dir, cp_name))
71
58
72
59 class TestNotebookManager(TestCase):
73 class TestNotebookManager(TestCase):
60
74
General Comments 0
You need to be logged in to leave comments. Login now