|
9 | 9 | from .checkpoints import (
|
10 | 10 | AsyncCheckpoints,
|
11 | 11 | Checkpoints,
|
| 12 | + AsyncGenericCheckpointsMixin, |
12 | 13 | GenericCheckpointsMixin,
|
13 | 14 | )
|
14 | 15 | from .fileio import AsyncFileManagerMixin, FileManagerMixin
|
@@ -266,3 +267,68 @@ def get_file_checkpoint(self, checkpoint_id, path):
|
266 | 267 | 'content': content,
|
267 | 268 | 'format': format,
|
268 | 269 | }
|
| 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 | + } |
0 commit comments