Skip to content

Commit 564325e

Browse files
Mariko WakabayashiZsailer
authored andcommitted
Create AsyncGenericFileCheckpoints
1 parent 9be92d8 commit 564325e

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

jupyter_server/services/contents/filecheckpoints.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .checkpoints import (
1010
AsyncCheckpoints,
1111
Checkpoints,
12+
AsyncGenericCheckpointsMixin,
1213
GenericCheckpointsMixin,
1314
)
1415
from .fileio import AsyncFileManagerMixin, FileManagerMixin
@@ -266,3 +267,68 @@ def get_file_checkpoint(self, checkpoint_id, path):
266267
'content': content,
267268
'format': format,
268269
}
270+
271+
272+
class AsyncGenericFileCheckpoints(AsyncGenericCheckpointsMixin, AsyncFileCheckpoints):
273+
"""
274+
Asynchronous Local filesystem Checkpoints that works with any conforming
275+
ContentsManager.
276+
"""
277+
async def create_file_checkpoint(self, content, format, path):
278+
"""Create a checkpoint from the current content of a file."""
279+
path = path.strip('/')
280+
# only the one checkpoint ID:
281+
checkpoint_id = u"checkpoint"
282+
os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)
283+
self.log.debug("creating checkpoint for %s", path)
284+
with self.perm_to_403():
285+
await self._save_file(os_checkpoint_path, content, format=format)
286+
287+
# return the checkpoint info
288+
return await self.checkpoint_model(checkpoint_id, os_checkpoint_path)
289+
290+
async def create_notebook_checkpoint(self, nb, path):
291+
"""Create a checkpoint from the current content of a notebook."""
292+
path = path.strip('/')
293+
# only the one checkpoint ID:
294+
checkpoint_id = u"checkpoint"
295+
os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)
296+
self.log.debug("creating checkpoint for %s", path)
297+
with self.perm_to_403():
298+
await self._save_notebook(os_checkpoint_path, nb)
299+
300+
# return the checkpoint info
301+
return await self.checkpoint_model(checkpoint_id, os_checkpoint_path)
302+
303+
async def get_notebook_checkpoint(self, checkpoint_id, path):
304+
"""Get a checkpoint for a notebook."""
305+
path = path.strip('/')
306+
self.log.info("restoring %s from checkpoint %s", path, checkpoint_id)
307+
os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)
308+
309+
if not os.path.isfile(os_checkpoint_path):
310+
self.no_such_checkpoint(path, checkpoint_id)
311+
312+
return {
313+
'type': 'notebook',
314+
'content': await self._read_notebook(
315+
os_checkpoint_path,
316+
as_version=4,
317+
),
318+
}
319+
320+
async def get_file_checkpoint(self, checkpoint_id, path):
321+
"""Get a checkpoint for a file."""
322+
path = path.strip('/')
323+
self.log.info("restoring %s from checkpoint %s", path, checkpoint_id)
324+
os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)
325+
326+
if not os.path.isfile(os_checkpoint_path):
327+
self.no_such_checkpoint(path, checkpoint_id)
328+
329+
content, format = await self._read_file(os_checkpoint_path, format=None)
330+
return {
331+
'type': 'file',
332+
'content': content,
333+
'format': format,
334+
}

tests/services/contents/test_config.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import pytest
22

33
from jupyter_server.services.contents.checkpoints import AsyncCheckpoints
4-
from jupyter_server.services.contents.filecheckpoints import GenericFileCheckpoints
4+
from jupyter_server.services.contents.filecheckpoints import AsyncGenericFileCheckpoints, GenericFileCheckpoints
55
from jupyter_server.services.contents.manager import AsyncContentsManager
66

77

8-
@pytest.fixture
9-
def jp_server_config():
10-
return {'FileContentsManager': {'checkpoints_class': GenericFileCheckpoints}}
8+
@pytest.fixture(params=[AsyncGenericFileCheckpoints, GenericFileCheckpoints])
9+
def jp_server_config(request):
10+
return {'FileContentsManager': {'checkpoints_class': request.param}}
1111

1212

13-
def test_config_did_something(jp_serverapp):
14-
assert isinstance(jp_serverapp.contents_manager.checkpoints, GenericFileCheckpoints)
13+
def test_config_did_something(jp_server_config, jp_serverapp):
14+
assert isinstance(jp_serverapp.contents_manager.checkpoints,
15+
jp_server_config['FileContentsManager']['checkpoints_class'])
1516

1617

1718
async def test_async_contents_manager(jp_configurable_serverapp):

0 commit comments

Comments
 (0)