Remove usage of deprecated getchildren() method.

This commit is contained in:
Lance Stout 2012-06-19 09:47:31 -07:00
parent 8119551049
commit 7858d969d8
11 changed files with 27 additions and 28 deletions

View file

@ -47,7 +47,7 @@ class Failure(StanzaBase):
def get_condition(self): def get_condition(self):
"""Return the condition element's name.""" """Return the condition element's name."""
for child in self.xml.getchildren(): for child in self.xml:
if "{%s}" % self.namespace in child.tag: if "{%s}" % self.namespace in child.tag:
cond = child.tag.split('}', 1)[-1] cond = child.tag.split('}', 1)[-1]
if cond in self.conditions: if cond in self.conditions:
@ -68,7 +68,7 @@ class Failure(StanzaBase):
def del_condition(self): def del_condition(self):
"""Remove the condition element.""" """Remove the condition element."""
for child in self.xml.getchildren(): for child in self.xml:
if "{%s}" % self.condition_ns in child.tag: if "{%s}" % self.condition_ns in child.tag:
tag = child.tag.split('}', 1)[-1] tag = child.tag.split('}', 1)[-1]
if tag in self.conditions: if tag in self.conditions:

View file

@ -77,12 +77,12 @@ class Item(ElementBase):
self.append(value) self.append(value)
def get_payload(self): def get_payload(self):
childs = self.xml.getchildren() childs = list(self.xml)
if len(childs) > 0: if len(childs) > 0:
return childs[0] return childs[0]
def del_payload(self): def del_payload(self):
for child in self.xml.getchildren(): for child in self.xml:
self.xml.remove(child) self.xml.remove(child)
@ -254,12 +254,12 @@ class PubsubState(ElementBase):
self.xml.append(value) self.xml.append(value)
def get_payload(self): def get_payload(self):
childs = self.xml.getchildren() childs = list(self.xml)
if len(childs) > 0: if len(childs) > 0:
return childs[0] return childs[0]
def del_payload(self): def del_payload(self):
for child in self.xml.getchildren(): for child in self.xml:
self.xml.remove(child) self.xml.remove(child)

View file

@ -33,7 +33,7 @@ class PubsubErrorCondition(ElementBase):
def get_condition(self): def get_condition(self):
"""Return the condition element's name.""" """Return the condition element's name."""
for child in self.parent().xml.getchildren(): for child in self.parent().xml:
if "{%s}" % self.condition_ns in child.tag: if "{%s}" % self.condition_ns in child.tag:
cond = child.tag.split('}', 1)[-1] cond = child.tag.split('}', 1)[-1]
if cond in self.conditions: if cond in self.conditions:
@ -55,7 +55,7 @@ class PubsubErrorCondition(ElementBase):
def del_condition(self): def del_condition(self):
"""Remove the condition element.""" """Remove the condition element."""
for child in self.parent().xml.getchildren(): for child in self.parent().xml:
if "{%s}" % self.condition_ns in child.tag: if "{%s}" % self.condition_ns in child.tag:
tag = child.tag.split('}', 1)[-1] tag = child.tag.split('}', 1)[-1]
if tag in self.conditions: if tag in self.conditions:

View file

@ -31,12 +31,12 @@ class EventItem(ElementBase):
self.xml.append(value) self.xml.append(value)
def get_payload(self): def get_payload(self):
childs = self.xml.getchildren() childs = list(self.xml)
if len(childs) > 0: if len(childs) > 0:
return childs[0] return childs[0]
def del_payload(self): def del_payload(self):
for child in self.xml.getchildren(): for child in self.xml:
self.xml.remove(child) self.xml.remove(child)

View file

@ -89,7 +89,7 @@ class Error(ElementBase):
def get_condition(self): def get_condition(self):
"""Return the condition element's name.""" """Return the condition element's name."""
for child in self.xml.getchildren(): for child in self.xml:
if "{%s}" % self.condition_ns in child.tag: if "{%s}" % self.condition_ns in child.tag:
cond = child.tag.split('}', 1)[-1] cond = child.tag.split('}', 1)[-1]
if cond in self.conditions: if cond in self.conditions:
@ -110,7 +110,7 @@ class Error(ElementBase):
def del_condition(self): def del_condition(self):
"""Remove the condition element.""" """Remove the condition element."""
for child in self.xml.getchildren(): for child in self.xml:
if "{%s}" % self.condition_ns in child.tag: if "{%s}" % self.condition_ns in child.tag:
tag = child.tag.split('}', 1)[-1] tag = child.tag.split('}', 1)[-1]
if tag in self.conditions: if tag in self.conditions:

View file

@ -122,7 +122,7 @@ class Iq(RootStanza):
def get_query(self): def get_query(self):
"""Return the namespace of the <query> element.""" """Return the namespace of the <query> element."""
for child in self.xml.getchildren(): for child in self.xml:
if child.tag.endswith('query'): if child.tag.endswith('query'):
ns = child.tag.split('}')[0] ns = child.tag.split('}')[0]
if '{' in ns: if '{' in ns:
@ -132,7 +132,7 @@ class Iq(RootStanza):
def del_query(self): def del_query(self):
"""Remove the <query> element.""" """Remove the <query> element."""
for child in self.xml.getchildren(): for child in self.xml:
if child.tag.endswith('query'): if child.tag.endswith('query'):
self.xml.remove(child) self.xml.remove(child)
return self return self

View file

@ -76,7 +76,7 @@ class SleekTest(unittest.TestCase):
known_prefixes[prefix], known_prefixes[prefix],
xml_string) xml_string)
xml = self.parse_xml(xml_string) xml = self.parse_xml(xml_string)
xml = xml.getchildren()[0] xml = list(xml)[0]
return xml return xml
else: else:
self.fail("XML data was mal-formed:\n%s" % xml_string) self.fail("XML data was mal-formed:\n%s" % xml_string)
@ -517,9 +517,9 @@ class SleekTest(unittest.TestCase):
if '{%s}lang' % xml_ns in recv_xml.attrib: if '{%s}lang' % xml_ns in recv_xml.attrib:
del recv_xml.attrib['{%s}lang' % xml_ns] del recv_xml.attrib['{%s}lang' % xml_ns]
if recv_xml.getchildren: if list(recv_xml):
# We received more than just the header # We received more than just the header
for xml in recv_xml.getchildren(): for xml in recv_xml:
self.xmpp.socket.recv_data(tostring(xml)) self.xmpp.socket.recv_data(tostring(xml))
attrib = recv_xml.attrib attrib = recv_xml.attrib
@ -698,7 +698,7 @@ class SleekTest(unittest.TestCase):
if xml.tag.startswith('{'): if xml.tag.startswith('{'):
return return
xml.tag = '{%s}%s' % (ns, xml.tag) xml.tag = '{%s}%s' % (ns, xml.tag)
for child in xml.getchildren(): for child in xml:
self.fix_namespaces(child, ns) self.fix_namespaces(child, ns)
def compare(self, xml, *other): def compare(self, xml, *other):
@ -741,7 +741,7 @@ class SleekTest(unittest.TestCase):
return False return False
# Step 4: Check children count # Step 4: Check children count
if len(xml.getchildren()) != len(other.getchildren()): if len(list(xml)) != len(list(other)):
return False return False
# Step 5: Recursively check children # Step 5: Recursively check children

View file

@ -151,8 +151,8 @@ class MatchXMLMask(MatcherBase):
""" """
tag = tag.split('}')[-1] tag = tag.split('}')[-1]
try: try:
children = [c.tag.split('}')[-1] for c in xml.getchildren()] children = [c.tag.split('}')[-1] for c in xml]
index = children.index(tag) index = children.index(tag)
except ValueError: except ValueError:
return None return None
return xml.getchildren()[index] return list(xml)[index]

View file

@ -77,10 +77,10 @@ class MatchXPath(MatcherBase):
# Skip empty tag name artifacts from the cleanup phase. # Skip empty tag name artifacts from the cleanup phase.
continue continue
children = [c.tag.split('}')[-1] for c in xml.getchildren()] children = [c.tag.split('}')[-1] for c in xml]
try: try:
index = children.index(tag) index = children.index(tag)
except ValueError: except ValueError:
return False return False
xml = xml.getchildren()[index] xml = list(xml)[index]
return True return True

View file

@ -445,7 +445,7 @@ class ElementBase(object):
return return
# Initialize values using provided XML # Initialize values using provided XML
for child in self.xml.getchildren(): for child in self.xml:
if child.tag in self.plugin_tag_map: if child.tag in self.plugin_tag_map:
plugin_class = self.plugin_tag_map[child.tag] plugin_class = self.plugin_tag_map[child.tag]
self.init_plugin(plugin_class.plugin_attrib, self.init_plugin(plugin_class.plugin_attrib,
@ -1050,8 +1050,7 @@ class ElementBase(object):
if parent is None: if parent is None:
parent = self.xml parent = self.xml
for element in elements: for element in elements:
if element.tag == original_target or \ if element.tag == original_target or not list(element):
not element.getchildren():
# Only delete the originally requested elements, and # Only delete the originally requested elements, and
# any parent elements that have become empty. # any parent elements that have become empty.
elem_lang = element.attrib.get('{%s}lang' % XML_NS, elem_lang = element.attrib.get('{%s}lang' % XML_NS,
@ -1491,7 +1490,7 @@ class StanzaBase(ElementBase):
def get_payload(self): def get_payload(self):
"""Return a list of XML objects contained in the stanza.""" """Return a list of XML objects contained in the stanza."""
return self.xml.getchildren() return list(self.xml)
def set_payload(self, value): def set_payload(self, value):
"""Add XML content to the stanza. """Add XML content to the stanza.

View file

@ -107,7 +107,7 @@ def tostring(xml=None, xmlns='', stanza_ns='', stream=None,
if xml.text: if xml.text:
output.append(xml_escape(xml.text)) output.append(xml_escape(xml.text))
if len(xml): if len(xml):
for child in xml.getchildren(): for child in xml:
output.append(tostring(child, tag_xmlns, stanza_ns, stream)) output.append(tostring(child, tag_xmlns, stanza_ns, stream))
output.append("</%s>" % tag_name) output.append("</%s>" % tag_name)
elif xml.text: elif xml.text: