Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
root = etree.fromstring(response.content)
except etree.ParseError:
# May fail in case it's html instead of xml.
# Can happen on wrong authentication.
# That means it is not an error reported from executing
# some service in the box, but rather not allowed to
# access the box at all.
# Whatever it is, report it here:
detail = re.sub(r'<.*?>', '', response.text)
msg = f'Unable to perform operation. {detail}'
raise FritzConnectionException(msg)
detail = root.find('.//detail')
children = detail.iter()
next(children) # skip detail itself
for node in children:
tag = localname(node)
text = node.text.strip()
if tag == "errorCode":
error_code = text
parts.append(f'{tag}: {text}')
message = '\n'.join(parts)
# try except:KeyError not possible,
# because one of the raised Exceptions may inherit from KeyError.
exception = FRITZ_ERRORS.get(error_code, FritzConnectionException)
raise exception(message)
def process_node(obj, root):
"""
Take an object and a root of nodes. The node.text of nodes with the
same name as an instance-attribute of 'obj' are set as values for
the corresponding instance-attribute. If the attribute is a
callable, processing the node gets delegated to the callable (which
in turn calls process_node).
"""
for node in root:
node_name = localname(node)
try:
attr = getattr(obj, node_name)
except AttributeError:
# ignore unknown nodes
continue
if callable(attr):
# attribute is a callable: delegate further
attr(node)
else:
# node is an attribute: set value
if isinstance(node.text, str):
value = node.text.strip()
else:
value = node.text
setattr(obj, node_name, value)