Skip to content

Commit

Permalink
Save settings outside the hw folder. (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahularya50 authored and kavigupta committed Apr 6, 2019
1 parent d04f807 commit 422068f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
24 changes: 12 additions & 12 deletions editor/local_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from execution_parser import strip_comments
from file_manager import get_scm_files, save, read_file, new_file
from formatter import prettify
from persistence import save_config, load_config
from runtime_limiter import TimeLimitException, OperationCanceledException, scheme_limiter
from scheme_exceptions import SchemeError, ParseError, TerminatedError

Expand Down Expand Up @@ -125,8 +126,7 @@ def handle_post_thread(self, data, path):
else:
state[key] = val
if "settings" in state:
with open("editor_settings.config", "w+") as file:
file.write(json.dumps(state["settings"]))
save_config("settings", state["settings"])
self.send_response(HTTPStatus.OK, 'test')
self.send_header("Content-type", "application/JSON")
self.end_headers()
Expand All @@ -142,19 +142,19 @@ def handle_post_thread(self, data, path):
self.wfile.write(bytes(json.dumps(state), "utf-8"))

elif path == "/load_settings":
try:
with open("editor_settings.config", "r") as file:
if "settings" not in state:
state["settings"] = {}
for key, val in json.loads(file.read()).items():
state["settings"][key] = val
except FileNotFoundError:
pass

self.send_response(HTTPStatus.OK, 'test')
self.send_header("Content-type", "application/JSON")
self.end_headers()
self.wfile.write(bytes(json.dumps(state["settings"]), "utf-8"))

try:
if "settings" not in state:
state["settings"] = {}
for key, val in load_config("settings").items():
state["settings"][key] = val
except FileNotFoundError:
self.wfile.write(b"fail")
else:
self.wfile.write(bytes(json.dumps(state["settings"]), "utf-8"))


elif path == "/documentation":
Expand Down
17 changes: 17 additions & 0 deletions editor/persistence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import json
import os
from pathlib import Path

home = str(Path.home())
config_path = home + "/.scheme_editor"


def save_config(key, content):
os.makedirs(config_path, exist_ok=True)
with open(config_path + "/" + key + ".config", "w+") as f:
f.write(json.dumps(content))


def load_config(key):
with open(config_path + "/" + key + ".config", "r") as f:
return json.loads(f.read())
3 changes: 3 additions & 0 deletions editor/static/scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ function init() {
});

$.post("./load_settings").done((data) => {
if (data === "fail") {
return;
}
setAllSettings($.parseJSON(data));
});
}
Expand Down

0 comments on commit 422068f

Please sign in to comment.