DefTree API

Element

class deftree.Element(name)

Element class. This class defines the Element interface

append(item)

Inserts the item at the end of this element’s internal list of children. Raises TypeError if item is not a Element or Attribute

clear()

Resets an element. This function removes all children, clears all attributes

copy()

Returns a deep copy of the current Element.

get_attribute(name[, value])

Returns the first Attribute instance whose name matches name and if value is not None whose value equal value. If none is found it returns None.

get_element(name)

Returns the first Element whose name matches name, if none is found returns None.

get_parent()

Returns the parent of the current Element

index(item)

Returns the index of the item in this element, raises ValueError if not found.

insert(index, item)

Inserts the item at the given position in this element. Raises TypeError if item is not a Element or Attribute

iter_all()

Creates a tree iterator with the current element as the root. The iterator iterates over this element and all elements below it, in document (depth first) order. Both Element and Attribute are returned from the iterator.

iter_attributes()

Creates a tree iterator with the current element as the root. The iterator iterates over this element and all elements below it, in document (depth first) order. Only Attributes are returned from the iterator.

iter_elements()

Creates a tree iterator with the current element as the root. The iterator iterates over this element and all elements below it, in document (depth first) order. Only Element are returned from the iterator.

iter_find_attributes(name[, value])

Creates a tree iterator with the current element as the root. The iterator iterates over this element and all elements below it, in document (depth first) order. Only Attributes whose name equals name, and if value is not None whose value equal value are returned from the iterator

iter_find_elements(name)

Creates a tree iterator with the current element as the root. The iterator iterates over this element and all elements below it, in document (depth first) order. Only Element whose name equals name are returned from the iterator

remove(child)

Removes child from the element. Compares on instance identity not name. Raises TypeError if child is not a Element or Attribute

Attribute

class deftree.Attribute(parent, name, value)

Attribute class. This class defines the Attribute interface.

get_parent()

Returns the parent element of the attribute.

DefTree

class deftree.DefTree

DefTree class. This class represents an entire element hierarchy.

dump()

Write the the DefTree structure to sys.stdout. This function should be used for debugging only.

from_string(text[, parser])

Parses a Defold document section from a string constant which it returns. parser is an optional parser instance. If not given the standard parser is used. Returns the root of DefTree.

get_root()

Returns the root Element

parse(source[, parser])

Parses a Defold document into a DefTree which it returns. source is a file_path. parser is an optional parser instance. If not given the standard parser is used.

write(file_path)

Writes the element tree to a file, as plain text. file_path needs to be a path

Helpers

deftree.SubElement(parent, name)

SubElement factory which creates an element instance with name, and appends it to an existing parent.

deftree.parse(source)

Parses a Defold document into a DefTree which it returns. source is a file_path. parser is an optional parser instance. If not given the standard parser is used.

deftree.from_string(text[, parser])

Parses a Defold document section from a string constant which it returns. parser is an optional parser instance. If not given the standard parser is used. Returns the root of DefTree.

deftree.to_string(element[, parser])

Generates a string representation of the Element, including all children. element is a Element instance.