view roundup/rcsv.py @ 2077:3e0961d6d44d

Added the "actor" property. Metakit backend not done (still not confident I know how it's supposed to work ;) Currently it will come up as NULL in the RDBMS backends for older items. The *dbm backends will look up the journal. I hope to remedy the former before 0.7's release. Fixed a bunch of migration issues in the rdbms backends while I was at it (index changes for key prop changes) and simplified the class table update code for RDBMSes that have "alter table" in their command set (ie. not sqlite) ... migration from "version 1" to "version 2" still hasn't actually been tested yet though.
author Richard Jones <richard@users.sourceforge.net>
date Mon, 15 Mar 2004 05:50:20 +0000
parents 261c2e6ceb1e
children c20c9d62cf99
line wrap: on
line source

"""Supplies a Python-2.3 Object Craft csv module work-alike to the extent
needed by Roundup using the Python 2.3 csv module.
"""
__docformat__ = 'restructuredtext'

from roundup.i18n import _
from cStringIO import StringIO
error = """
Sorry, you need a csv module. Either upgrade your Python to 2.3 or later,
or get and install the csv module from:
http://www.object-craft.com.au/projects/csv/
"""
try:
    import csv
    try:
        _reader = csv.reader
        writer = csv.writer
        excel = csv.excel
        error = ''
    except AttributeError:
        # fake it all up using the Object-Craft CSV module
        class excel:
            pass
        if hasattr(csv, 'parser'):
            error = ''
            def _reader(fileobj, dialect=excel):
                # note real readers take an iterable but 2.1 doesn't
                # support iterable access to file objects.
                result = []
                p = csv.parser(field_sep=dialect.delimiter)

                while 1:
                    line = fileobj.readline()
                    if not line: break
 
                    # parse lines until we get a complete entry
                    while 1:
                        fields = p.parse(line)
                        if fields: break
                        line = fileobj.readline()
                        if not line:
                            raise ValueError, "Unexpected EOF during CSV parse"
                    result.append(fields)
                return result
            class writer:
                def __init__(self, fileobj, dialect=excel):
                    self.fileobj = fileobj
                    self.p = csv.parser(field_sep = dialect.delimiter)
                def writerow(self, fields):
                    print >>self.fileobj, self.p.join(fields)
                def writerows(self, rows):
                    for fields in rows:
                        print >>self.fileobj, self.p.join(fields)

except ImportError:
    class excel:
        pass
       
class colon_separated(excel):
    delimiter = ':' 
class comma_separated(excel):
    delimiter = ',' 

def reader(fileobject, dialect=excel):
    csv_lines = [line for line in fileobject.readlines() if line.strip()]
    return _reader(StringIO(''.join(csv_lines)), dialect)

if __name__ == "__main__":
    f=open('testme.txt', 'r')
    r = reader(f, colon_separated)
    remember = []
    for record in r:
        print record
        remember.append(record)
    f.close()
    import sys
    w = writer(sys.stdout, colon_separated)
    w.writerows(remember)


Roundup Issue Tracker: http://roundup-tracker.org/