We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
GenericCheckpointsMixin missing methods
In the commit https://github.com/ipython/ipython/commit/82dcba3ba9ffa3ee3193602e2d5df3ef3c3de106 two new methods were introduced `get_notebook_checkpoint` and `get_file_checkpoint` but the definition of 
`GenericCheckpointsMixin` itself was not updated. 

It still has `get_checkpoint` method. Subclasses work now, but just thought of notifying it.

The file in question is: `ipython/IPython/html/services/contents/checkpoints.py` and the branch https://github.com/ipython/ipython/releases/tag/rel-3.1.0


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `IPython/html/services/contents/checkpoints.py`
Content:
```
1 """
2 Classes for managing Checkpoints.
3 """
4 
5 # Copyright (c) IPython Development Team.
6 # Distributed under the terms of the Modified BSD License.
7 
8 from tornado.web import HTTPError
9 
10 from IPython.config.configurable import LoggingConfigurable
11 
12 
13 class Checkpoints(LoggingConfigurable):
14     """
15     Base class for managing checkpoints for a ContentsManager.
16 
17     Subclasses are required to implement:
18 
19     create_checkpoint(self, contents_mgr, path)
20     restore_checkpoint(self, contents_mgr, checkpoint_id, path)
21     rename_checkpoint(self, checkpoint_id, old_path, new_path)
22     delete_checkpoint(self, checkpoint_id, path)
23     list_checkpoints(self, path)
24     """
25     def create_checkpoint(self, contents_mgr, path):
26         """Create a checkpoint."""
27         raise NotImplementedError("must be implemented in a subclass")
28 
29     def restore_checkpoint(self, contents_mgr, checkpoint_id, path):
30         """Restore a checkpoint"""
31         raise NotImplementedError("must be implemented in a subclass")
32 
33     def rename_checkpoint(self, checkpoint_id, old_path, new_path):
34         """Rename a single checkpoint from old_path to new_path."""
35         raise NotImplementedError("must be implemented in a subclass")
36 
37     def delete_checkpoint(self, checkpoint_id, path):
38         """delete a checkpoint for a file"""
39         raise NotImplementedError("must be implemented in a subclass")
40 
41     def list_checkpoints(self, path):
42         """Return a list of checkpoints for a given file"""
43         raise NotImplementedError("must be implemented in a subclass")
44 
45     def rename_all_checkpoints(self, old_path, new_path):
46         """Rename all checkpoints for old_path to new_path."""
47         for cp in self.list_checkpoints(old_path):
48             self.rename_checkpoint(cp['id'], old_path, new_path)
49 
50     def delete_all_checkpoints(self, path):
51         """Delete all checkpoints for the given path."""
52         for checkpoint in self.list_checkpoints(path):
53             self.delete_checkpoint(checkpoint['id'], path)
54 
55 
56 class GenericCheckpointsMixin(object):
57     """
58     Helper for creating Checkpoints subclasses that can be used with any
59     ContentsManager.
60 
61     Provides a ContentsManager-agnostic implementation of `create_checkpoint`
62     and `restore_checkpoint` in terms of the following operations:
63 
64     - create_file_checkpoint(self, content, format, path)
65     - create_notebook_checkpoint(self, nb, path)
66     - get_file_checkpoint(self, checkpoint_id, path)
67     - get_notebook_checkpoint(self, checkpoint_id, path)
68 
69     To create a generic CheckpointManager, add this mixin to a class that
70     implement the above three methods plus the remaining Checkpoints API
71     methods:
72 
73     - delete_checkpoint(self, checkpoint_id, path)
74     - list_checkpoints(self, path)
75     - rename_checkpoint(self, checkpoint_id, old_path, new_path)
76     """
77 
78     def create_checkpoint(self, contents_mgr, path):
79         model = contents_mgr.get(path, content=True)
80         type = model['type']
81         if type == 'notebook':
82             return self.create_notebook_checkpoint(
83                 model['content'],
84                 path,
85             )
86         elif type == 'file':
87             return self.create_file_checkpoint(
88                 model['content'],
89                 model['format'],
90                 path,
91             )
92         else:
93             raise HTTPError(500, u'Unexpected type %s' % type)
94 
95     def restore_checkpoint(self, contents_mgr, checkpoint_id, path):
96         """Restore a checkpoint."""
97         type = contents_mgr.get(path, content=False)['type']
98         if type == 'notebook':
99             model = self.get_notebook_checkpoint(checkpoint_id, path)
100         elif type == 'file':
101             model = self.get_file_checkpoint(checkpoint_id, path)
102         else:
103             raise HTTPError(500, u'Unexpected type %s' % type)
104         contents_mgr.save(model, path)
105 
106     # Required Methods
107     def create_file_checkpoint(self, content, format, path):
108         """Create a checkpoint of the current state of a file
109 
110         Returns a checkpoint model for the new checkpoint.
111         """
112         raise NotImplementedError("must be implemented in a subclass")
113 
114     def create_notebook_checkpoint(self, nb, path):
115         """Create a checkpoint of the current state of a file
116 
117         Returns a checkpoint model for the new checkpoint.
118         """
119         raise NotImplementedError("must be implemented in a subclass")
120 
121     def get_checkpoint(self, checkpoint_id, path, type):
122         """Get the content of a checkpoint.
123 
124         Returns an unvalidated model with the same structure as
125         the return value of ContentsManager.get
126         """
127         raise NotImplementedError("must be implemented in a subclass")
128
```

Path: `IPython/html/services/contents/filecheckpoints.py`
Content:
```
1 """
2 File-based Checkpoints implementations.
3 """
4 import os
5 import shutil
6 
7 from tornado.web import HTTPError
8 
9 from .checkpoints import (
10     Checkpoints,
11     GenericCheckpointsMixin,
12 )
13 from .fileio import FileManagerMixin
14 
15 from IPython.utils import tz
16 from IPython.utils.path import ensure_dir_exists
17 from IPython.utils.py3compat import getcwd
18 from IPython.utils.traitlets import Unicode
19 
20 
21 class FileCheckpoints(FileManagerMixin, Checkpoints):
22     """
23     A Checkpoints that caches checkpoints for files in adjacent
24     directories.
25 
26     Only works with FileContentsManager.  Use GenericFileCheckpoints if
27     you want file-based checkpoints with another ContentsManager.
28     """
29 
30     checkpoint_dir = Unicode(
31         '.ipynb_checkpoints',
32         config=True,
33         help="""The directory name in which to keep file checkpoints
34 
35         This is a path relative to the file's own directory.
36 
37         By default, it is .ipynb_checkpoints
38         """,
39     )
40 
41     root_dir = Unicode(config=True)
42 
43     def _root_dir_default(self):
44         try:
45             return self.parent.root_dir
46         except AttributeError:
47             return getcwd()
48 
49     # ContentsManager-dependent checkpoint API
50     def create_checkpoint(self, contents_mgr, path):
51         """Create a checkpoint."""
52         checkpoint_id = u'checkpoint'
53         src_path = contents_mgr._get_os_path(path)
54         dest_path = self.checkpoint_path(checkpoint_id, path)
55         self._copy(src_path, dest_path)
56         return self.checkpoint_model(checkpoint_id, dest_path)
57 
58     def restore_checkpoint(self, contents_mgr, checkpoint_id, path):
59         """Restore a checkpoint."""
60         src_path = self.checkpoint_path(checkpoint_id, path)
61         dest_path = contents_mgr._get_os_path(path)
62         self._copy(src_path, dest_path)
63 
64     # ContentsManager-independent checkpoint API
65     def rename_checkpoint(self, checkpoint_id, old_path, new_path):
66         """Rename a checkpoint from old_path to new_path."""
67         old_cp_path = self.checkpoint_path(checkpoint_id, old_path)
68         new_cp_path = self.checkpoint_path(checkpoint_id, new_path)
69         if os.path.isfile(old_cp_path):
70             self.log.debug(
71                 "Renaming checkpoint %s -> %s",
72                 old_cp_path,
73                 new_cp_path,
74             )
75             with self.perm_to_403():
76                 shutil.move(old_cp_path, new_cp_path)
77 
78     def delete_checkpoint(self, checkpoint_id, path):
79         """delete a file's checkpoint"""
80         path = path.strip('/')
81         cp_path = self.checkpoint_path(checkpoint_id, path)
82         if not os.path.isfile(cp_path):
83             self.no_such_checkpoint(path, checkpoint_id)
84 
85         self.log.debug("unlinking %s", cp_path)
86         with self.perm_to_403():
87             os.unlink(cp_path)
88 
89     def list_checkpoints(self, path):
90         """list the checkpoints for a given file
91 
92         This contents manager currently only supports one checkpoint per file.
93         """
94         path = path.strip('/')
95         checkpoint_id = "checkpoint"
96         os_path = self.checkpoint_path(checkpoint_id, path)
97         if not os.path.isfile(os_path):
98             return []
99         else:
100             return [self.checkpoint_model(checkpoint_id, os_path)]
101 
102     # Checkpoint-related utilities
103     def checkpoint_path(self, checkpoint_id, path):
104         """find the path to a checkpoint"""
105         path = path.strip('/')
106         parent, name = ('/' + path).rsplit('/', 1)
107         parent = parent.strip('/')
108         basename, ext = os.path.splitext(name)
109         filename = u"{name}-{checkpoint_id}{ext}".format(
110             name=basename,
111             checkpoint_id=checkpoint_id,
112             ext=ext,
113         )
114         os_path = self._get_os_path(path=parent)
115         cp_dir = os.path.join(os_path, self.checkpoint_dir)
116         with self.perm_to_403():
117             ensure_dir_exists(cp_dir)
118         cp_path = os.path.join(cp_dir, filename)
119         return cp_path
120 
121     def checkpoint_model(self, checkpoint_id, os_path):
122         """construct the info dict for a given checkpoint"""
123         stats = os.stat(os_path)
124         last_modified = tz.utcfromtimestamp(stats.st_mtime)
125         info = dict(
126             id=checkpoint_id,
127             last_modified=last_modified,
128         )
129         return info
130 
131     # Error Handling
132     def no_such_checkpoint(self, path, checkpoint_id):
133         raise HTTPError(
134             404,
135             u'Checkpoint does not exist: %s@%s' % (path, checkpoint_id)
136         )
137 
138 
139 class GenericFileCheckpoints(GenericCheckpointsMixin, FileCheckpoints):
140     """
141     Local filesystem Checkpoints that works with any conforming
142     ContentsManager.
143     """
144     def create_file_checkpoint(self, content, format, path):
145         """Create a checkpoint from the current content of a notebook."""
146         path = path.strip('/')
147         # only the one checkpoint ID:
148         checkpoint_id = u"checkpoint"
149         os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)
150         self.log.debug("creating checkpoint for %s", path)
151         with self.perm_to_403():
152             self._save_file(os_checkpoint_path, content, format=format)
153 
154         # return the checkpoint info
155         return self.checkpoint_model(checkpoint_id, os_checkpoint_path)
156 
157     def create_notebook_checkpoint(self, nb, path):
158         """Create a checkpoint from the current content of a notebook."""
159         path = path.strip('/')
160         # only the one checkpoint ID:
161         checkpoint_id = u"checkpoint"
162         os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)
163         self.log.debug("creating checkpoint for %s", path)
164         with self.perm_to_403():
165             self._save_notebook(os_checkpoint_path, nb)
166 
167         # return the checkpoint info
168         return self.checkpoint_model(checkpoint_id, os_checkpoint_path)
169 
170     def get_notebook_checkpoint(self, checkpoint_id, path):
171 
172         path = path.strip('/')
173         self.log.info("restoring %s from checkpoint %s", path, checkpoint_id)
174         os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)
175 
176         if not os.path.isfile(os_checkpoint_path):
177             self.no_such_checkpoint(path, checkpoint_id)
178 
179         return {
180             'type': 'notebook',
181             'content': self._read_notebook(
182                 os_checkpoint_path,
183                 as_version=4,
184             ),
185         }
186 
187     def get_file_checkpoint(self, checkpoint_id, path):
188         path = path.strip('/')
189         self.log.info("restoring %s from checkpoint %s", path, checkpoint_id)
190         os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)
191 
192         if not os.path.isfile(os_checkpoint_path):
193             self.no_such_checkpoint(path, checkpoint_id)
194 
195         content, format = self._read_file(os_checkpoint_path, format=None)
196         return {
197             'type': 'file',
198             'content': content,
199             'format': format,
200         }
201
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/IPython/html/services/contents/checkpoints.py b/IPython/html/services/contents/checkpoints.py
--- a/IPython/html/services/contents/checkpoints.py
+++ b/IPython/html/services/contents/checkpoints.py
@@ -67,7 +67,7 @@
     - 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
+    implement the above four methods plus the remaining Checkpoints API
     methods:
 
     - delete_checkpoint(self, checkpoint_id, path)
@@ -118,10 +118,25 @@
         """
         raise NotImplementedError("must be implemented in a subclass")
 
-    def get_checkpoint(self, checkpoint_id, path, type):
-        """Get the content of a checkpoint.
+    def get_file_checkpoint(self, checkpoint_id, path):
+        """Get the content of a checkpoint for a non-notebook file.
 
-        Returns an unvalidated model with the same structure as
-        the return value of ContentsManager.get
+        Returns a dict of the form:
+        {
+            'type': 'file',
+            'content': <str>,
+            'format': {'text','base64'},
+        }
+        """
+        raise NotImplementedError("must be implemented in a subclass")
+
+    def get_notebook_checkpoint(self, checkpoint_id, path):
+        """Get the content of a checkpoint for a notebook.
+
+        Returns a dict of the form:
+        {
+            'type': 'notebook',
+            'content': <output of nbformat.read>,
+        }
         """
         raise NotImplementedError("must be implemented in a subclass")
diff --git a/IPython/html/services/contents/filecheckpoints.py b/IPython/html/services/contents/filecheckpoints.py
--- a/IPython/html/services/contents/filecheckpoints.py
+++ b/IPython/html/services/contents/filecheckpoints.py
@@ -142,7 +142,7 @@
     ContentsManager.
     """
     def create_file_checkpoint(self, content, format, path):
-        """Create a checkpoint from the current content of a notebook."""
+        """Create a checkpoint from the current content of a file."""
         path = path.strip('/')
         # only the one checkpoint ID:
         checkpoint_id = u"checkpoint"
@@ -168,7 +168,7 @@
         return self.checkpoint_model(checkpoint_id, os_checkpoint_path)
 
     def get_notebook_checkpoint(self, checkpoint_id, path):
-
+        """Get a checkpoint for a notebook."""
         path = path.strip('/')
         self.log.info("restoring %s from checkpoint %s", path, checkpoint_id)
         os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)
@@ -185,6 +185,7 @@
         }
 
     def get_file_checkpoint(self, checkpoint_id, path):
+        """Get a checkpoint for a file."""
         path = path.strip('/')
         self.log.info("restoring %s from checkpoint %s", path, checkpoint_id)
         os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)

{"golden_diff": "diff --git a/IPython/html/services/contents/checkpoints.py b/IPython/html/services/contents/checkpoints.py\n--- a/IPython/html/services/contents/checkpoints.py\n+++ b/IPython/html/services/contents/checkpoints.py\n@@ -67,7 +67,7 @@\n     - get_notebook_checkpoint(self, checkpoint_id, path)\n \n     To create a generic CheckpointManager, add this mixin to a class that\n-    implement the above three methods plus the remaining Checkpoints API\n+    implement the above four methods plus the remaining Checkpoints API\n     methods:\n \n     - delete_checkpoint(self, checkpoint_id, path)\n@@ -118,10 +118,25 @@\n         \"\"\"\n         raise NotImplementedError(\"must be implemented in a subclass\")\n \n-    def get_checkpoint(self, checkpoint_id, path, type):\n-        \"\"\"Get the content of a checkpoint.\n+    def get_file_checkpoint(self, checkpoint_id, path):\n+        \"\"\"Get the content of a checkpoint for a non-notebook file.\n \n-        Returns an unvalidated model with the same structure as\n-        the return value of ContentsManager.get\n+        Returns a dict of the form:\n+        {\n+            'type': 'file',\n+            'content': <str>,\n+            'format': {'text','base64'},\n+        }\n+        \"\"\"\n+        raise NotImplementedError(\"must be implemented in a subclass\")\n+\n+    def get_notebook_checkpoint(self, checkpoint_id, path):\n+        \"\"\"Get the content of a checkpoint for a notebook.\n+\n+        Returns a dict of the form:\n+        {\n+            'type': 'notebook',\n+            'content': <output of nbformat.read>,\n+        }\n         \"\"\"\n         raise NotImplementedError(\"must be implemented in a subclass\")\ndiff --git a/IPython/html/services/contents/filecheckpoints.py b/IPython/html/services/contents/filecheckpoints.py\n--- a/IPython/html/services/contents/filecheckpoints.py\n+++ b/IPython/html/services/contents/filecheckpoints.py\n@@ -142,7 +142,7 @@\n     ContentsManager.\n     \"\"\"\n     def create_file_checkpoint(self, content, format, path):\n-        \"\"\"Create a checkpoint from the current content of a notebook.\"\"\"\n+        \"\"\"Create a checkpoint from the current content of a file.\"\"\"\n         path = path.strip('/')\n         # only the one checkpoint ID:\n         checkpoint_id = u\"checkpoint\"\n@@ -168,7 +168,7 @@\n         return self.checkpoint_model(checkpoint_id, os_checkpoint_path)\n \n     def get_notebook_checkpoint(self, checkpoint_id, path):\n-\n+        \"\"\"Get a checkpoint for a notebook.\"\"\"\n         path = path.strip('/')\n         self.log.info(\"restoring %s from checkpoint %s\", path, checkpoint_id)\n         os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)\n@@ -185,6 +185,7 @@\n         }\n \n     def get_file_checkpoint(self, checkpoint_id, path):\n+        \"\"\"Get a checkpoint for a file.\"\"\"\n         path = path.strip('/')\n         self.log.info(\"restoring %s from checkpoint %s\", path, checkpoint_id)\n         os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)\n", "issue": "GenericCheckpointsMixin missing methods\nIn the commit https://github.com/ipython/ipython/commit/82dcba3ba9ffa3ee3193602e2d5df3ef3c3de106 two new methods were introduced `get_notebook_checkpoint` and `get_file_checkpoint` but the definition of \n`GenericCheckpointsMixin` itself was not updated. \n\nIt still has `get_checkpoint` method. Subclasses work now, but just thought of notifying it.\n\nThe file in question is: `ipython/IPython/html/services/contents/checkpoints.py` and the branch https://github.com/ipython/ipython/releases/tag/rel-3.1.0\n\n", "before_files": [{"content": "\"\"\"\nClasses for managing Checkpoints.\n\"\"\"\n\n# Copyright (c) IPython Development Team.\n# Distributed under the terms of the Modified BSD License.\n\nfrom tornado.web import HTTPError\n\nfrom IPython.config.configurable import LoggingConfigurable\n\n\nclass Checkpoints(LoggingConfigurable):\n    \"\"\"\n    Base class for managing checkpoints for a ContentsManager.\n\n    Subclasses are required to implement:\n\n    create_checkpoint(self, contents_mgr, path)\n    restore_checkpoint(self, contents_mgr, checkpoint_id, path)\n    rename_checkpoint(self, checkpoint_id, old_path, new_path)\n    delete_checkpoint(self, checkpoint_id, path)\n    list_checkpoints(self, path)\n    \"\"\"\n    def create_checkpoint(self, contents_mgr, path):\n        \"\"\"Create a checkpoint.\"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def restore_checkpoint(self, contents_mgr, checkpoint_id, path):\n        \"\"\"Restore a checkpoint\"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def rename_checkpoint(self, checkpoint_id, old_path, new_path):\n        \"\"\"Rename a single checkpoint from old_path to new_path.\"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def delete_checkpoint(self, checkpoint_id, path):\n        \"\"\"delete a checkpoint for a file\"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def list_checkpoints(self, path):\n        \"\"\"Return a list of checkpoints for a given file\"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def rename_all_checkpoints(self, old_path, new_path):\n        \"\"\"Rename all checkpoints for old_path to new_path.\"\"\"\n        for cp in self.list_checkpoints(old_path):\n            self.rename_checkpoint(cp['id'], old_path, new_path)\n\n    def delete_all_checkpoints(self, path):\n        \"\"\"Delete all checkpoints for the given path.\"\"\"\n        for checkpoint in self.list_checkpoints(path):\n            self.delete_checkpoint(checkpoint['id'], path)\n\n\nclass GenericCheckpointsMixin(object):\n    \"\"\"\n    Helper for creating Checkpoints subclasses that can be used with any\n    ContentsManager.\n\n    Provides a ContentsManager-agnostic implementation of `create_checkpoint`\n    and `restore_checkpoint` in terms of the following operations:\n\n    - create_file_checkpoint(self, content, format, path)\n    - create_notebook_checkpoint(self, nb, path)\n    - get_file_checkpoint(self, checkpoint_id, path)\n    - get_notebook_checkpoint(self, checkpoint_id, path)\n\n    To create a generic CheckpointManager, add this mixin to a class that\n    implement the above three methods plus the remaining Checkpoints API\n    methods:\n\n    - delete_checkpoint(self, checkpoint_id, path)\n    - list_checkpoints(self, path)\n    - rename_checkpoint(self, checkpoint_id, old_path, new_path)\n    \"\"\"\n\n    def create_checkpoint(self, contents_mgr, path):\n        model = contents_mgr.get(path, content=True)\n        type = model['type']\n        if type == 'notebook':\n            return self.create_notebook_checkpoint(\n                model['content'],\n                path,\n            )\n        elif type == 'file':\n            return self.create_file_checkpoint(\n                model['content'],\n                model['format'],\n                path,\n            )\n        else:\n            raise HTTPError(500, u'Unexpected type %s' % type)\n\n    def restore_checkpoint(self, contents_mgr, checkpoint_id, path):\n        \"\"\"Restore a checkpoint.\"\"\"\n        type = contents_mgr.get(path, content=False)['type']\n        if type == 'notebook':\n            model = self.get_notebook_checkpoint(checkpoint_id, path)\n        elif type == 'file':\n            model = self.get_file_checkpoint(checkpoint_id, path)\n        else:\n            raise HTTPError(500, u'Unexpected type %s' % type)\n        contents_mgr.save(model, path)\n\n    # Required Methods\n    def create_file_checkpoint(self, content, format, path):\n        \"\"\"Create a checkpoint of the current state of a file\n\n        Returns a checkpoint model for the new checkpoint.\n        \"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def create_notebook_checkpoint(self, nb, path):\n        \"\"\"Create a checkpoint of the current state of a file\n\n        Returns a checkpoint model for the new checkpoint.\n        \"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def get_checkpoint(self, checkpoint_id, path, type):\n        \"\"\"Get the content of a checkpoint.\n\n        Returns an unvalidated model with the same structure as\n        the return value of ContentsManager.get\n        \"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n", "path": "IPython/html/services/contents/checkpoints.py"}, {"content": "\"\"\"\nFile-based Checkpoints implementations.\n\"\"\"\nimport os\nimport shutil\n\nfrom tornado.web import HTTPError\n\nfrom .checkpoints import (\n    Checkpoints,\n    GenericCheckpointsMixin,\n)\nfrom .fileio import FileManagerMixin\n\nfrom IPython.utils import tz\nfrom IPython.utils.path import ensure_dir_exists\nfrom IPython.utils.py3compat import getcwd\nfrom IPython.utils.traitlets import Unicode\n\n\nclass FileCheckpoints(FileManagerMixin, Checkpoints):\n    \"\"\"\n    A Checkpoints that caches checkpoints for files in adjacent\n    directories.\n\n    Only works with FileContentsManager.  Use GenericFileCheckpoints if\n    you want file-based checkpoints with another ContentsManager.\n    \"\"\"\n\n    checkpoint_dir = Unicode(\n        '.ipynb_checkpoints',\n        config=True,\n        help=\"\"\"The directory name in which to keep file checkpoints\n\n        This is a path relative to the file's own directory.\n\n        By default, it is .ipynb_checkpoints\n        \"\"\",\n    )\n\n    root_dir = Unicode(config=True)\n\n    def _root_dir_default(self):\n        try:\n            return self.parent.root_dir\n        except AttributeError:\n            return getcwd()\n\n    # ContentsManager-dependent checkpoint API\n    def create_checkpoint(self, contents_mgr, path):\n        \"\"\"Create a checkpoint.\"\"\"\n        checkpoint_id = u'checkpoint'\n        src_path = contents_mgr._get_os_path(path)\n        dest_path = self.checkpoint_path(checkpoint_id, path)\n        self._copy(src_path, dest_path)\n        return self.checkpoint_model(checkpoint_id, dest_path)\n\n    def restore_checkpoint(self, contents_mgr, checkpoint_id, path):\n        \"\"\"Restore a checkpoint.\"\"\"\n        src_path = self.checkpoint_path(checkpoint_id, path)\n        dest_path = contents_mgr._get_os_path(path)\n        self._copy(src_path, dest_path)\n\n    # ContentsManager-independent checkpoint API\n    def rename_checkpoint(self, checkpoint_id, old_path, new_path):\n        \"\"\"Rename a checkpoint from old_path to new_path.\"\"\"\n        old_cp_path = self.checkpoint_path(checkpoint_id, old_path)\n        new_cp_path = self.checkpoint_path(checkpoint_id, new_path)\n        if os.path.isfile(old_cp_path):\n            self.log.debug(\n                \"Renaming checkpoint %s -> %s\",\n                old_cp_path,\n                new_cp_path,\n            )\n            with self.perm_to_403():\n                shutil.move(old_cp_path, new_cp_path)\n\n    def delete_checkpoint(self, checkpoint_id, path):\n        \"\"\"delete a file's checkpoint\"\"\"\n        path = path.strip('/')\n        cp_path = self.checkpoint_path(checkpoint_id, path)\n        if not os.path.isfile(cp_path):\n            self.no_such_checkpoint(path, checkpoint_id)\n\n        self.log.debug(\"unlinking %s\", cp_path)\n        with self.perm_to_403():\n            os.unlink(cp_path)\n\n    def list_checkpoints(self, path):\n        \"\"\"list the checkpoints for a given file\n\n        This contents manager currently only supports one checkpoint per file.\n        \"\"\"\n        path = path.strip('/')\n        checkpoint_id = \"checkpoint\"\n        os_path = self.checkpoint_path(checkpoint_id, path)\n        if not os.path.isfile(os_path):\n            return []\n        else:\n            return [self.checkpoint_model(checkpoint_id, os_path)]\n\n    # Checkpoint-related utilities\n    def checkpoint_path(self, checkpoint_id, path):\n        \"\"\"find the path to a checkpoint\"\"\"\n        path = path.strip('/')\n        parent, name = ('/' + path).rsplit('/', 1)\n        parent = parent.strip('/')\n        basename, ext = os.path.splitext(name)\n        filename = u\"{name}-{checkpoint_id}{ext}\".format(\n            name=basename,\n            checkpoint_id=checkpoint_id,\n            ext=ext,\n        )\n        os_path = self._get_os_path(path=parent)\n        cp_dir = os.path.join(os_path, self.checkpoint_dir)\n        with self.perm_to_403():\n            ensure_dir_exists(cp_dir)\n        cp_path = os.path.join(cp_dir, filename)\n        return cp_path\n\n    def checkpoint_model(self, checkpoint_id, os_path):\n        \"\"\"construct the info dict for a given checkpoint\"\"\"\n        stats = os.stat(os_path)\n        last_modified = tz.utcfromtimestamp(stats.st_mtime)\n        info = dict(\n            id=checkpoint_id,\n            last_modified=last_modified,\n        )\n        return info\n\n    # Error Handling\n    def no_such_checkpoint(self, path, checkpoint_id):\n        raise HTTPError(\n            404,\n            u'Checkpoint does not exist: %s@%s' % (path, checkpoint_id)\n        )\n\n\nclass GenericFileCheckpoints(GenericCheckpointsMixin, FileCheckpoints):\n    \"\"\"\n    Local filesystem Checkpoints that works with any conforming\n    ContentsManager.\n    \"\"\"\n    def create_file_checkpoint(self, content, format, path):\n        \"\"\"Create a checkpoint from the current content of a notebook.\"\"\"\n        path = path.strip('/')\n        # only the one checkpoint ID:\n        checkpoint_id = u\"checkpoint\"\n        os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)\n        self.log.debug(\"creating checkpoint for %s\", path)\n        with self.perm_to_403():\n            self._save_file(os_checkpoint_path, content, format=format)\n\n        # return the checkpoint info\n        return self.checkpoint_model(checkpoint_id, os_checkpoint_path)\n\n    def create_notebook_checkpoint(self, nb, path):\n        \"\"\"Create a checkpoint from the current content of a notebook.\"\"\"\n        path = path.strip('/')\n        # only the one checkpoint ID:\n        checkpoint_id = u\"checkpoint\"\n        os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)\n        self.log.debug(\"creating checkpoint for %s\", path)\n        with self.perm_to_403():\n            self._save_notebook(os_checkpoint_path, nb)\n\n        # return the checkpoint info\n        return self.checkpoint_model(checkpoint_id, os_checkpoint_path)\n\n    def get_notebook_checkpoint(self, checkpoint_id, path):\n\n        path = path.strip('/')\n        self.log.info(\"restoring %s from checkpoint %s\", path, checkpoint_id)\n        os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)\n\n        if not os.path.isfile(os_checkpoint_path):\n            self.no_such_checkpoint(path, checkpoint_id)\n\n        return {\n            'type': 'notebook',\n            'content': self._read_notebook(\n                os_checkpoint_path,\n                as_version=4,\n            ),\n        }\n\n    def get_file_checkpoint(self, checkpoint_id, path):\n        path = path.strip('/')\n        self.log.info(\"restoring %s from checkpoint %s\", path, checkpoint_id)\n        os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)\n\n        if not os.path.isfile(os_checkpoint_path):\n            self.no_such_checkpoint(path, checkpoint_id)\n\n        content, format = self._read_file(os_checkpoint_path, format=None)\n        return {\n            'type': 'file',\n            'content': content,\n            'format': format,\n        }\n", "path": "IPython/html/services/contents/filecheckpoints.py"}], "after_files": [{"content": "\"\"\"\nClasses for managing Checkpoints.\n\"\"\"\n\n# Copyright (c) IPython Development Team.\n# Distributed under the terms of the Modified BSD License.\n\nfrom tornado.web import HTTPError\n\nfrom IPython.config.configurable import LoggingConfigurable\n\n\nclass Checkpoints(LoggingConfigurable):\n    \"\"\"\n    Base class for managing checkpoints for a ContentsManager.\n\n    Subclasses are required to implement:\n\n    create_checkpoint(self, contents_mgr, path)\n    restore_checkpoint(self, contents_mgr, checkpoint_id, path)\n    rename_checkpoint(self, checkpoint_id, old_path, new_path)\n    delete_checkpoint(self, checkpoint_id, path)\n    list_checkpoints(self, path)\n    \"\"\"\n    def create_checkpoint(self, contents_mgr, path):\n        \"\"\"Create a checkpoint.\"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def restore_checkpoint(self, contents_mgr, checkpoint_id, path):\n        \"\"\"Restore a checkpoint\"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def rename_checkpoint(self, checkpoint_id, old_path, new_path):\n        \"\"\"Rename a single checkpoint from old_path to new_path.\"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def delete_checkpoint(self, checkpoint_id, path):\n        \"\"\"delete a checkpoint for a file\"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def list_checkpoints(self, path):\n        \"\"\"Return a list of checkpoints for a given file\"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def rename_all_checkpoints(self, old_path, new_path):\n        \"\"\"Rename all checkpoints for old_path to new_path.\"\"\"\n        for cp in self.list_checkpoints(old_path):\n            self.rename_checkpoint(cp['id'], old_path, new_path)\n\n    def delete_all_checkpoints(self, path):\n        \"\"\"Delete all checkpoints for the given path.\"\"\"\n        for checkpoint in self.list_checkpoints(path):\n            self.delete_checkpoint(checkpoint['id'], path)\n\n\nclass GenericCheckpointsMixin(object):\n    \"\"\"\n    Helper for creating Checkpoints subclasses that can be used with any\n    ContentsManager.\n\n    Provides a ContentsManager-agnostic implementation of `create_checkpoint`\n    and `restore_checkpoint` in terms of the following operations:\n\n    - create_file_checkpoint(self, content, format, path)\n    - create_notebook_checkpoint(self, nb, path)\n    - get_file_checkpoint(self, checkpoint_id, path)\n    - get_notebook_checkpoint(self, checkpoint_id, path)\n\n    To create a generic CheckpointManager, add this mixin to a class that\n    implement the above four methods plus the remaining Checkpoints API\n    methods:\n\n    - delete_checkpoint(self, checkpoint_id, path)\n    - list_checkpoints(self, path)\n    - rename_checkpoint(self, checkpoint_id, old_path, new_path)\n    \"\"\"\n\n    def create_checkpoint(self, contents_mgr, path):\n        model = contents_mgr.get(path, content=True)\n        type = model['type']\n        if type == 'notebook':\n            return self.create_notebook_checkpoint(\n                model['content'],\n                path,\n            )\n        elif type == 'file':\n            return self.create_file_checkpoint(\n                model['content'],\n                model['format'],\n                path,\n            )\n        else:\n            raise HTTPError(500, u'Unexpected type %s' % type)\n\n    def restore_checkpoint(self, contents_mgr, checkpoint_id, path):\n        \"\"\"Restore a checkpoint.\"\"\"\n        type = contents_mgr.get(path, content=False)['type']\n        if type == 'notebook':\n            model = self.get_notebook_checkpoint(checkpoint_id, path)\n        elif type == 'file':\n            model = self.get_file_checkpoint(checkpoint_id, path)\n        else:\n            raise HTTPError(500, u'Unexpected type %s' % type)\n        contents_mgr.save(model, path)\n\n    # Required Methods\n    def create_file_checkpoint(self, content, format, path):\n        \"\"\"Create a checkpoint of the current state of a file\n\n        Returns a checkpoint model for the new checkpoint.\n        \"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def create_notebook_checkpoint(self, nb, path):\n        \"\"\"Create a checkpoint of the current state of a file\n\n        Returns a checkpoint model for the new checkpoint.\n        \"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def get_file_checkpoint(self, checkpoint_id, path):\n        \"\"\"Get the content of a checkpoint for a non-notebook file.\n\n        Returns a dict of the form:\n        {\n            'type': 'file',\n            'content': <str>,\n            'format': {'text','base64'},\n        }\n        \"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n\n    def get_notebook_checkpoint(self, checkpoint_id, path):\n        \"\"\"Get the content of a checkpoint for a notebook.\n\n        Returns a dict of the form:\n        {\n            'type': 'notebook',\n            'content': <output of nbformat.read>,\n        }\n        \"\"\"\n        raise NotImplementedError(\"must be implemented in a subclass\")\n", "path": "IPython/html/services/contents/checkpoints.py"}, {"content": "\"\"\"\nFile-based Checkpoints implementations.\n\"\"\"\nimport os\nimport shutil\n\nfrom tornado.web import HTTPError\n\nfrom .checkpoints import (\n    Checkpoints,\n    GenericCheckpointsMixin,\n)\nfrom .fileio import FileManagerMixin\n\nfrom IPython.utils import tz\nfrom IPython.utils.path import ensure_dir_exists\nfrom IPython.utils.py3compat import getcwd\nfrom IPython.utils.traitlets import Unicode\n\n\nclass FileCheckpoints(FileManagerMixin, Checkpoints):\n    \"\"\"\n    A Checkpoints that caches checkpoints for files in adjacent\n    directories.\n\n    Only works with FileContentsManager.  Use GenericFileCheckpoints if\n    you want file-based checkpoints with another ContentsManager.\n    \"\"\"\n\n    checkpoint_dir = Unicode(\n        '.ipynb_checkpoints',\n        config=True,\n        help=\"\"\"The directory name in which to keep file checkpoints\n\n        This is a path relative to the file's own directory.\n\n        By default, it is .ipynb_checkpoints\n        \"\"\",\n    )\n\n    root_dir = Unicode(config=True)\n\n    def _root_dir_default(self):\n        try:\n            return self.parent.root_dir\n        except AttributeError:\n            return getcwd()\n\n    # ContentsManager-dependent checkpoint API\n    def create_checkpoint(self, contents_mgr, path):\n        \"\"\"Create a checkpoint.\"\"\"\n        checkpoint_id = u'checkpoint'\n        src_path = contents_mgr._get_os_path(path)\n        dest_path = self.checkpoint_path(checkpoint_id, path)\n        self._copy(src_path, dest_path)\n        return self.checkpoint_model(checkpoint_id, dest_path)\n\n    def restore_checkpoint(self, contents_mgr, checkpoint_id, path):\n        \"\"\"Restore a checkpoint.\"\"\"\n        src_path = self.checkpoint_path(checkpoint_id, path)\n        dest_path = contents_mgr._get_os_path(path)\n        self._copy(src_path, dest_path)\n\n    # ContentsManager-independent checkpoint API\n    def rename_checkpoint(self, checkpoint_id, old_path, new_path):\n        \"\"\"Rename a checkpoint from old_path to new_path.\"\"\"\n        old_cp_path = self.checkpoint_path(checkpoint_id, old_path)\n        new_cp_path = self.checkpoint_path(checkpoint_id, new_path)\n        if os.path.isfile(old_cp_path):\n            self.log.debug(\n                \"Renaming checkpoint %s -> %s\",\n                old_cp_path,\n                new_cp_path,\n            )\n            with self.perm_to_403():\n                shutil.move(old_cp_path, new_cp_path)\n\n    def delete_checkpoint(self, checkpoint_id, path):\n        \"\"\"delete a file's checkpoint\"\"\"\n        path = path.strip('/')\n        cp_path = self.checkpoint_path(checkpoint_id, path)\n        if not os.path.isfile(cp_path):\n            self.no_such_checkpoint(path, checkpoint_id)\n\n        self.log.debug(\"unlinking %s\", cp_path)\n        with self.perm_to_403():\n            os.unlink(cp_path)\n\n    def list_checkpoints(self, path):\n        \"\"\"list the checkpoints for a given file\n\n        This contents manager currently only supports one checkpoint per file.\n        \"\"\"\n        path = path.strip('/')\n        checkpoint_id = \"checkpoint\"\n        os_path = self.checkpoint_path(checkpoint_id, path)\n        if not os.path.isfile(os_path):\n            return []\n        else:\n            return [self.checkpoint_model(checkpoint_id, os_path)]\n\n    # Checkpoint-related utilities\n    def checkpoint_path(self, checkpoint_id, path):\n        \"\"\"find the path to a checkpoint\"\"\"\n        path = path.strip('/')\n        parent, name = ('/' + path).rsplit('/', 1)\n        parent = parent.strip('/')\n        basename, ext = os.path.splitext(name)\n        filename = u\"{name}-{checkpoint_id}{ext}\".format(\n            name=basename,\n            checkpoint_id=checkpoint_id,\n            ext=ext,\n        )\n        os_path = self._get_os_path(path=parent)\n        cp_dir = os.path.join(os_path, self.checkpoint_dir)\n        with self.perm_to_403():\n            ensure_dir_exists(cp_dir)\n        cp_path = os.path.join(cp_dir, filename)\n        return cp_path\n\n    def checkpoint_model(self, checkpoint_id, os_path):\n        \"\"\"construct the info dict for a given checkpoint\"\"\"\n        stats = os.stat(os_path)\n        last_modified = tz.utcfromtimestamp(stats.st_mtime)\n        info = dict(\n            id=checkpoint_id,\n            last_modified=last_modified,\n        )\n        return info\n\n    # Error Handling\n    def no_such_checkpoint(self, path, checkpoint_id):\n        raise HTTPError(\n            404,\n            u'Checkpoint does not exist: %s@%s' % (path, checkpoint_id)\n        )\n\n\nclass GenericFileCheckpoints(GenericCheckpointsMixin, FileCheckpoints):\n    \"\"\"\n    Local filesystem Checkpoints that works with any conforming\n    ContentsManager.\n    \"\"\"\n    def create_file_checkpoint(self, content, format, path):\n        \"\"\"Create a checkpoint from the current content of a file.\"\"\"\n        path = path.strip('/')\n        # only the one checkpoint ID:\n        checkpoint_id = u\"checkpoint\"\n        os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)\n        self.log.debug(\"creating checkpoint for %s\", path)\n        with self.perm_to_403():\n            self._save_file(os_checkpoint_path, content, format=format)\n\n        # return the checkpoint info\n        return self.checkpoint_model(checkpoint_id, os_checkpoint_path)\n\n    def create_notebook_checkpoint(self, nb, path):\n        \"\"\"Create a checkpoint from the current content of a notebook.\"\"\"\n        path = path.strip('/')\n        # only the one checkpoint ID:\n        checkpoint_id = u\"checkpoint\"\n        os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)\n        self.log.debug(\"creating checkpoint for %s\", path)\n        with self.perm_to_403():\n            self._save_notebook(os_checkpoint_path, nb)\n\n        # return the checkpoint info\n        return self.checkpoint_model(checkpoint_id, os_checkpoint_path)\n\n    def get_notebook_checkpoint(self, checkpoint_id, path):\n        \"\"\"Get a checkpoint for a notebook.\"\"\"\n        path = path.strip('/')\n        self.log.info(\"restoring %s from checkpoint %s\", path, checkpoint_id)\n        os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)\n\n        if not os.path.isfile(os_checkpoint_path):\n            self.no_such_checkpoint(path, checkpoint_id)\n\n        return {\n            'type': 'notebook',\n            'content': self._read_notebook(\n                os_checkpoint_path,\n                as_version=4,\n            ),\n        }\n\n    def get_file_checkpoint(self, checkpoint_id, path):\n        \"\"\"Get a checkpoint for a file.\"\"\"\n        path = path.strip('/')\n        self.log.info(\"restoring %s from checkpoint %s\", path, checkpoint_id)\n        os_checkpoint_path = self.checkpoint_path(checkpoint_id, path)\n\n        if not os.path.isfile(os_checkpoint_path):\n            self.no_such_checkpoint(path, checkpoint_id)\n\n        content, format = self._read_file(os_checkpoint_path, format=None)\n        return {\n            'type': 'file',\n            'content': content,\n            'format': format,\n        }\n", "path": "IPython/html/services/contents/filecheckpoints.py"}]}