diff --git a/jupygit/GitHandlers.py b/jupygit/GitHandlers.py index 4d21c19..abbdf18 100644 --- a/jupygit/GitHandlers.py +++ b/jupygit/GitHandlers.py @@ -7,29 +7,31 @@ from .gitignore_manipulator import add_gitignore_entry from .nb_manipulator import clean_nb +FILE_EXTENSION = ".ipynb" +FILE_SUFFIX = "-jupygit___.ipynb" +EXTENSION = slice(None, -6) + class GitRestoreHandler(IPythonHandler): - file_suffix = "-jupygit___.ipynb" def post(self): data = parse_qs(self.request.body.decode('utf8')) dirty_path = data["path"][0] - clean_path = dirty_path[:-len(self.file_suffix)] + ".ipynb" + clean_path = dirty_path[:-len(FILE_SUFFIX)] + FILE_EXTENSION os.remove(clean_path) self.set_status(200) class GitCleanHandler(IPythonHandler): - file_suffix = "-jupygit___.ipynb" def post(self): data = parse_qs(self.request.body.decode('utf8')) clean_path = data["path"][0] add_gitignore_entry(os.path.dirname(clean_path)) - dirty_path = clean_path[:-6] + self.file_suffix + dirty_path = clean_path[EXTENSION] + FILE_SUFFIX with open(dirty_path, "r") as r: dirty = json.load(r) @@ -41,3 +43,17 @@ def post(self): w.write("\n") # Fix for the new line issue self.set_status(200) + + +class GitCheckRecoveryHandler(IPythonHandler): + + def get(self): + notebook_path = self.get_argument('path') + + attempt_to_recover = False + + if notebook_path.endswith(FILE_SUFFIX): + if os.path.exists(notebook_path.replace(FILE_SUFFIX, FILE_EXTENSION)): + attempt_to_recover = True + + self.write({'try_to_recover': attempt_to_recover}) diff --git a/jupygit/__init__.py b/jupygit/__init__.py index 4375c60..acf975c 100644 --- a/jupygit/__init__.py +++ b/jupygit/__init__.py @@ -1,6 +1,6 @@ from notebook.utils import url_path_join -from .GitHandlers import GitCleanHandler, GitRestoreHandler +from .GitHandlers import GitCleanHandler, GitRestoreHandler, GitCheckRecoveryHandler def load_jupyter_server_extension(nb_server_app): @@ -10,7 +10,9 @@ def load_jupyter_server_extension(nb_server_app): (url_path_join(web_app.settings['base_url'], r'/git/clean'), GitCleanHandler), (url_path_join(web_app.settings['base_url'], r'/git/restore'), - GitRestoreHandler) + GitRestoreHandler), + (url_path_join(web_app.settings['base_url'], r'/git/check'), + GitCheckRecoveryHandler) ]) diff --git a/jupygit/static/index.js b/jupygit/static/index.js index 658a3eb..e6acb60 100644 --- a/jupygit/static/index.js +++ b/jupygit/static/index.js @@ -9,11 +9,24 @@ define([ Jupyter.original_name = ""; + function getCookies(cookie) { + var dictionary = {}; + var parts = cookie.split(";") + + parts.forEach(function(s) { + var trimmed = s.trim(); + var i = trimmed.indexOf("="); + dictionary[trimmed.slice(0, i)] = trimmed.slice(i + 1) + }); + + return dictionary; + } + function make_request() { var clean_url = utils.url_path_join(utils.get_body_data('baseUrl'), 'git/clean') var restore_url = utils.url_path_join(utils.get_body_data('baseUrl'), 'git/restore') + var cookies = getCookies(document.cookie); - var _xsrf = cookies["_xsrf"]; var notebook_path = Jupyter.notebook.notebook_path; if (Jupyter.original_name === "") { @@ -52,7 +65,7 @@ define([ make_request(); }) }, - error: function (jqXHR, textStatus, errorThrown) { + error: function(jqXHR, textStatus, errorThrown) { Jupyter.notebook.rename(Jupyter.original_name).then(function() { Jupyter.original_name = ""; alert("Something went wrong while cleaning your notebook"); @@ -81,19 +94,6 @@ define([ } } - function getCookies(cookie) { - var dictionary = {}; - var parts = cookie.split(";") - - parts.forEach(function(s) { - var trimmed = s.trim(); - var i = trimmed.indexOf("="); - dictionary[trimmed.slice(0, i)] = trimmed.slice(i + 1) - }); - - return dictionary; - } - function place_button() { if (!Jupyter.toolbar) { $([Jupyter.events]).on("app_initialized.NotebookApp", place_button); @@ -109,9 +109,55 @@ define([ }]) } + function check_recovery() { + + var check_url = utils.url_path_join(utils.get_body_data('baseUrl'), 'git/check?name=%s&path=%s'); + var cookies = getCookies(document.cookie); + + $.ajax({ + type: "GET", + data: { + name: Jupyter.notebook.notebook_name, + path: Jupyter.notebook.notebook_path + }, + headers: { + "X-XSRFToken": cookies["_xsrf"] + }, + success: function(d) { + if (d['try_to_recover']) { + + + var modal = dialog.modal({ + backdrop: 'static', + keyboard: false, + show: true, + title: Jupyter.notebook.notebook_name + ' seems dirty!', + body: 'Do you want to restore your notebook\'s name?', + buttons: { + 'Recover' : { + class: 'btn-primary btn-large', + click: function () { + nbname = Jupyter.notebook.notebook_name; + Jupyter.original_name = nbname.substring(0, nbname.length - file_suffix.length); + make_request(); + } + }, + 'Close': { + class: 'btn-default btn-large' + } + } + }); + + } + }, + url: check_url + }); + } + function load_ipython_extension() { - console.log("Loading"); + console.log("Loaded Jupygit"); place_button(); + check_recovery(); } return {