mirror of
https://github.com/jupyter/notebook.git
synced 2025-02-05 12:19:58 +08:00
DEV: Break get_checkpoint into separate methods.
One for notebooks and one for checkpoints.
This commit is contained in:
parent
2569c3069d
commit
64e5c49608
@ -5,6 +5,8 @@ Classes for managing Checkpoints.
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
from tornado.web import HTTPError
|
||||
|
||||
from IPython.config.configurable import LoggingConfigurable
|
||||
|
||||
|
||||
@ -59,17 +61,18 @@ class GenericCheckpointsMixin(object):
|
||||
Provides a ContentsManager-agnostic implementation of `create_checkpoint`
|
||||
and `restore_checkpoint` in terms of the following operations:
|
||||
|
||||
create_file_checkpoint(self, content, format, path)
|
||||
create_notebook_checkpoint(self, nb, path)
|
||||
get_checkpoint(self, checkpoint_id, path, type)
|
||||
- create_file_checkpoint(self, content, format, path)
|
||||
- create_notebook_checkpoint(self, nb, path)
|
||||
- get_file_checkpoint(self, checkpoint_id, path)
|
||||
- get_notebook_checkpoint(self, checkpoint_id, path)
|
||||
|
||||
To create a generic CheckpointManager, add this mixin to a class that
|
||||
implement the above three methods plus the remaining Checkpoints API
|
||||
methods:
|
||||
|
||||
delete_checkpoint(self, checkpoint_id, path)
|
||||
list_checkpoints(self, path)
|
||||
rename_checkpoint(self, checkpoint_id, old_path, new_path)
|
||||
- delete_checkpoint(self, checkpoint_id, path)
|
||||
- list_checkpoints(self, path)
|
||||
- rename_checkpoint(self, checkpoint_id, old_path, new_path)
|
||||
"""
|
||||
|
||||
def create_checkpoint(self, contents_mgr, path):
|
||||
@ -86,11 +89,18 @@ class GenericCheckpointsMixin(object):
|
||||
model['format'],
|
||||
path,
|
||||
)
|
||||
else:
|
||||
raise HTTPError(500, u'Unexpected type %s' % type)
|
||||
|
||||
def restore_checkpoint(self, contents_mgr, checkpoint_id, path):
|
||||
"""Restore a checkpoint."""
|
||||
type = contents_mgr.get(path, content=False)['type']
|
||||
model = self.get_checkpoint(checkpoint_id, path, type)
|
||||
if type == 'notebook':
|
||||
model = self.get_notebook_checkpoint(checkpoint_id, path)
|
||||
elif type == 'file':
|
||||
model = self.get_file_checkpoint(checkpoint_id, path)
|
||||
else:
|
||||
raise HTTPError(500, u'Unexpected type %s' % type)
|
||||
contents_mgr.save(model, path)
|
||||
|
||||
# Required Methods
|
||||
|
@ -167,31 +167,34 @@ class GenericFileCheckpoints(GenericCheckpointsMixin, FileCheckpoints):
|
||||
# return the checkpoint info
|
||||
return self.checkpoint_model(checkpoint_id, os_checkpoint_path)
|
||||
|
||||
def get_checkpoint(self, checkpoint_id, path, type):
|
||||
"""Get the content of a checkpoint.
|
||||
def get_notebook_checkpoint(self, checkpoint_id, path):
|
||||
|
||||
Returns a model suitable for passing to ContentsManager.save.
|
||||
"""
|
||||
path = path.strip('/')
|
||||
self.log.info("restoring %s from checkpoint %s", path, checkpoint_id)
|
||||
os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)
|
||||
|
||||
if not os.path.isfile(os_checkpoint_path):
|
||||
self.no_such_checkpoint(path, checkpoint_id)
|
||||
|
||||
if type == 'notebook':
|
||||
return {
|
||||
'type': type,
|
||||
'content': self._read_notebook(
|
||||
os_checkpoint_path,
|
||||
as_version=4,
|
||||
),
|
||||
}
|
||||
elif type == 'file':
|
||||
content, format = self._read_file(os_checkpoint_path, format=None)
|
||||
return {
|
||||
'type': type,
|
||||
'content': content,
|
||||
'format': format,
|
||||
}
|
||||
else:
|
||||
raise HTTPError(500, u'Unexpected type %s' % type)
|
||||
return {
|
||||
'type': 'notebook',
|
||||
'content': self._read_notebook(
|
||||
os_checkpoint_path,
|
||||
as_version=4,
|
||||
),
|
||||
}
|
||||
|
||||
def get_file_checkpoint(self, checkpoint_id, path):
|
||||
path = path.strip('/')
|
||||
self.log.info("restoring %s from checkpoint %s", path, checkpoint_id)
|
||||
os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)
|
||||
|
||||
if not os.path.isfile(os_checkpoint_path):
|
||||
self.no_such_checkpoint(path, checkpoint_id)
|
||||
|
||||
content, format = self._read_file(os_checkpoint_path, format=None)
|
||||
return {
|
||||
'type': 'file',
|
||||
'content': content,
|
||||
'format': format,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user