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
]
-
append
(item)¶ Inserts the item at the end of this element’s internal list of children. Raises TypeError if item is not a
Element
orAttribute
-
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
-
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
-
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
orAttribute
-
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
andAttribute
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
]
-
Attribute¶
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
-
parse
(source)¶ Parses a Defold document into this
DefTree
. It returns the rootElement
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 FalseReturn type: bool
-
deftree.
is_attribute
(item)¶ Returns True if the item is an
Attribute
else returns FalseReturn 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
, orElement
.
-
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