Skip to content

Commit

Permalink
make xml backend loader worked although it nees a lot more work
Browse files Browse the repository at this point in the history
  • Loading branch information
ssato committed Oct 25, 2014
1 parent f17d85d commit 54f22c6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
48 changes: 33 additions & 15 deletions anyconfig/backend/tests/xml_.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (C) 2012 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#
import anyconfig.backend.xml_ as T
import anyconfig.backend.xml_ as TT

import os
import tempfile
Expand All @@ -12,7 +12,7 @@
CONF_0 = """<?xml version="1.0" encoding="UTF-8"?>
<config name='foo'>
<a>0</a>
<b>"bbb"</b>
<b id="b0">bbb</b>
<sect0>
<c>x, y, z</c>
</sect0>
Expand All @@ -31,27 +31,45 @@ def tearDown(self):
os.remove(self.config_path)

def test_00_supports(self):
self.assertFalse(T.XmlConfigParser.supports("/a/b/c/d.ini"))
self.assertTrue(T.XmlConfigParser.supports("/a/b/c/d.xml"))
self.assertFalse(TT.XmlConfigParser.supports("/a/b/c/d.ini"))
self.assertTrue(TT.XmlConfigParser.supports("/a/b/c/d.xml"))

def test_10_loads(self):
"""FIXME: Implement test cases for XmlConfigParser.loads"""
return

c = T.XmlConfigParser.loads(CONF_0)["config"]

self.assertEquals(c["children"][0]['a'], 0, str(c))
self.assertEquals(c["children"][0]['b'], "bbb", c)
self.assertEquals(c["attributes"][0]['name'], "foo", c)

# FIXME: Needs to implement list parser ?
# self.assertEquals(c.sect0.c, ['x', 'y', 'z'])
c = TT.XmlConfigParser.loads(CONF_0)
# container = TT.XmlConfigParser.container()

self.assertTrue("config" in c)
self.assertTrue("attrs" in c["config"])
self.assertTrue("name" in c["config"]["attrs"])
self.assertEquals(c["config"]["attrs"].get("name", None), "foo")

self.assertTrue(isinstance(c["config"]["children"], list))
self.assertNotEquals(c["config"]["children"], [])

self.assertTrue('a' in c["config"]["children"][0])
self.assertTrue("text" in c["config"]["children"][0]['a'])
self.assertTrue("attrs" not in c["config"]["children"][0]['a'])
self.assertEquals(c["config"]["children"][0]['a']["text"], '0')

self.assertTrue('b' in c["config"]["children"][1])
self.assertTrue("text" in c["config"]["children"][1]['b'])
self.assertTrue("attrs" in c["config"]["children"][1]['b'])
self.assertEquals(c["config"]["children"][1]['b']["text"], "bbb")
self.assertTrue("id" in c["config"]["children"][1]['b']["attrs"])
self.assertEquals(c["config"]["children"][1]['b']["attrs"]["id"], "b0")

self.assertTrue('sect0' in c["config"]["children"][2])
self.assertTrue("text" not in c["config"]["children"][2]['sect0'])
self.assertTrue("attrs" not in c["config"]["children"][2]['sect0'])
self.assertTrue("children" in c["config"]["children"][2]['sect0'])
self.assertTrue(c["config"]["children"][2]['sect0']["children"])

def test_20_load(self):
"""FIXME: Implement test cases for XmlConfigParser.load"""
return

c = T.XmlConfigParser.load(self.config_path)["config"]
c = TT.XmlConfigParser.load(self.config_path)["config"]

self.assertEquals(c['a'], 0, str(c))
self.assertEquals(c['b'], "bbb", c)
Expand Down
20 changes: 10 additions & 10 deletions anyconfig/backend/xml_.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# License: MIT
#
from anyconfig.globals import LOGGER as logging

import anyconfig.backend.base as Base
import anyconfig.compat as AC


SUPPORTED = True
Expand Down Expand Up @@ -50,25 +52,23 @@ def etree_to_container(root, container):
"""
Convert XML ElementTree to a collection of container objects.
:param root: etree root object
:param root: etree root object or None
:param container:
"""
tree = container()
tree[root.tag] = container()

if root.attrib:
tree[root.tag]["attrs"] = container(AC.iteritems(root.attrib))

if root.text.strip():
tree[root.tag]["text"] = root.text.strip()

if len(root): # It has children.
# FIXME: Configuration item cannot have both attributes and
# values (list) at the same time in current implementation:
tree[root.tag] = container()
tree[root.tag]["children"] = [etree_to_container(c, container) for c
in root]
tree[root.tag]["attributes"] = container(**root.attrib)

if root.text.strip():
tree[root.tag]["text"] = root.text.strip()
else:
tree[root.tag]["attributes"] = container(**root.attrib)
if root.text.strip():
tree[root.tag]["text"] = root.text.strip()

return tree

Expand Down

0 comments on commit 54f22c6

Please sign in to comment.