text
stringlengths
0
828
4016,"def clear_element(parent_to_parse, element_path=None):
""""""
Clears everything (text, attributes and children) from the parsed parent
or named element.
""""""
element = get_element(parent_to_parse, element_path)
if element is None:
return parent_to_parse
else:
element.clear()
return element"
4017,"def copy_element(from_element, to_element=None, path_to_copy=None):
""""""
Copies the element at path_to_copy in from_element and uses it to create or update
the first element found at the same location (path_to_copy) in to_element.
If path_to_copy is not provided, from_element is copied to the root of to_element.
""""""
from_element = get_element(from_element, path_to_copy)
dest_element = get_element(to_element, path_to_copy)
if from_element is None:
return None
if dest_element is None:
if path_to_copy is None:
dest_element = Element(from_element.tag)
else:
dest_element = insert_element(Element(from_element.tag), 0, path_to_copy)
dest_element.tag = from_element.tag
dest_element.text = from_element.text
dest_element.tail = from_element.tail
dest_element.attrib = from_element.attrib
copied_children = []
for elem in from_element:
copied_children.append(copy_element(elem))
for idx, child in enumerate(copied_children):
dest_element.insert(idx, child)
return dest_element"
4018,"def get_element_tree(parent_to_parse):
""""""
:return: an ElementTree initialized with the parsed element.
:see: get_element(parent_to_parse, element_path)
""""""
if isinstance(parent_to_parse, ElementTree):
return parent_to_parse
element = get_element(parent_to_parse)
return ElementTree() if element is None else ElementTree(element)"
4019,"def get_element(parent_to_parse, element_path=None):
""""""
:return: an element from the parent or parsed from a Dictionary, XML string
or file. If element_path is not provided the root element is returned.
""""""
if parent_to_parse is None:
return None
elif isinstance(parent_to_parse, ElementTree):
parent_to_parse = parent_to_parse.getroot()
elif hasattr(parent_to_parse, 'read'):
parent_to_parse = string_to_element(parent_to_parse.read())
elif isinstance(parent_to_parse, STRING_TYPES):
parent_to_parse = string_to_element(parent_to_parse)
elif isinstance(parent_to_parse, dict):
parent_to_parse = dict_to_element(parent_to_parse)
if parent_to_parse is None:
return None
elif not isinstance(parent_to_parse, ElementType):
element_type = type(parent_to_parse).__name__
raise TypeError('Invalid element type: {0}'.format(element_type))
return parent_to_parse.find(element_path) if element_path else parent_to_parse"
4020,"def get_remote_element(url, element_path=None):
""""""
:return: an element initialized with the content at the specified file or URL
:see: get_element(parent_to_parse, element_path)
""""""
content = None
if url is None:
return content
elif _FILE_LOCATION_REGEX.match(url):
with open(url, 'rb') as xml: