#!/usr/bin/env python # # Copyright 2003-2006 Robert W. Brewer rwb123 at gmail dot com # # This is released under the GNU GPL (General Public License) # version 2 or later. # # convert a supermemo for palm database file (.pdb) into a # Q&A .txt file which can be subsequently used by supermemox import PyritePublisher.prc import struct import sys from cStringIO import StringIO if len(sys.argv) < 3: sys.exit("Usage: %s inputfile_pdb outfile_txt" % sys.argv[0]) infile = PyritePublisher.prc.File(sys.argv[1]) outfile = open(sys.argv[2], "w") print >>outfile, "/ Converted from", sys.argv[1] print >>outfile UNFILED_CAT = "Unfiled" # Unfiled category is always valid cats = {0xffff: UNFILED_CAT} print "total records %s" % infile.getRecords() # find all category records print "processing categories" for r in xrange(infile.getRecords()): raw, i, id, attr, cat = infile.getRecord(r) print (raw, id, attr, cat) rawF = StringIO(raw) (type,) = struct.unpack(">H", rawF.read(struct.calcsize(">H"))) type &= 0x7fff print type if type == 3: # category record temp = ">32s16s16s16s16s16s16s" #print "cat size: %s raw len: %s" % (struct.calcsize(temp), len(raw)) (cat, f1, f2, f3, f4, f5, f6) = \ struct.unpack(temp, rawF.read(struct.calcsize(temp))) print "category record: " + cat cat = cat[:cat.index("\x00")] # discard trailing trash if cat != UNFILED_CAT: cats[len(cats) - 1] = cat print "processing items" for r in xrange(infile.getRecords()): raw, i, id, attr, cat = infile.getRecord(r) print (raw, id, attr, cat) rawF = StringIO(raw) (type,) = struct.unpack(">H", rawF.read(struct.calcsize(">H"))) type &= 0x7fff print type if type == 2: # item record temp = ">HHH12s" (cat, fill, date, learning) = \ struct.unpack(temp, rawF.read(struct.calcsize(temp))) fields = rawF.read().split('\x00') print fields print "guessed cat: %s" % (cats[cat]) assert(len(fields) == 7) # last always empty the way split works print >>outfile, "C:", cats[cat] if fields[0]: print >>outfile, "Q:", fields[0] if fields[1]: print >>outfile, "A:", fields[1] for f in range(2, 6): if fields[f]: print >>outfile, "%s: %s" % (f, fields[f]) print >>outfile