core.dfraw module

Modification of Dwarf Fortress raw files.

class core.dfraw.DFRaw(path)[source]

Bases: DFRawNode

Represents a Dwarf Fortress raw file.


Gets the value of the first tag named <field>. Returns None if no such field exists.


Returns the values of <fields> in a list. The nesting and order of the resulting list will match the nesting and order of <fields>. Equivalent to calling get_value for each field.

static open(path, mode)[source]

Opens a raw file at <path> with mode <mode> and returns a stream.


Path to raw file


File mode (see, typically ‘rt’ or ‘wt’

classmethod read(path)[source]

Returns the contents of the raw file at <path>.


Re-writes the current raw file, saving all changes.

set_all(field, value)[source]

Sets all tags named <field> to <value>.

set_value(field, value)[source]

Sets the first tag named <field> to <value>.

classmethod write(path, text)[source]

Writes <text> to a raw file located at <path>.

class core.dfraw.DFRawComment(parent, text)[source]

Bases: DFRawNode

Represents a comment (non-tag) in a raw file.

class core.dfraw.DFRawNode(parent, node_id, value, node_type, **kwargs)[source]

Bases: object

Class representing a node in a raw file.

add_child(child, **kwargs)[source]

Adds <child> to the list of child nodes and sets its parent to this node. If <child> already has another parent, it is first removed from that parent. Has no effect if <child> is a root node.


The child node to add.

Keyword Arguments:

after – If None, the node is inserted as the first child. Otherwise, it is inserted after the child node provided in this argument. If omitted, or if the provided child node does not exist, the child is added as the last child.

property elements

Generator producing a flat view of this node and its subnodes. Yields raw nodes.

property filename

Returns the filename for this raw file.


Returns a list of all child nodes with the tag name field.


Returns the first child node with the tag name field, or None if no such node exists.

property fulltext

Returns the text for this node and all its children.

property is_comment

Returns True if this is a comment node.

property is_container

Returns True if this node represents a container (has children).

property is_flag

Returns True if this node represents a flag (has no values).

property is_root

Returns True if this is the root node for a raw file.

property is_tag

Returns True if this node represents a tag.

property parent

Returns the parent for this node, or itself if this is the root.


Removes <child> as a child node and sets its parent to None.

property root

Returns the root node.

property text

Returns the text for this node.

property value

Returns the unparsed value for this node.

property values

Returns a list of values associated with this node.

class core.dfraw.DFRawTag(parent, tag, value)[source]

Bases: DFRawNode

Represents a tag in a raw file.

core.dfraw.parse_raw(parent, text)[source]

Parses the raw text contained in <text> and places resulting nodes in a tree under <parent>.


Generator which returns nodes from a raw file.


text – text of the raw file to parse.


(kind, token) – tuple of “Tag” or “Comment”, and token text including any delimiters.