Factor out classes conversion code

portnov [2009-02-24 19:36:30]
Factor out classes conversion code
Filename
configs.py
xmlserialize.py
diff --git a/configs.py b/configs.py
index d451693..ba58d95 100644
--- a/configs.py
+++ b/configs.py
@@ -96,10 +96,4 @@ def read_config(path):
     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
+    return xml.convert(xmlcfg,cls)
diff --git a/xmlserialize.py b/xmlserialize.py
index 0240a7a..390de1d 100644
--- a/xmlserialize.py
+++ b/xmlserialize.py
@@ -72,6 +72,16 @@ def deserialize(text):
     xml = etree.parse(StringIO(text))
     return work(xml.getroot())

+def convert(ob,cls):
+    "Convert object (loaded from xml) to given class"
+    res = cls()
+    for name in dir(ob):
+        if name.startswith('_'):
+            continue
+        v = getattr(ob,name)
+        setattr(res,name,v)
+    return res
+
 if __name__ == '__main__':

     a = Storable()
ViewGit