in libcloud/common/nttcis.py [0:0]
def __init__(self, parent_element):
if parent_element.items():
if "property" in parent_element.tag:
self.update({parent_element.attrib.get("name"): parent_element.attrib.get("value")})
else:
self.update(dict(parent_element.items()))
for element in parent_element:
if len(element) > 0:
# treat like dict - we assume that if the first two tags
# in a series are different, then they are all different.
if len(element) == 1 or element[0].tag != element[1].tag:
elem_dict = XmlDictConfig(element)
# treat like list - we assume that if the first two tags
# in a series are the same, then the rest are the same.
else:
# here, we put the list in dictionary; the key is the
# tag name the list elements all share in common, and
# the value is the list itself
elem_dict = {element[0].tag.split("}")[1]: XmlListConfig(element)}
# if the tag has attributes, add those to the dict
if element.items():
elem_dict.update(dict(element.items()))
self.update({element.tag.split("}")[1]: elem_dict})
# this assumes that if you've got an attribute in a tag,
# you won't be having any text. This may or may not be a
# good idea -- time will tell. It works for the way we are
# currently doing XML configuration files...
elif element.items():
# It is possible to have duplicate element tags.
# If so, convert to a dict of lists
if element.tag.split("}")[1] in self:
if isinstance(self[element.tag.split("}")[1]], list):
self[element.tag.split("}")[1]].append(dict(element.items()))
else:
tmp_list = list()
tmp_dict = dict()
for k, v in self[element.tag.split("}")[1]].items():
if isinstance(k, XmlListConfig):
tmp_list.append(k)
else:
tmp_dict.update({k: v})
tmp_list.append(tmp_dict)
tmp_list.append(dict(element.items()))
self[element.tag.split("}")[1]] = tmp_list
else:
self.update({element.tag.split("}")[1]: dict(element.items())})
# finally, if there are no child tags and no attributes, extract
# the text
else:
self.update({element.tag.split("}")[1]: element.text})