Commit 819a9c29 authored by Benjamin Fischer's avatar Benjamin Fischer
Browse files

Reworked workspace.ini read/write to allow multiple values be sat/requested in one call.

 Also retrieve a merge workspace.ini (global & user specific) as requested in ref #1914
parent a6c92de1
......@@ -311,24 +311,21 @@ class FSAjaxController(AbstractController):
@cherrypy.expose
@cherrypy.tools.ajax(encoded=True)
def get_from_public_workspaceini(self, section, option):
def get_workspaceini(self, request, fail_on_missing=False):
self.release_session()
fs = self.get('fs')
self.release_database()
return fs.get_from_public_workspaceini(section, option)
@cherrypy.expose
@cherrypy.tools.ajax(encoded=True)
def get_from_private_workspaceini(self, section, option):
self.release_session()
fs = self.get('fs')
self.release_database()
return fs.get_from_private_workspaceini(section, option)
fail_on_missing = self.convert(fail_on_missing, bool)
return fs.get_workspaceini(request, fail_on_missing=fail_on_missing)
@cherrypy.expose
@cherrypy.tools.ajax(encoded=True)
def set_to_private_workspaceini(self, section, option, value):
@cherrypy.tools.ajax()
def set_workspaceini(self, request):
self.release_session()
fs = self.get('fs')
self.release_database()
return fs.set_to_private_workspaceini(section, option, value)
\ No newline at end of file
err = fs.set_workspaceini(request)
if err:
raise MessageException(err)
return {"success": not err}
\ No newline at end of file
......@@ -21,170 +21,132 @@ var BookmarkContainer = Class.extend({
setContent: function() {
var self = this;
// get names from workspaceini
var promise1 = self.instance.GET("/ajax/fs/get_from_private_workspaceini", {
"section": "Bookmarks",
"option": "names"
});
promise1.done(function(names) {
// then get pathes from workspaceini
var promise2 = self.instance.GET("/ajax/fs/get_from_private_workspaceini", {
"section": "Bookmarks",
"option": "pathes"
});
promise2.done(function(pathes) {
if (!names.success || !pathes.success) {
self.instance.POST("/ajax/fs/get_workspaceini",{
request: JSON.stringify({
Bookmarks:['names','paths']
})
}).done(function(data) {
if (!data.success) {
self.names = [];
self.paths = [];
self.instance.alert("Can't read workspace.ini.");
} else {
// split into arrays
self.names = data.content.Bookmarks.names.split(":");
self.paths = data.content.Bookmarks.paths.split(":");
if (self.names.join() == "") {
self.names = [];
self.pathes = [];
self.instance.alert("Can't read private workspace.ini.");
} else {
// split into arrays
self.names = names.content.split(":");
self.pathes = pathes.content.split(":");
if (self.names.join() == "") {
self.names = [];
self.pathes = [];
}
}
if (self.names.length !== self.pathes.length) {
self.paths = [];
} else if (self.names.length !== self.paths.length) {
self.names = [];
self.pathes = [];
self.paths = [];
self.instance.alert("Bookmarks in workspace.ini may be broken.");
}
// set directives and render
var directives = {
"bookmark-name": {
"text": function() {
return this.value;
}
}
// set directives and render
var directives = {
"bookmark-name": {
"text": function() {
return this.value;
}
};
$(".bookmarks", self.parentnode).render(self.names, directives);
// invisible if empty
if (self.names.length == 0) $(".bookmarks", self.parentnode).css({
"visibility": "hidden"
});
else $(".bookmarks", self.parentnode).css({
"visibility": "visible"
});
}
};
$(".bookmarks", self.parentnode).render(self.names, directives);
// invisible if empty
if (self.names.length == 0) $(".bookmarks", self.parentnode).css({
"visibility": "hidden"
});
else $(".bookmarks", self.parentnode).css({
"visibility": "visible"
});
// add clicks
var clicktype = vispa.device.hasTouch ? "tap" : "click";
// button click
var node = $(".bookmark-button", self.parentnode);
node.off();
node.on(clicktype, function(event) {
// add clicks
var clicktype = vispa.device.hasTouch ? "tap" : "click";
// button click
var node = $(".bookmark-button", self.parentnode);
node.off();
node.on(clicktype, function(event) {
event.stopPropagation();
self.addBookmark();
});
// bookmark clicks
$.each($(".bookmark", self.parentnode), function(index, node) {
// click on bookmark self
$(node).off();
$(node).on(clicktype, function(event) {
event.stopPropagation();
self.addBookmark();
self.FileBase.workflow.path = self.paths[index];
self.FileBase.updateView();
});
// bookmark clicks
$.each($(".bookmark", self.parentnode), function(index, node) {
// click on bookmark self
$(node).off();
$(node).on(clicktype, function(event) {
event.stopPropagation();
self.FileBase.workflow.path = self.pathes[index];
self.FileBase.updateView();
});
// click on remove glyph
$(".bookmark-glyphicon-remove", node).off();
$(".bookmark-glyphicon-remove", node).on(clicktype, function(event) {
event.stopPropagation();
self.removeBookmark(index);
});
// click on edit glyph
$(".bookmark-glyphicon-edit", node).off();
$(".bookmark-glyphicon-edit", node).on(clicktype, function(event) {
event.stopPropagation();
self.editBookmark(index);
});
// click on remove glyph
$(".bookmark-glyphicon-remove", node).off();
$(".bookmark-glyphicon-remove", node).on(clicktype, function(event) {
event.stopPropagation();
self.removeBookmark(index);
});
// click on edit glyph
$(".bookmark-glyphicon-edit", node).off();
$(".bookmark-glyphicon-edit", node).on(clicktype, function(event) {
event.stopPropagation();
self.editBookmark(index);
});
});
});
},
saveBookmarks: function() {
return this.instance.POST("/ajax/fs/set_workspaceini", {
request: JSON.stringify({
Bookmarks:{
names: this.names.join(":"),
paths: this.paths.join(":")
}
})
})
},
addBookmark: function() {
var self = this;
// add data
self.pathes[self.pathes.length] = self.FileBase.workflow.path;
self.names[self.names.length] = self.FileBase.helper.filenameFromPath(self.pathes[self.pathes.length - 1]);
if (self.names[self.names.length - 1] == "") self.names[self.names.length - 1] = "New bookmark";
this.paths[this.paths.length] = this.FileBase.workflow.path;
this.names[this.names.length] = this.FileBase.helper.filenameFromPath(this.paths[this.paths.length - 1]);
if (this.names[this.names.length - 1] == "") this.names[this.names.length - 1] = "New bookmark";
// set new data to ini and update view
var promise1 = self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "names",
"value": self.names.join(":")
});
promise1.done(function(res1) {
var promise2 = self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "pathes",
"value": self.pathes.join(":")
});
promise2.done(function(res2) {
if (res1.success && res2.success) {
self.setContent();
self.editBookmark(self.names.length - 1);
} else {
self.instance.alert("Couldn't add bookmark.");
self.names.splice(self.names.length - 1, 1);
self.pathes.splice(self.pathes.length - 1, 1);
self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "names",
"value": self.names.join(":")
});
self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "pathes",
"value": self.pathes.join(":")
});
}
});
this.saveBookmarks().done(function(res) {
if (res.success) {
self.setContent();
self.editBookmark(self.names.length - 1);
} else {
self.instance.alert("Couldn't add bookmark.");
self.names.splice(self.names.length - 1, 1);
self.paths.splice(self.paths.length - 1, 1);
self.saveBookmarks();
}
});
},
removeBookmark: function(index) {
// delete entry in arrays
var self = this;
var name = self.names[index];
var path = self.pathes[index];
self.instance.confirm(
var name = this.names[index];
var path = this.paths[index];
this.instance.confirm(
"Do you want to delete bookmark \"" + name + "\"?",
function(confirmed) {
if (confirmed) {
self.names.splice(index, 1);
self.pathes.splice(index, 1);
self.paths.splice(index, 1);
// set new data to ini and update view
var promise1 = self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "names",
"value": self.names.join(":")
});
promise1.done(function(res1) {
var promise2 = self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "pathes",
"value": self.pathes.join(":")
});
promise2.done(function(res2) {
if (res1.success && res2.success) {
self.setContent();
} else {
self.instance.alert("Couldn't remove bookmark.");
self.names.splice(index, 0, name);
self.pathes.splice(index, 0, path);
self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "names",
"value": self.names.join(":")
});
self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "pathes",
"value": self.pathes.join(":")
});
}
});
self.saveBookmarks().done(function(res) {
if (res.success) {
self.setContent();
} else {
self.instance.alert("Couldn't remove bookmark.");
self.names.splice(index, 0, name);
self.paths.splice(index, 0, path);
self.saveBookmarks();
}
});
} else {
return;
......@@ -195,7 +157,7 @@ var BookmarkContainer = Class.extend({
editBookmark: function(index) {
var self = this;
// prompt for new data
self.instance.prompt(
this.instance.prompt(
"Enter: position + \":\" + name",
function(newdata) {
var newindex = parseInt(newdata.split(":")[0], 10);
......@@ -214,45 +176,24 @@ var BookmarkContainer = Class.extend({
var oldname = self.names[index];
self.names[index] = newname;
var name = self.names[index];
var path = self.pathes[index];
var path = self.paths[index];
self.names.splice(index, 1);
self.names.splice(newindex, 0, name);
self.pathes.splice(index, 1);
self.pathes.splice(newindex, 0, path);
self.paths.splice(index, 1);
self.paths.splice(newindex, 0, path);
// set new data to ini and update view
var promise1 = self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "names",
"value": self.names.join(":")
});
promise1.done(function(res1) {
var promise2 = self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "pathes",
"value": self.pathes.join(":")
});
promise2.done(function(res2) {
if (res1.success && res2.success) {
self.setContent();
} else {
self.instance.alert("Couldn't edit bookmark.");
self.names.splice(newindex, 1);
self.names.splice(index, 0, name);
self.pathes.splice(newindex, 1);
self.pathes.splice(index, 0, path);
self.names[index] = oldname;
self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "names",
"value": self.names.join(":")
});
self.instance.POST("/ajax/fs/set_to_private_workspaceini", {
"section": "Bookmarks",
"option": "pathes",
"value": self.pathes.join(":")
});
}
});
self.saveBookmarks().done(function(res) {
if (res) {
self.setContent();
} else {
self.instance.alert("Couldn't edit bookmark.");
self.names.splice(newindex, 1);
self.names.splice(index, 0, name);
self.paths.splice(newindex, 1);
self.paths.splice(index, 0, path);
self.names[index] = oldname;
self.saveBookmarks();
}
});
}, {
defaultValue: String(index + 1) + ":" + self.names[index]
......
......@@ -608,55 +608,60 @@ class FileSystem(object):
return ""
def get_from_public_workspaceini(self, section, option):
def get_workspaceini(self, request, fail_on_missing=False):
try:
request_dict = json.loads(request)
config = ConfigParser.ConfigParser()
config.read("/etc/vispa/workspace.ini")
config.read(["/etc/vispa/workspace.ini",
self.expand("$HOME/.vispa/workspace.ini")])
if not isinstance(request_dict, dict):
request_dict = {section:True for section in config.sections()}
data = {}
for section, name_list in request_dict.iteritems():
if config.has_section(section):
if isinstance(name_list, basestring):
name_list = [name_list]
if not isinstance(name_list, list):
name_list = dict(config.items(section))
else:
data[section] = {}
for name in name_list:
if config.has_option(section, name):
data[section][name] = config.get(section, name)
elif fail_on_missing:
raise Exception('workspace.ini is missing the option "%s" in section [%s] ' % (name, section))
elif fail_on_missing:
raise Exception('workspace.ini is missing the section [%s]' % section)
return json.dumps({
"content": config.get(section, option),
"content": data,
"success": True
})
except Exception as e:
return json.dumps({
"content": str(e),
"success": False
})
def get_from_private_workspaceini(self, section, option):
try:
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser(os.path.expandvars("$HOME/.vispa/workspace.ini")))
return json.dumps({
"content": config.get(section, option),
"success": True
})
except Exception as e:
return json.dumps({
"content": str(e),
"success": False
"content": "",
"success": False,
"error": str(e)
})
def set_to_private_workspaceini(self, section, option, value):
def set_workspaceini(self, request):
try:
request_dict = json.loads(request)
if not isinstance(request_dict, dict):
raise Exception('Given values to be set in workspace.ini in wrong format')
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser(os.path.expandvars("$HOME/.vispa/workspace.ini")))
if (config.has_section(section)):
config.set(section, option, value)
else:
config.add_section(section)
config.set(section, option, value)
workspaceini = open(os.path.expanduser(os.path.expandvars("$HOME/.vispa/workspace.ini")), "w")
config.write(workspaceini)
workspaceini.close()
return json.dumps({
"message": "Written successfully",
"success": True
})
config.read(self.expand("$HOME/.vispa/workspace.ini"))
for section, options in request_dict.iteritems():
if not isinstance(options, dict):
raise Exception('Given values to be set in workspace.ini in wrong format')
if not config.has_section(section):
config.add_section(section)
for name, value in options.iteritems():
config.set(section, name, value)
with open(self.expand("$HOME/.vispa/workspace.ini"), 'w') as f:
config.write(f)
return ""
except Exception as e:
return json.dumps({
"message": str(e),
"success": False
})
return str(e)
def string_compare(a, b):
if a == b:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment