summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--module/XMLConfigParser.py51
-rw-r--r--module/config/core_default.xml1
-rwxr-xr-xpyLoadCore.py3
3 files changed, 50 insertions, 5 deletions
diff --git a/module/XMLConfigParser.py b/module/XMLConfigParser.py
index 8be2df048..50cc4485d 100644
--- a/module/XMLConfigParser.py
+++ b/module/XMLConfigParser.py
@@ -24,20 +24,25 @@ from xml.dom.minidom import parse
from shutil import copy
class XMLConfigParser():
- def __init__(self, data):
+ def __init__(self, data, forceDefault=False):
self.xml = None
self.version = "0.1"
self.file = data
self.file_default = self.file.replace(".xml", "_default.xml")
+ self.forceDefault = forceDefault
self.config = {}
self.data = {}
self.types = {}
self.loadData()
- self.root = None
+ self.root = self.xml.documentElement
+ if not forceDefault:
+ self.defaultParser = XMLConfigParser(data, True)
def loadData(self):
file = self.file
- if not exists(self.file):
+ if self.forceDefault:
+ file = self.file_default
+ if not exists(self.file) and self.forceDefault:
self._copyConfig()
with open(file, 'r') as fh:
self.xml = parse(fh)
@@ -59,6 +64,8 @@ class XMLConfigParser():
exit() #ok?
def saveData(self):
+ if self.forceDefault:
+ return
with open(self.file, 'w') as fh:
self.xml.writexml(fh)
@@ -99,15 +106,19 @@ class XMLConfigParser():
try:
return self.config[section][option]
except:
- return default
+ if self.forceDefault:
+ return default
+ return self.defaultParser.get(section, option, default)
def getConfig(self):
- return self.config
+ return Config(self)
def set(self, section, data, value):
root = self.root
replace = False
sectionNode = False
+ if type(data) == str:
+ data = {"option": data}
for node in root.childNodes:
if node.nodeType == node.ELEMENT_NODE:
if section == node.tagName:
@@ -183,3 +194,33 @@ class XMLConfigParser():
return option
else:
return section
+
+ def isValidSection(self, section):
+ try:
+ self.config[section]
+ return True
+ except:
+ return False
+
+class Config(object):
+ def __init__(self, parser):
+ self.parser = parser
+
+ def __getitem__(self, key):
+ if self.parser.isValidSection(key):
+ return Section(self.parser, key)
+ raise Exception("invalid section")
+
+ def keys(self):
+ return self.parser.config.keys()
+
+class Section(object):
+ def __init__(self, parser, section):
+ self.parser = parser
+ self.section = section
+
+ def __getitem__(self, key):
+ return self.parser.get(self.section, key)
+
+ def __setitem__(self, key, value):
+ self.parser.set(self.section, key, value)
diff --git a/module/config/core_default.xml b/module/config/core_default.xml
index 8223768ec..0aea08325 100644
--- a/module/config/core_default.xml
+++ b/module/config/core_default.xml
@@ -5,6 +5,7 @@
<listenaddr type="ip" name="Adress">0.0.0.0</listenaddr>
<username type="str" name="Username">admin</username>
<password type="str" name="Password">pwhere</password>
+ <test>bla</test>
</remote>
<ssl name="SSL">
<activated type="bool" name="Activated">False</activated>
diff --git a/pyLoadCore.py b/pyLoadCore.py
index d5b387baf..c2cf6d829 100755
--- a/pyLoadCore.py
+++ b/pyLoadCore.py
@@ -464,6 +464,9 @@ class ServerMethods():
else:
raise Exception("not allowed!")
+ def get_config(self):
+ pass
+
def pause_server(self):
self.core.thread_list.pause = True