diff --git a/configs.py b/configs.py
new file mode 100644
index 0000000..d451693
--- /dev/null
+++ b/configs.py
@@ -0,0 +1,105 @@
+import inspect
+from copy import copy
+from lxml import etree
+import templates
+import xmlserialize as xml
+
+bytemplate = {}
+byid = {}
+config_templates = {}
+lastid = 0
+
+def add_config(template_name, cfg):
+ global bytemplate
+ if template_name in bytemplate:
+ bytemplate[template_name].append(cfg)
+ else:
+ bytemplate[template_name] = [cfg]
+
+def read_template(path):
+ global config_templates
+
+ class ConfigTemplate(object):
+ def __init__(self,**kwargs):
+ global byid
+ global lastid
+
+ for k,v in kwargs.items():
+ if hasattr(self,k):
+ setattr(self,k,v)
+ else:
+ raise AttributeError, "%s config has no attribute %s" % (self.__class__.__name__, k)
+ add_config(self.__class__.__name__, self)
+ if not hasattr(self,'id'):
+ lastid += 1
+ self.id = lastid
+ byid[self.id] = self
+
+ def __call__(self,**kwargs):
+ cfg = copy(self)
+ d = dir(self)
+ for k in d:
+ if not k.startswith('_'):
+ v = getattr(self,k)
+ if inspect.ismethod(v):
+ continue
+ setattr(cfg,k,v)
+ for k,v in kwargs.items():
+ if k in d:
+ setattr(cfg,k,v)
+ else:
+ raise AttributeError, "%s config has no attribute %s" % (self.__class__.__name__, k)
+ return cfg
+
+ def __str__(self):
+ if hasattr(self,'template'):
+ r = "<Config for %s>" % self.template
+ else:
+ r = "<Config>"
+ for k in dir(self):
+ if k.startswith('_'):
+ continue
+ v = getattr(self,k)
+ if inspect.ismethod(v):
+ continue
+ r += "\n %s = %s" % (k,v)
+ r += "\n</Config>"
+ return r
+
+ def render(self):
+ return templates.do_render(self)
+
+ def save(self,path,render=True):
+ self.template = self.__class__.__name__
+ if render:
+ xmldata = self.render()
+ else:
+ xmldata = xml.serialize(self)
+ f = open(path,'w')
+ f.write(xmldata)
+ f.close()
+
+ xmldata = etree.parse(path).getroot()
+ name = xmldata.attrib['name']
+ for node in xmldata:
+ if node.text:
+ setattr(ConfigTemplate, node.attrib['name'], node.text)
+ else:
+ setattr(ConfigTemplate, node.attrib['name'], '')
+ ConfigTemplate.__name__ = name
+ config_templates[name] = ConfigTemplate
+
+ return ConfigTemplate
+
+def read_config(path):
+ global config_templates
+ xmldata = open(path).read()
+ xmlcfg = xml.deserialize(xmldata)
+ cls = config_templates[xmlcfg.template]
+ cfg = cls()
+ for name in dir(xmlcfg):
+ if name.startswith('_'):
+ continue
+ v = getattr(xmlcfg,name)
+ setattr(cfg,name,v)
+ return cfg
diff --git a/configsets.py b/configsets.py
index 411ec09..0f6f685 100644
--- a/configsets.py
+++ b/configsets.py
@@ -1,7 +1,7 @@
from os.path import join
from copy import copy
from lxml import etree
-import configtemplate
+import configs
class ConfigSet(dict):
def __init__(self,name):
@@ -41,13 +41,12 @@ class ConfigSet(dict):
def read_configset(dir,filename):
cset = ConfigSet('')
spath = join(dir,'configsets',filename)
- print "| Parsing", spath
xml = etree.parse(spath).getroot()
cset.name = xml.attrib['name']
for item in xml:
cid = item.attrib['id']
fn = item.text
template = item.attrib['for']
- cfg = configtemplate.read_config(join(dir,fn))
+ cfg = configs.read_config(join(dir,fn))
cset[template] = cfg
return cset
diff --git a/configtemplate.py b/configtemplate.py
deleted file mode 100644
index d451693..0000000
--- a/configtemplate.py
+++ /dev/null
@@ -1,105 +0,0 @@
-import inspect
-from copy import copy
-from lxml import etree
-import templates
-import xmlserialize as xml
-
-bytemplate = {}
-byid = {}
-config_templates = {}
-lastid = 0
-
-def add_config(template_name, cfg):
- global bytemplate
- if template_name in bytemplate:
- bytemplate[template_name].append(cfg)
- else:
- bytemplate[template_name] = [cfg]
-
-def read_template(path):
- global config_templates
-
- class ConfigTemplate(object):
- def __init__(self,**kwargs):
- global byid
- global lastid
-
- for k,v in kwargs.items():
- if hasattr(self,k):
- setattr(self,k,v)
- else:
- raise AttributeError, "%s config has no attribute %s" % (self.__class__.__name__, k)
- add_config(self.__class__.__name__, self)
- if not hasattr(self,'id'):
- lastid += 1
- self.id = lastid
- byid[self.id] = self
-
- def __call__(self,**kwargs):
- cfg = copy(self)
- d = dir(self)
- for k in d:
- if not k.startswith('_'):
- v = getattr(self,k)
- if inspect.ismethod(v):
- continue
- setattr(cfg,k,v)
- for k,v in kwargs.items():
- if k in d:
- setattr(cfg,k,v)
- else:
- raise AttributeError, "%s config has no attribute %s" % (self.__class__.__name__, k)
- return cfg
-
- def __str__(self):
- if hasattr(self,'template'):
- r = "<Config for %s>" % self.template
- else:
- r = "<Config>"
- for k in dir(self):
- if k.startswith('_'):
- continue
- v = getattr(self,k)
- if inspect.ismethod(v):
- continue
- r += "\n %s = %s" % (k,v)
- r += "\n</Config>"
- return r
-
- def render(self):
- return templates.do_render(self)
-
- def save(self,path,render=True):
- self.template = self.__class__.__name__
- if render:
- xmldata = self.render()
- else:
- xmldata = xml.serialize(self)
- f = open(path,'w')
- f.write(xmldata)
- f.close()
-
- xmldata = etree.parse(path).getroot()
- name = xmldata.attrib['name']
- for node in xmldata:
- if node.text:
- setattr(ConfigTemplate, node.attrib['name'], node.text)
- else:
- setattr(ConfigTemplate, node.attrib['name'], '')
- ConfigTemplate.__name__ = name
- config_templates[name] = ConfigTemplate
-
- return ConfigTemplate
-
-def read_config(path):
- global config_templates
- xmldata = open(path).read()
- xmlcfg = xml.deserialize(xmldata)
- cls = config_templates[xmlcfg.template]
- cfg = cls()
- for name in dir(xmlcfg):
- if name.startswith('_'):
- continue
- v = getattr(xmlcfg,name)
- setattr(cfg,name,v)
- return cfg
diff --git a/test.py b/test.py
index 7c59676..7dd3656 100755
--- a/test.py
+++ b/test.py
@@ -6,7 +6,7 @@ from copy import copy
import discovery
import templates
-import configtemplate
+import configs
import configsets
# import xmlserialize as XML
@@ -81,7 +81,7 @@ templates.machine = machine
time.sleep(5)
# Read template
-system = configtemplate.read_template('configs/templates/system.xml')
+system = configs.read_template('configs/templates/system.xml')
#####################################
# User interface testing