pyorg.ast module

Work with org file abstract syntax trees.

See https://orgmode.org/worg/dev/org-syntax.html for a description of the org syntax.

class pyorg.ast.DispatchNodeType(default, registry=None, doc=None)[source]

Bases: pyorg.util.SingleDispatchBase

Generic function which dispatches on the node type of its first argument.

format_key(key)[source]
get_key(node)[source]

Get the key to look up the correct implementation for the given argument.

pyorg.ast.NODE_CLASSES = {'headline': <class 'pyorg.ast.OrgOutlineNode'>, 'org-data': <class 'pyorg.ast.OrgOutlineNode'>, 'table': <class 'pyorg.ast.OrgTableNode'>, 'timestamp': <class 'pyorg.ast.OrgTimestampNode'>}

Mapping from org element/node types to their Python class

pyorg.ast.ORG_NODE_TYPES = {'babel-call': OrgNodeType('babel-call'), 'bold': OrgNodeType('bold'), 'center-block': OrgNodeType('center-block'), 'clock': OrgNodeType('clock'), 'code': OrgNodeType('code'), 'comment': OrgNodeType('comment'), 'comment-block': OrgNodeType('comment-block'), 'diary-sexp': OrgNodeType('diary-sexp'), 'drawer': OrgNodeType('drawer'), 'dynamic-block': OrgNodeType('dynamic-block'), 'entity': OrgNodeType('entity'), 'example-block': OrgNodeType('example-block'), 'export-block': OrgNodeType('export-block'), 'export-snippet': OrgNodeType('export-snippet'), 'fixed-width': OrgNodeType('fixed-width'), 'footnote-definition': OrgNodeType('footnote-definition'), 'footnote-reference': OrgNodeType('footnote-reference'), 'headline': OrgNodeType('headline'), 'horizontal-rule': OrgNodeType('horizontal-rule'), 'inline-babel-call': OrgNodeType('inline-babel-call'), 'inline-src-block': OrgNodeType('inline-src-block'), 'inlinetask': OrgNodeType('inlinetask'), 'italic': OrgNodeType('italic'), 'item': OrgNodeType('item'), 'keyword': OrgNodeType('keyword'), 'latex-environment': OrgNodeType('latex-environment'), 'latex-fragment': OrgNodeType('latex-fragment'), 'line-break': OrgNodeType('line-break'), 'link': OrgNodeType('link'), 'macro': OrgNodeType('macro'), 'node-property': OrgNodeType('node-property'), 'org-data': OrgNodeType('org-data'), 'paragraph': OrgNodeType('paragraph'), 'plain-list': OrgNodeType('plain-list'), 'planning': OrgNodeType('planning'), 'property-drawer': OrgNodeType('property-drawer'), 'quote-block': OrgNodeType('quote-block'), 'radio-target': OrgNodeType('radio-target'), 'section': OrgNodeType('section'), 'special-block': OrgNodeType('special-block'), 'src-block': OrgNodeType('src-block'), 'statistics-cookie': OrgNodeType('statistics-cookie'), 'strike-through': OrgNodeType('strike-through'), 'subscript': OrgNodeType('subscript'), 'superscript': OrgNodeType('superscript'), 'table': OrgNodeType('table'), 'table-cell': OrgNodeType('table-cell'), 'table-row': OrgNodeType('table-row'), 'target': OrgNodeType('target'), 'timestamp': OrgNodeType('timestamp'), 'underline': OrgNodeType('underline'), 'verbatim': OrgNodeType('verbatim'), 'verse-block': OrgNodeType('verse-block')}

Mapping from names of all AST node types to OrgNodeType instances.

class pyorg.ast.OrgNode(type_, props=None, contents=None, keywords=None, parent=None, outline=None)[source]

Bases: object

A node in an org file abstract syntax tree.

Implements the sequence protocol as a sequence containing its child nodes (identically to contents). Also allows accessing property values by indexing with a string key.

type

Node type, obtained from org-element-type.

Type:OrgNodeType
props

Dictionary of property values, obtained from org-element-property.

Type:dict
contents

List of contents (org nodes or strings), obtained from org-element-contents.

Type:list
keywords

Dictionary of keyword values.

Type:dict
parent

Parent AST node.

Type:OrgNode
outline

Most recent outline node in the node’s ancestors (not including self).

Type:OrgOutlineNode
is_outline

Whether this node is an outline node.

Type:bool
children

Iterator over all child AST nodes (in contents or keyword/property values.

dump(index=None, properties=False, indent=' ', _level=0)[source]

Print a debug representation of the node and its descendants.

is_outline = False
class pyorg.ast.OrgNodeType[source]

Bases: pyorg.ast.OrgNodeType

The properties of an org AST node type.

name

The unique name of this node type.

Type:str
is_element

Whether this node type is an element. “An element defines syntactical parts that are at the same level as a paragraph, i.e. which cannot contain or be included in a paragraph.”

Type:bool
is_object

Whether this node type is an object. All nodes which are not elements are objects. “An object is a part that could be included in an element.”

Type:bool
is_greater_element

Whether this node type is a greater element. “Greater elements are all parts that can contain an element.”

Type:bool
is_recursive

Whether this node type is a recursive object.

Type:bool
is_object_container

Whether this node type is an object container, i.e. can directly contain objects.

Type:bool

References

Org Syntax

is_object
class pyorg.ast.OrgOutlineNode(type_, *args, title=None, id=None, **kw)[source]

Bases: pyorg.ast.OrgNode

Org node that is a component of the outline tree.

Corresponds to the root org-data node or a headline node.

level

Outline level. 0 corresponds to the root node of the file.

Type:int
title

Title of outline node as plain text.

Type:str
id

Unique ID for TOC tree.

Type:str
section

Org node with type “section” that contains the outline node’s direct content (not part of any nested outline nodes).

Type:OrgNode
has_todo

Whether this outline has a TODO keyword.

Type:bool
priority_chr

Priority character if headline with priority, otherwise None.

Type:str
dump_outline()[source]

Print representation of node’s outline subtree.

has_todo
is_outline = True
outline_children

Iterable over child outline nodes.

outline_tree()[source]

Create a list of (child, child_tree) pairs.

priority_chr
class pyorg.ast.OrgTableNode(type_, props=None, contents=None, keywords=None, parent=None, outline=None)[source]

Bases: pyorg.ast.OrgNode

An org node with type “table”.

rows

List of standard rows.

Type:list of OrgNode
blocks()[source]

Standard rows divided into “blocks”, which were separated by rule rows.

Returns:
Return type:list of list of OrgNode
cells()[source]
rows
class pyorg.ast.OrgTimestampNode(type_, *args, **kwargs)[source]

Bases: pyorg.ast.OrgNode

An org node with type “timestamp”.

begin

Begin date, parsed from properties

Type:datetime
end

End date, parsed from properties

Type:datetime
pyorg.ast.as_node_type(t)[source]

Convert to node type object, looking up strings by name.

pyorg.ast.as_secondary_string(obj)[source]

Convert argument to a “secondary string” (list of nodes or strings.

Parameters:obj (OrgNode or str or list) –
Returns:
Return type:list
Raises:TypeError : if obj is not a str or OrgNode or iterable of these.
pyorg.ast.assign_outline_ids(root, depth=3)[source]

Assign unique IDs to outline nodes.

pyorg.ast.dispatch_node_type(parent=None)[source]

Decorator to create DispatchNodeType instance from default implementation.

pyorg.ast.get_node_type(obj, name=False)[source]

Get type of AST node, returning None for other types.

pyorg.ast.node_cls(type_)[source]

Register a node class for a particular type in NODE_CLASSES.