beancount.tools

Standalone tools that aren’t linked to Beancount but that are useful with it.

The beancount.scripts package contains the implementation of scripts which invoke the Beancount library code. This beancount.tools pacakge implements other tools which aren’t directly invoking Beancount library code and which could be theoretically copied and used independently. However, these are to be distributed with Beancount and in order to maintain all the source code together they are put in this package and invokes from stubs under beancount/bin/, just like the other scripts.

beancount.tools.sheets_upload

beancount.tools.treeify

Identify a column of text that contains hierarchical id and treeify that column.

This script will inspect a text file and attempt to find a vertically left-aligned column of text that contains identifiers with multiple components, such as “Assets:US:Bank:Checking”, and replace those by a tree-like structure rendered in ASCII, inserting new empty lines where necessary to create the tree.

Note: If your paths have spaces in them, this will not work. Space is used as a delimiter to detect the end of a column. You can customize the delimiter with an option.

class beancount.tools.treeify.Node(name)

A node with a name attribute, a list of line numbers and a list of children (from its parent class).

beancount.tools.treeify.create_tree(column_matches, regexp_split)

Build up a tree from a list of matches.

Parameters
  • column_matches – A list of (line-number, name) pairs.

  • regexp_split – A regular expression string, to use for splitting the names of components.

Returns

An instance of Node, the root node of the created tree.

beancount.tools.treeify.dump_tree(node, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, prefix='')

Render a tree as a tree.

Parameters
  • node – An instance of Node.

  • file – A file object to write to.

  • prefix – A prefix string for each of the lines of the children.

beancount.tools.treeify.enum_tree_by_input_line_num(tree_lines)

Accumulate the lines of a tree until a line number is found.

Parameters

tree_lines – A list of lines as returned by render_tree.

Yields

Pairs of (line number, list of (line, node)).

beancount.tools.treeify.find_column(lines, pattern, delimiter)

Find a valid column with hierarchical data in the text lines.

Parameters
  • lines – A list of strings, the contents of the input.

  • pattern – A regular expression for the hierarchical entries.

  • delimiter – A regular expression that dictates how we detect the end of a column. Normally this is a single space. If the patterns contain spaces, you will need to increase this.

Returns

A tuple of
matches: A list of (line-number, name) tuples where ‘name’ is the

hierarchical string to treeify and line-number is an integer, the line number where this applies.

left: An integer, the leftmost column. right: An integer, the rightmost column.

Note that not all line numbers may be present, so you may need to skip some. However, they are in guaranteed in sorted order.

beancount.tools.treeify.main()
beancount.tools.treeify.render_tree(root)

Render a tree of nodes.

Returns

A list of tuples of (first_line, continuation_line, node) where
first_line: A string, the first line to render, which includes the

account name.

continuation_line: A string, further line to render if necessary. node: The Node instance which corresponds to this line.

and an integer, the width of the new columns.