Skip to content

Commit 1cfc211

Browse files
Mariko WakabayashiZsailer
authored andcommitted
[DRAFT] Asynchronous Contents API
1 parent 28f784b commit 1cfc211

File tree

3 files changed

+346
-20
lines changed

3 files changed

+346
-20
lines changed

jupyter_server/services/contents/checkpoints.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,54 @@ def get_notebook_checkpoint(self, checkpoint_id, path):
140140
}
141141
"""
142142
raise NotImplementedError("must be implemented in a subclass")
143+
144+
145+
class AsyncCheckpoints(Checkpoints):
146+
"""
147+
Base class for managing checkpoints for a ContentsManager asynchronously.
148+
"""
149+
150+
async def rename_all_checkpoints(self, old_path, new_path):
151+
"""Rename all checkpoints for old_path to new_path."""
152+
for cp in (await self.list_checkpoints(old_path)):
153+
await self.rename_checkpoint(cp['id'], old_path, new_path)
154+
155+
async def delete_all_checkpoints(self, path):
156+
"""Delete all checkpoints for the given path."""
157+
for checkpoint in (await self.list_checkpoints(path)):
158+
await self.delete_checkpoint(checkpoint['id'], path)
159+
160+
161+
class AsyncGenericCheckpointsMixin(GenericCheckpointsMixin):
162+
"""
163+
Helper for creating Asynchronous Checkpoints subclasses that can be used with any
164+
ContentsManager.
165+
"""
166+
167+
async def create_checkpoint(self, contents_mgr, path):
168+
model = await contents_mgr.get(path, content=True)
169+
type = model['type']
170+
if type == 'notebook':
171+
return await self.create_notebook_checkpoint(
172+
model['content'],
173+
path,
174+
)
175+
elif type == 'file':
176+
return await self.create_file_checkpoint(
177+
model['content'],
178+
model['format'],
179+
path,
180+
)
181+
else:
182+
raise HTTPError(500, u'Unexpected type %s' % type)
183+
184+
async def restore_checkpoint(self, contents_mgr, checkpoint_id, path):
185+
"""Restore a checkpoint."""
186+
type = await contents_mgr.get(path, content=False)['type']
187+
if type == 'notebook':
188+
model = await self.get_notebook_checkpoint(checkpoint_id, path)
189+
elif type == 'file':
190+
model = await self.get_file_checkpoint(checkpoint_id, path)
191+
else:
192+
raise HTTPError(500, u'Unexpected type %s' % type)
193+
await contents_mgr.save(model, path)

0 commit comments

Comments
 (0)