diff --git a/configs/-1222056212.xml b/configs/-1222056212.xml
new file mode 100644
index 0000000..57554c7
--- /dev/null
+++ b/configs/-1222056212.xml
@@ -0,0 +1 @@
+<object><attribute name='dns'>{{services.dns.ip}}</attribute><attribute name='gateway'>{{services.gateway}}</attribute><attribute name='proxy'>{{services.proxy}}</attribute><attribute name='template'>system</attribute></object>
\ No newline at end of file
diff --git a/configs/configsets/first.xml b/configs/configsets/first.xml
new file mode 100644
index 0000000..d403a30
--- /dev/null
+++ b/configs/configsets/first.xml
@@ -0,0 +1,3 @@
+<? xml version='1.0' ?>
+ <configset name='first'><config for='system'>-1222056212.xml</config>
+</configset>
\ No newline at end of file
diff --git a/configsets.py b/configsets.py
new file mode 100644
index 0000000..e792e9c
--- /dev/null
+++ b/configsets.py
@@ -0,0 +1,28 @@
+from os.path import join
+from copy import copy
+# import xmlserialize as xml
+
+class ConfigSet(dict):
+ def __init__(self,name):
+ dict.__init__(self)
+ self.name = name
+
+ def __call__(self,**kwargs):
+ new = copy(self)
+ new.update(kwargs)
+ return new
+
+ def save(self,path):
+ sfn = join(path,'configsets',self.name+'.xml')
+ xmldata = """<? xml version='1.0' ?>
+ <configset name='%s'>""" % self.name
+ for key in self:
+ cfg = self[key]
+ fn = "%s.xml" % hash(cfg)
+ xmldata += "<config for='%s'>%s</config>\n" % (key,fn)
+ cfg.save(join(path,fn), render=False)
+ xmldata += "</configset>"
+ f = open(sfn,'w')
+ f.write(xmldata)
+ f.close()
+
diff --git a/configtemplate.py b/configtemplate.py
index 7819c82..6093d78 100644
--- a/configtemplate.py
+++ b/configtemplate.py
@@ -1,4 +1,17 @@
+import inspect
+from copy import copy
from lxml import etree
+import templates
+import xmlserialize as xml
+
+bytemplate = {}
+
+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):
class ConfigTemplate(object):
@@ -8,13 +21,16 @@ def read_template(path):
setattr(self,k,v)
else:
raise AttributeError, "%s config has no attribute %s" % (self.__class__.__name__, k)
+ add_config(self.__class__.__name__, self)
def __call__(self,**kwargs):
- cfg = ConfigTemplate()
+ 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:
@@ -23,6 +39,19 @@ def read_template(path):
raise AttributeError, "%s config has no attribute %s" % (self.__class__.__name__, k)
return cfg
+ 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:
@@ -33,3 +62,8 @@ def read_template(path):
ConfigTemplate.__name__ = name
return ConfigTemplate
+
+def read_config(path):
+ xmldata = open(path).read()
+ cfg = xml.deserialize(xmldata)
+ return cfg
diff --git a/templates.py b/templates.py
index 411223a..09f8736 100644
--- a/templates.py
+++ b/templates.py
@@ -3,5 +3,6 @@ from jinja2 import Template
import xmlserialize as xml
def do_render(conf):
- return Template(xml.serialize(conf)).render(services=services,machine=machine)
+ xmldata = xml.serialize(conf)
+ return Template(xmldata).render(services=services,machine=machine)
diff --git a/test.py b/test.py
index a2aa3b9..5dbbc3a 100755
--- a/test.py
+++ b/test.py
@@ -7,6 +7,9 @@ from copy import copy
import discovery
import templates
import configtemplate
+import configsets
+
+# import xmlserialize as XML
#####################################
# Internal section
@@ -86,12 +89,15 @@ system = configtemplate.read_template('configs/templates/system.xml')
# Create an instance of system config
scfg = system(proxy='{{services.proxy}}')
print "First config:"
-print templates.do_render(scfg)
+print scfg.render()
# create a copy of scfg, but with different dns setting
-ncfg = scfg(dns='{{services.dns.ip}}')
+ncfg = scfg(dns='{{services.dns}}')
# chage gateway setting
ncfg.gateway = '{{services.gateway}}'
print "Second config:"
-print templates.do_render(ncfg)
+print ncfg.render()
+cset = configsets.ConfigSet('first')
+cset['system'] = ncfg
+cset.save('configs')
diff --git a/xmlserialize.py b/xmlserialize.py
index 55bf79e..0240a7a 100644
--- a/xmlserialize.py
+++ b/xmlserialize.py
@@ -1,3 +1,4 @@
+import inspect
from lxml import etree
from StringIO import StringIO
@@ -31,7 +32,10 @@ def serialize(ob):
for name in dir(ob):
if name.startswith('_'):
continue
- r += "<attribute name='%s'>%s</attribute>" % (name,serialize(getattr(ob,name)))
+ val = getattr(ob,name)
+ if inspect.ismethod(val):
+ continue
+ r += "<attribute name='%s'>%s</attribute>" % (name,serialize(val))
r += '</object>'
return r