Orgnode - reading org-mode in Python

Why this module?

I have written several Python scripts to extract data from org-mode files. Each script requires different components of the data, for example, headlines, tags and todo status. It made sense to write one program to access the file contents allowing me to concentrate on implementing the functionality of my script instead of the intricacies of parsing org-mode.

The first application of this module is a program to select a word at random from a vocabulary file (stored in org-mode) and display in a GUI.

The Orgnode module in a nutshell

The Orgnode module contains one class, called Orgnode and a routine to read an org-mode file and create a list of Orgnode objects.

The module is for reading an org-mode file. There are no methods for writing org-mode since since I have no need for this functionality.

You are welcome to extend the functionality of the __repr__ method of the Orgnode class if you would like this functionality.

Some simple examples

Here are two simple examples. The first program reads my file of active tasks and projects (newgtd.org) and prints the tasks that are scheduled for today.

import Orgnode, datetime

today = datetime.date.today()
print "Daily plan for", today
print "-------------------------\n"

filename = "c:/charles/GTD/newgtd.org"
nodelist = Orgnode.makelist(filename)

for n in nodelist:
    if n.Scheduled() == today:
         print "[ ] %s (%s)" % (n.Heading(), n.Tag() )

Another script prints my project list and the brief description of the project which follows the heading.

from Orgnode import *

import sys
filename = sys.argv[1]
nodelist = makelist(filename)

print "\n--------- PROJECT LIST ----------------"
for node in nodelist:
   if node.hasTag("PROJECT"):
       print "[ ]", node.Heading()

Download

Download the Orgnode.py Python source code. The source code is available under the MIT License.

Module documentation

Documentation is embedded in the module using docstrings. Here is the output of help(Orgnode).

NAME
    Orgnode

FILE
    c:\homes\charles\python\orgnode.py

DESCRIPTION
    The Orgnode module consists of the Orgnode class for representing a
    headline and associated text from an org-mode file, and routines for
    constructing data structures of these classes.

CLASSES
    __builtin__.object
        Orgnode

    class Orgnode(__builtin__.object)
     |  Orgnode class represents a headline, tags and text associated
     |  with the headline.
     |
     |  Methods defined here:
     |
     |  Body(self)
     |      Returns all lines of text of the body of this node except the
     |      Property Drawer
     |
     |  Deadline(self)
     |      Return the deadline date object or null if nonexistent
     |
     |  Heading(self)
     |      Return the Heading text of the node without the TODO tag
     |
     |  Level(self)
     |      Returns an integer corresponding to the level of the node.
     |      Top level (one asterisk) has a level of 1.
     |
     |  Priority(self)
     |      Returns the priority of this headline: 'A', 'B', 'C' or empty
     |      string if priority has not been set.
     |
     |  Property(self, keyval)
     |      Returns the value of the requested property or null if the
     |      property does not exist.
     |
     |  Scheduled(self)
     |      Return the scheduled date object or null if nonexistent
     |
     |  Tag(self)
     |      Returns the value of the first tag.
     |      For example, :HOME:COMPUTER: would return HOME
     |
     |  Tags(self)
     |      Returns a list of all tags
     |      For example, :HOME:COMPUTER: would return ['HOME', 'COMPUTER']
     |
     |  Todo(self)
     |      Return the value of the TODO tag
     |
     |  __init__(self, level, headline, body, tag, alltags)
     |      Create an Orgnode object given the parameters of level (as the
     |      raw asterisks), headline text (including the TODO tag), and
     |      first tag. The makelist routine postprocesses the list to
     |      identify TODO tags and updates headline and todo fields.
     |
     |  __repr__(self)
     |      Print the level, heading text and tag of a node and the body
     |      text as used to construct the node.
     |
     |  hasTag(self, srch)
     |      Returns True if the supplied tag is present in this headline
     |      For example, hasTag('COMPUTER') on headling containing
     |      :HOME:COMPUTER: would return True.
     |
     |  setDeadline(self, dateval)
     |      Set the deadline (due) date using the supplied date object
     |
     |  setHeading(self, newhdng)
     |      Change the heading to the supplied string
     |
     |  setPriority(self, newprty)
     |      Change the value of the priority of this headline.
     |      Values values are '', 'A', 'B', 'C'
     |
     |  setProperties(self, dictval)
     |      Sets all properties using the supplied dictionary of
     |      name/value pairs
     |
     |  setScheduled(self, dateval)
     |      Set the scheduled date using the supplied date object
     |
     |  setTag(self, newtag)
     |      Change the value of the first tag to the supplied string
     |
     |  setTags(self, taglist)
     |      Store all the tags found in the headline. The first tag will
     |      also be stored as if the setTag method was called.
     |
     |  setTodo(self, value)
     |      Set the value of the TODO tag to the supplied string
     |

FUNCTIONS
    makelist(filename)
        Read an org-mode file and return a list of Orgnode objects
        created from this file.

Author: Charles Cave <charlesweb@optusnet.com.au>

Date: 2010-06-14