annotate roundup/rcsv.py @ 2002:b046b65e51db

if you're going to enforce class-level permissions... ...then enforce them at the class level
author Richard Jones <richard@users.sourceforge.net>
date Wed, 11 Feb 2004 00:22:20 +0000
parents 0afdf96ea9d7
children fc52d57c6c3e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1767
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
1 """
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
2 Supplies a Python-2.3 Object Craft csv module work-alike to the extent
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
3 needed by Roundup using the Python 2.3 csv module.
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
4
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
5 """
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
6
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
7 from roundup.i18n import _
1862
0afdf96ea9d7 Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents: 1767
diff changeset
8 from cStringIO import StringIO
1767
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
9 error = """Sorry, you need a module compatible with the csv module.
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
10 Either upgrade your Python to 2.3 or later, or get and install
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
11 the csv module from:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
12 http://www.object-craft.com.au/projects/csv/
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
13
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
14 These two csv modules are different but Roundup can use either.
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
15 """
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
16 try:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
17 import csv
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
18 try:
1862
0afdf96ea9d7 Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents: 1767
diff changeset
19 _reader = csv.reader
1767
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
20 writer = csv.writer
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
21 excel = csv.excel
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
22 error = ''
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
23 except AttributeError:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
24 # fake it all up using the Object-Craft CSV module
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
25 class excel:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
26 pass
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
27 if hasattr(csv, 'parser'):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
28 error = ''
1862
0afdf96ea9d7 Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents: 1767
diff changeset
29 def _reader(fileobj, dialect=excel):
1767
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
30 # note real readers take an iterable but 2.1 doesn't
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
31 # support iterable access to file objects.
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
32 result = []
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
33 p = csv.parser(field_sep=dialect.delimiter)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
34
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
35 while 1:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
36 line = fileobj.readline()
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
37 if not line: break
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
38
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
39 # parse lines until we get a complete entry
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
40 while 1:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
41 fields = p.parse(line)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
42 if fields: break
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
43 line = fileobj.readline()
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
44 if not line:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
45 raise ValueError, "Unexpected EOF during CSV parse"
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
46 result.append(fields)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
47 return result
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
48 class writer:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
49 def __init__(self, fileobj, dialect=excel):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
50 self.fileobj = fileobj
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
51 self.p = csv.parser(field_sep = dialect.delimiter)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
52 def writerow(self, fields):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
53 print >>self.fileobj, self.p.join(fields)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
54 def writerows(self, rows):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
55 for fields in rows:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
56 print >>self.fileobj, self.p.join(fields)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
57
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
58 except ImportError:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
59 class excel:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
60 pass
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
61
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
62 class colon_separated(excel):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
63 delimiter = ':'
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
64 class comma_separated(excel):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
65 delimiter = ','
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
66
1862
0afdf96ea9d7 Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents: 1767
diff changeset
67 def reader(fileobject, dialect=excel):
0afdf96ea9d7 Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents: 1767
diff changeset
68 csv_lines = [line for line in fileobject.readlines() if line.strip()]
0afdf96ea9d7 Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents: 1767
diff changeset
69 return _reader(StringIO(''.join(csv_lines)), dialect)
1767
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
70
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
71 if __name__ == "__main__":
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
72 f=open('testme.txt', 'r')
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
73 r = reader(f, colon_separated)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
74 remember = []
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
75 for record in r:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
76 print record
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
77 remember.append(record)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
78 f.close()
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
79 import sys
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
80 w = writer(sys.stdout, colon_separated)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
81 w.writerows(remember)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
82

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