From d08271cbb66f3ccbd8f3c5cf707008388ff4297e Mon Sep 17 00:00:00 2001 From: mkaay Date: Thu, 17 Dec 2009 21:33:13 +0100 Subject: new xml config for core --- module/XMLConfigParser.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 module/XMLConfigParser.py (limited to 'module/XMLConfigParser.py') diff --git a/module/XMLConfigParser.py b/module/XMLConfigParser.py new file mode 100644 index 000000000..1bd5e3417 --- /dev/null +++ b/module/XMLConfigParser.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 -*- +""" + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . + + @author: mkaay +""" + +from xml.dom.minidom import parse + +class XMLConfigParser(): + def __init__(self, data): + self.xml = None + self.file = data + self.config = {} + self.loadData() + self.root = None + + def loadData(self): + with open(self.file, 'r') as fh: + self.xml = parse(fh) + self._read_config() + + def saveData(self): + with open(self.file, 'w') as fh: + self.xml.writexml(fh) + + def _read_config(self): + def format(val): + if val.lower() == "true": + return True + elif val.lower() == "false": + return False + else: + return val + root = self.xml.documentElement + self.root = root + config = {} + for node in root.childNodes: + if node.nodeType == node.ELEMENT_NODE: + section = node.tagName + config[section] = {} + for opt in node.childNodes: + if opt.nodeType == opt.ELEMENT_NODE: + config[section][opt.tagName] = format(opt.firstChild.data) + self.config = config + + def get(self, section, option, default=None): + try: + return self.config[section][option] + except: + return default + + def getConfig(self): + return self.config + + def set(self, section, option, value): + root = self.root + replace = False + sectionNode = False + for node in root.childNodes: + if node.nodeType == node.ELEMENT_NODE: + if section == node.tagName: + sectionNode = node + for opt in node.childNodes: + if opt.nodeType == opt.ELEMENT_NODE: + if option == opt.tagName: + replace = opt + text = self.createTextNode(value) + if replace: + replace.replaceChild(text, replace.firstChild) + else: + newNode = self.createElement(option) + newNode.appendChild(text) + if sectionNode: + sectionNode.appendChild(newNode) + else: + newSection = self.createElement(section) + newSection.appendChild(newNode) + root.appendChild(newSection) + self.saveData() + self.loadData() -- cgit v1.2.3 From e17c410d4c0fcd6c9c59368e1472cdc524617dc1 Mon Sep 17 00:00:00 2001 From: mkaay Date: Thu, 17 Dec 2009 21:53:59 +0100 Subject: fixed with statement (python2.5) --- module/XMLConfigParser.py | 1 + 1 file changed, 1 insertion(+) (limited to 'module/XMLConfigParser.py') diff --git a/module/XMLConfigParser.py b/module/XMLConfigParser.py index 1bd5e3417..b46f9c959 100644 --- a/module/XMLConfigParser.py +++ b/module/XMLConfigParser.py @@ -15,6 +15,7 @@ @author: mkaay """ +from __future__ import with_statement from xml.dom.minidom import parse -- cgit v1.2.3 From 5ee3579572b60bb8f9e6475a517d69462b0cfe29 Mon Sep 17 00:00:00 2001 From: mkaay Date: Wed, 23 Dec 2009 21:04:06 +0100 Subject: download speed limit --- module/XMLConfigParser.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'module/XMLConfigParser.py') diff --git a/module/XMLConfigParser.py b/module/XMLConfigParser.py index b46f9c959..51741fdfd 100644 --- a/module/XMLConfigParser.py +++ b/module/XMLConfigParser.py @@ -17,18 +17,24 @@ """ from __future__ import with_statement +from os.path import exists + from xml.dom.minidom import parse class XMLConfigParser(): - def __init__(self, data): + def __init__(self, data, default_data=None): self.xml = None self.file = data + self.file_default = default_data self.config = {} self.loadData() self.root = None def loadData(self): - with open(self.file, 'r') as fh: + file = self.file + if not exists(self.file): + file = self.file_default + with open(file, 'r') as fh: self.xml = parse(fh) self._read_config() -- cgit v1.2.3 From 15841561a8e5650d88e4af477b8e4f8f96a81253 Mon Sep 17 00:00:00 2001 From: mkaay Date: Sat, 26 Dec 2009 21:17:45 +0100 Subject: pluginconfig now in xml --- module/XMLConfigParser.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'module/XMLConfigParser.py') diff --git a/module/XMLConfigParser.py b/module/XMLConfigParser.py index 51741fdfd..eedc00b67 100644 --- a/module/XMLConfigParser.py +++ b/module/XMLConfigParser.py @@ -59,7 +59,10 @@ class XMLConfigParser(): config[section] = {} for opt in node.childNodes: if opt.nodeType == opt.ELEMENT_NODE: - config[section][opt.tagName] = format(opt.firstChild.data) + try: + config[section][opt.tagName] = format(opt.firstChild.data) + except: + config[section][opt.tagName] = "" self.config = config def get(self, section, option, default=None): -- cgit v1.2.3 From 4ead56475df252c3783701ddf27be9abbd410ad3 Mon Sep 17 00:00:00 2001 From: mkaay Date: Sat, 2 Jan 2010 15:32:29 +0100 Subject: new xmlparser --- module/XMLConfigParser.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'module/XMLConfigParser.py') diff --git a/module/XMLConfigParser.py b/module/XMLConfigParser.py index eedc00b67..444d45928 100644 --- a/module/XMLConfigParser.py +++ b/module/XMLConfigParser.py @@ -27,6 +27,8 @@ class XMLConfigParser(): self.file = data self.file_default = default_data self.config = {} + self.data = {} + self.types = {} self.loadData() self.root = None @@ -53,17 +55,27 @@ class XMLConfigParser(): root = self.xml.documentElement self.root = root config = {} + data = {} for node in root.childNodes: if node.nodeType == node.ELEMENT_NODE: section = node.tagName config[section] = {} + data[section] = {} + data[section]["options"] = {} + data[section]["name"] = node.getAttribute("name") for opt in node.childNodes: if opt.nodeType == opt.ELEMENT_NODE: + data[section]["options"][opt.tagName] = {} try: config[section][opt.tagName] = format(opt.firstChild.data) + data[section]["options"][opt.tagName]["value"] = format(opt.firstChild.data) except: config[section][opt.tagName] = "" + data[section]["options"][opt.tagName]["name"] = opt.getAttribute("name") + data[section]["options"][opt.tagName]["type"] = opt.getAttribute("type") + data[section]["options"][opt.tagName]["input"] = opt.getAttribute("input") self.config = config + self.data = data def get(self, section, option, default=None): try: @@ -100,3 +112,27 @@ class XMLConfigParser(): root.appendChild(newSection) self.saveData() self.loadData() + + def getType(self, section, option): + try: + return self.data[section]["options"][option]["type"] + except: + return "str" + + def getInputValues(self, section, option): + try: + return self.data[section]["options"][option]["input"].split(";") + except: + return [] + + def getDisplayName(self, section, option=None): + try: + if option: + return self.data[section]["options"][option]["name"] + else: + return self.data[section]["name"] + except: + if option: + return option + else: + return section -- cgit v1.2.3 From 3e3ba5240d20a5eb8df669272cb0f3d838d8fcfa Mon Sep 17 00:00:00 2001 From: spoob Date: Sun, 3 Jan 2010 14:58:28 +0100 Subject: Full Config File --- module/XMLConfigParser.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'module/XMLConfigParser.py') diff --git a/module/XMLConfigParser.py b/module/XMLConfigParser.py index 444d45928..0d2094dae 100644 --- a/module/XMLConfigParser.py +++ b/module/XMLConfigParser.py @@ -85,6 +85,9 @@ class XMLConfigParser(): def getConfig(self): return self.config + + def getData(self): + return self.data def set(self, section, option, value): root = self.root -- cgit v1.2.3 From d2aa3fceb9f896343b128d40f20a0465cf69efca Mon Sep 17 00:00:00 2001 From: mkaay Date: Wed, 6 Jan 2010 22:11:24 +0100 Subject: small fixes, new hook stuff --- module/XMLConfigParser.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'module/XMLConfigParser.py') diff --git a/module/XMLConfigParser.py b/module/XMLConfigParser.py index 0d2094dae..5b1966152 100644 --- a/module/XMLConfigParser.py +++ b/module/XMLConfigParser.py @@ -101,16 +101,16 @@ class XMLConfigParser(): if opt.nodeType == opt.ELEMENT_NODE: if option == opt.tagName: replace = opt - text = self.createTextNode(value) + text = self.xml.createTextNode(str(value)) if replace: replace.replaceChild(text, replace.firstChild) else: - newNode = self.createElement(option) + newNode = self.xml.createElement(option) newNode.appendChild(text) if sectionNode: sectionNode.appendChild(newNode) else: - newSection = self.createElement(section) + newSection = self.xml.createElement(section) newSection.appendChild(newNode) root.appendChild(newSection) self.saveData() -- cgit v1.2.3 From 205c200b94f3b4edf1220a9ffd5ebeab58aa098b Mon Sep 17 00:00:00 2001 From: spoob Date: Thu, 7 Jan 2010 19:05:42 +0100 Subject: Better ConfigParser --- module/XMLConfigParser.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'module/XMLConfigParser.py') diff --git a/module/XMLConfigParser.py b/module/XMLConfigParser.py index 5b1966152..8105eb929 100644 --- a/module/XMLConfigParser.py +++ b/module/XMLConfigParser.py @@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, see . - @author: mkaay + @author: mkaay, spoob """ from __future__ import with_statement @@ -21,11 +21,14 @@ from os.path import exists from xml.dom.minidom import parse +from shutil import copy + class XMLConfigParser(): - def __init__(self, data, default_data=None): + def __init__(self, data): self.xml = None + self.version = "0.1" self.file = data - self.file_default = default_data + self.file_default = self.file.replace(".xml", "_default.xml") self.config = {} self.data = {} self.types = {} @@ -35,15 +38,29 @@ class XMLConfigParser(): def loadData(self): file = self.file if not exists(self.file): - file = self.file_default + self._copyConfig() with open(file, 'r') as fh: self.xml = parse(fh) + if not self.xml.documentElement.getAttribute("version") == self.version: + self._copyConfig() + with open(file, 'r') as fh: + self.xml = parse(fh) + if not self.xml.documentElement.getAttribute("version") == self.version: + print "Cant Update %s" % self.file + exit() #ok? self._read_config() - + + def _copyConfig(self): + try: + copy(self.file_default, self.file) + except: + print "%s not found" % self.file_default + exit() #ok? + def saveData(self): with open(self.file, 'w') as fh: self.xml.writexml(fh) - + def _read_config(self): def format(val): if val.lower() == "true": @@ -85,10 +102,7 @@ class XMLConfigParser(): def getConfig(self): return self.config - - def getData(self): - return self.data - + def set(self, section, option, value): root = self.root replace = False -- cgit v1.2.3 From a0fec87a584a029854fc7eec4773c1074bf21b80 Mon Sep 17 00:00:00 2001 From: spoob Date: Sat, 9 Jan 2010 17:15:45 +0100 Subject: Hooks can set Attributes --- module/XMLConfigParser.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'module/XMLConfigParser.py') diff --git a/module/XMLConfigParser.py b/module/XMLConfigParser.py index 8105eb929..5f58c834b 100644 --- a/module/XMLConfigParser.py +++ b/module/XMLConfigParser.py @@ -103,7 +103,7 @@ class XMLConfigParser(): def getConfig(self): return self.config - def set(self, section, option, value): + def set(self, section, data, value): root = self.root replace = False sectionNode = False @@ -113,13 +113,14 @@ class XMLConfigParser(): sectionNode = node for opt in node.childNodes: if opt.nodeType == opt.ELEMENT_NODE: - if option == opt.tagName: + if data["option"] == opt.tagName: replace = opt + self._setAttributes(node, data) text = self.xml.createTextNode(str(value)) if replace: - replace.replaceChild(text, replace.firstChild) + replace.replaceChild(text, replace.firstChild) else: - newNode = self.xml.createElement(option) + newNode = self.xml.createElement(data["option"]) newNode.appendChild(text) if sectionNode: sectionNode.appendChild(newNode) @@ -127,8 +128,19 @@ class XMLConfigParser(): newSection = self.xml.createElement(section) newSection.appendChild(newNode) root.appendChild(newSection) + self._setAttributes(newSection, data) self.saveData() self.loadData() + + def _setAttributes(self, node, data): + node.setAttribute("name", node.tagName) + option = node.getElementsByTagName(data["option"])[0] + option.setAttribute("name", data["name"]) + option.setAttribute("type", data["type"]) + try: + option.setAttribute("input", data["input"]) + except: + pass def getType(self, section, option): try: -- cgit v1.2.3 From ad532495edc4103129bf86b53dec81038babe063 Mon Sep 17 00:00:00 2001 From: spoob Date: Sat, 9 Jan 2010 22:05:15 +0100 Subject: Hooks Config Incomplete --- module/XMLConfigParser.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'module/XMLConfigParser.py') diff --git a/module/XMLConfigParser.py b/module/XMLConfigParser.py index 5f58c834b..678338b41 100644 --- a/module/XMLConfigParser.py +++ b/module/XMLConfigParser.py @@ -48,6 +48,7 @@ class XMLConfigParser(): if not self.xml.documentElement.getAttribute("version") == self.version: print "Cant Update %s" % self.file exit() #ok? + self.root = self.xml.documentElement self._read_config() def _copyConfig(self): @@ -115,7 +116,6 @@ class XMLConfigParser(): if opt.nodeType == opt.ELEMENT_NODE: if data["option"] == opt.tagName: replace = opt - self._setAttributes(node, data) text = self.xml.createTextNode(str(value)) if replace: replace.replaceChild(text, replace.firstChild) @@ -128,13 +128,12 @@ class XMLConfigParser(): newSection = self.xml.createElement(section) newSection.appendChild(newNode) root.appendChild(newSection) - self._setAttributes(newSection, data) + self._setAttributes(section, data) self.saveData() self.loadData() def _setAttributes(self, node, data): - node.setAttribute("name", node.tagName) - option = node.getElementsByTagName(data["option"])[0] + option = self.root.getElementsByTagName(node)[0].getElementsByTagName(data["option"])[0] option.setAttribute("name", data["name"]) option.setAttribute("type", data["type"]) try: -- cgit v1.2.3