DefTree API

Element

class deftree.Element(name)

Element class. This class defines the Element interface

add_attribute(name, value)

Creates an Attribute instance with name and value as a child to self.

Return type:Union[DefTreeBool, DefTreeEnum, DefTreeFloat, DefTreeInt, DefTreeString]
add_element(name)

Creates an Element instance with name as a child to self.

Return type:Element
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

attributes(name=None, value=None)

Iterates over the current element and returns all attributes. Only Attributes. Name and value are optional and used for filters.

Return type:Iterator[Attribute]
clear()

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

copy()

Returns a deep copy of the current Element.

Return type:Element
elements(name=None)

Iterates over the current element and returns all elements. If the optional argument name is not None only Element with a name equal to name is returned.

Return type:Iterator[Element]
get_attribute(name, value=None)

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

Return type:Attribute
get_element(name)

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

Return type:Element
get_parent()

Returns the parent of the current Element

Return type:Element
index(item)

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

Return type:int
insert(index, item)

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

iter()

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.

Return type:Iterator[Union[Element, Attribute]]
iter_attributes(name=None)

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. If the optional argument name is not None only Attribute with a name equal to name is returned.

Return type:Iterator[Attribute]
iter_elements(name=None)

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. If the optional argument name is not None only Element with a name equal to name is returned.

Return type:Iterator[Element]
remove(child)

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

set_attribute(name, value)

Sets the first Attribute with name to value.

Attribute

class deftree.Attribute(parent, name, value)

Attribute class. This class defines the Attribute interface.

get_parent()

Returns the parent element of the attribute.

Return type:Element
name

The name of the attribute, used to set and get the name

value

The value of the attribute, used to set and get the attributes value

DefTree

class deftree.DefTree

DefTree class. This class represents an entire element hierarchy.

dump()

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

from_string(text)

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.

Return type:DefTree
get_document_path()

Returns the path to the parsed document.

Return type:str
get_root()

Returns the root Element

Return type:Element
parse(source)

Parses a Defold document into this DefTree. It returns the root Element of the DefTree. source is a file_path.

Return type:Element
write(file_path=None)

Writes the element tree to a file, as plain text. uses the parsed file as a default

Helpers

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.

Return type:DefTree
deftree.from_string(text)

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.

Return type:DefTree
deftree.is_element(item)

Returns True if the item is an Element else returns False

Return type:bool
deftree.is_attribute(item)

Returns True if the item is an Attribute else returns False

Return type:bool
deftree.to_string(element)

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

Return type:str
deftree.dump(element)

Writes the element tree or element structure to sys.stdout. This function should be used for debugging only. element is either an DefTree, or Element.

deftree.validate(string, path_or_string, verbose=False)

Verifies that a document in string format equals a document in path_or_string. If Verbose is True it echoes the result. This function should be used for debugging only. Returns a bool representing the result

Return type:bool