#!/usr/bin/python # For some reason (a bug in org-mode?), I appear to have a bunch of entries in # my archive file that I didn't archive. This finds them. # Adam Sampson import os, sys, re org_dir = os.getenv("HOME") + "/org" def read_org_file(fn): items = [(None, [])] f = open(fn) while True: l = f.readline() if l == "": break l = l[:-1] if l.startswith("*"): items.append((l, [])) else: items[-1][1].append(l) f.close() return items def write_org_file(items, fn): f = open(fn, "w") for heading, lines in items: if heading is not None: f.write(heading + "\n") for line in lines: f.write(line + "\n") f.close() todo_items = read_org_file(org_dir + "/todo.org") in_todo = {} for heading, lines in todo_items: in_todo.setdefault(heading, []).append(lines) archive_items = read_org_file(org_dir + "/archive.org") in_archive = {} archived_headings = set() for heading, lines in archive_items: if heading is None or not heading.startswith("*** "): continue todo_heading = heading.replace("DONE", "TODO") archived = any([l.find(":ARCHIVE_TIME:") != -1 for l in lines]) in_archive.setdefault(todo_heading, []).append(lines) if archived: archived_headings.add(todo_heading) archive_keep = [] rescued = [] for heading, lines in archive_items: archive_keep.append((heading, lines)) if heading is None or not heading.startswith("*** "): continue todo_heading = heading.replace("DONE", "TODO") archived = any([l.find(":ARCHIVE_TIME:") != -1 for l in lines]) if archived: # This was really archived. status = "arch" drop = False elif todo_heading in archived_headings and not archived: # There is an archived version, but this isn't it. status = "later" drop = True elif todo_heading[1:] in in_todo: # This is still in todo.org. status = "todo" drop = True else: status = "??" drop = True rescued.append((heading, lines)) print "%-6s %3d %s" % (status, len(in_archive[todo_heading]), heading) if drop: archive_keep.pop() write_org_file(archive_keep, org_dir + "/archive.org.new") write_org_file(rescued, org_dir + "/rescued.org")