Mon, 07 Jul 2003
Listing TODO items in your source

This afternoon is TODO time (since my main job of the day - generating nice little help tooltips - was solved by the very cool overlib.) So, in usual fashion I avoid the job for a while and write a script to help me. The following is the result, finding all TODO items in source trees and printing them out with some context:

#! /usr/bin/env python2.3

import sys, os

def walker(args, path, filenames):
    for filename in filenames:
        joined = os.path.join(path, filename)
        if os.path.isdir(joined):
            continue

        # find lines to display, at least 2 either side of a TODO marker
        lines = open(joined).readlines()
        display = {}
        for i, line in enumerate(lines):
            if 'TODO' not in line:
                continue
            for n in range(i-2, i+3):
                display[n] = 1
        if not display:
            continue

        # organise the lines to display
        display = [n for n in display.keys() if n >= 0 and n < len(lines)]
        display.sort()

        # print them
        print '**', joined
        last = None
        for n in display:
            if last is not None and n > last+1:
                print '---'
            last = n
            sys.stdout.write(lines[n])
        print '*'*75

if __name__=='__main__':
    for arg in sys.argv[1:]:
        os.path.walk(arg, walker, None)

Yes, I took the time to have a look at some new python 2.3 features too (see if you can spot them :)

path: /python | permanent link |