Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
Code to retrieve whether we should try to recover or not.
Browse files Browse the repository at this point in the history
  • Loading branch information
fferegrino committed Dec 5, 2018
1 parent 49c58ac commit 4df1667
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
24 changes: 20 additions & 4 deletions jupygit/GitHandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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})
6 changes: 4 additions & 2 deletions jupygit/__init__.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
])


Expand Down
56 changes: 40 additions & 16 deletions jupygit/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === "") {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -109,9 +109,33 @@ 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']) {
alert('Try to recover!');
}
},
url: check_url
});
}

function load_ipython_extension() {
console.log("Loading");
console.log("Loaded Jupygit");
place_button();
check_recovery();
}

return {
Expand Down

0 comments on commit 4df1667

Please sign in to comment.