root/stuff/sandbox/sourceforge/csv-export.py

Revision 351 (by effbot, 07/12/06 12:24:59)

robustificaton (from xml-export)

# $Id$
# example: generate CSV export file

# note: this exporter only includes a subset of all information.  most
# importantly, attached files are not included.

import csv, os, re, sys
import htmlload
import extract

def fixtext(s):
    return s.encode("utf-8")

def fixdate(s):
    return s

def fixuser(s):
    return s.rsplit(" - ", 1)[-1]

def fixstatus(s):
    return s.upper()

FIELDS = [
    # tag, handler
    ("item_id", fixtext),
    ("category", fixtext),
    ("date_submitted", fixdate),
    ("date_last_updated", fixdate),
    ("priority", fixstatus),
    ("resolution", fixstatus),
    ("status", fixstatus),
    ("submitted_by", fixuser),
    ("summary", fixtext),
]

tracker = sys.argv[1]
if tracker.startswith("tracker-"):
    dirname = tracker
else:
    dirname = "tracker-" + tracker

while not os.path.basename(dirname):
    dirname = os.path.dirname(dirname)

file = open(dirname + ".csv", "w")

out = csv.writer(file)

out.writerow([key for key, handler in FIELDS])

files = extract.getpagefiles(dirname)

for index, pagefile in enumerate(files):
    print >>sys.stderr, "\r", pagefile, "-", index, "of", len(files),
    try:
	info = extract.extract(pagefile)
    except IOError, v:
	print >>sys.stderr, "\n", "*** failed to read", pagefile, "-", v
	continue
    # fields
    row = []
    for key, handler in FIELDS:
        value = handler(info.get(key, ""))
        row.append(value)
    # add comments to the end of the row
    for comment in info.get("comments", []):
        row.append(fixtext(comment.get("description", "")))
    out.writerow(row)

file.close()

print
print file.name, "ok"

Note: See TracBrowser for help on using the browser.