From 205c200b94f3b4edf1220a9ffd5ebeab58aa098b Mon Sep 17 00:00:00 2001 From: spoob Date: Thu, 7 Jan 2010 19:05:42 +0100 Subject: Better ConfigParser --- module/Plugin.py | 2 +- module/XMLConfigParser.py | 34 ++++++++++++++++++++++++---------- module/config/core_default.xml | 2 +- module/config/plugin_default.xml | 2 +- module/plugins/hooks/Hook.py | 2 +- 5 files changed, 28 insertions(+), 14 deletions(-) (limited to 'module') diff --git a/module/Plugin.py b/module/Plugin.py index 7854aa66d..45371c3af 100644 --- a/module/Plugin.py +++ b/module/Plugin.py @@ -31,7 +31,7 @@ from module.download_thread import CaptchaError class Plugin(): def __init__(self, parent): - self.configparser = XMLConfigParser(join("module","config","plugin.xml"), join("module","config","plugin_default.xml")) + self.configparser = XMLConfigParser(join("module","config","plugin.xml")) self.config = {} props = {} props['name'] = "BasePlugin" 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 diff --git a/module/config/core_default.xml b/module/config/core_default.xml index 366197a61..07d4ddc1c 100644 --- a/module/config/core_default.xml +++ b/module/config/core_default.xml @@ -1,4 +1,4 @@ - + 7227 0.0.0.0 diff --git a/module/config/plugin_default.xml b/module/config/plugin_default.xml index 1ad653251..3f033fc04 100644 --- a/module/config/plugin_default.xml +++ b/module/config/plugin_default.xml @@ -1,4 +1,4 @@ - + False diff --git a/module/plugins/hooks/Hook.py b/module/plugins/hooks/Hook.py index 09b21ba49..739af3200 100644 --- a/module/plugins/hooks/Hook.py +++ b/module/plugins/hooks/Hook.py @@ -26,7 +26,7 @@ from module.XMLConfigParser import XMLConfigParser class Hook(): def __init__(self, core): self.logger = logging.getLogger("log") - self.configParser = XMLConfigParser(join("module","config","plugin.xml"), join("module","config","plugin_default.xml")) + self.configParser = XMLConfigParser(join("module","config","plugin.xml")) self.config = {} props = {} props['name'] = "Hook" -- cgit v1.2.3