core.dfraw module

Modification of Dwarf Fortress raw files.

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

Bases: core.dfraw.DFRawNode

Represents a Dwarf Fortress raw file.

get_value(field)[source]

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

get_values(*fields)[source]

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.

Params:
path

Path to raw file

mode

File mode (see io.open), typically ‘rt’ or ‘wt’

classmethod read(path)[source]

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

save()[source]

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: core.dfraw.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.

Params:
child

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.

find_all(field)[source]

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

find_first(field)[source]

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.

remove_child(child)[source]

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: core.dfraw.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>.

core.dfraw.tokenize_raw(text)[source]

Generator which returns nodes from a raw file.

Parameters

text – text of the raw file to parse.

Returns

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