annotate roundup/rcsv.py @ 1767:fdaa0b751355

python2.3 CSV support, also missing thankyou in index.txt :)
author Richard Jones <richard@users.sourceforge.net>
date Thu, 28 Aug 2003 04:46:54 +0000
parents
children 0afdf96ea9d7 3555941caea0
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 _
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
8 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
9 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
10 the csv module from:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
11 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
12
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
13 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
14 """
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
15 try:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
16 import csv
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
17 try:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
18 reader = csv.reader
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
19 writer = csv.writer
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
20 excel = csv.excel
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
21 error = ''
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
22 except AttributeError:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
23 # 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
24 class excel:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
25 pass
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
26 if hasattr(csv, 'parser'):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
27 error = ''
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
28 def reader(fileobj, dialect=excel):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
29 # 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
30 # 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
31 result = []
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
32 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
33
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
34 while 1:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
35 line = fileobj.readline()
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
36 if not line: break
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
37
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
38 # 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
39 while 1:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
40 fields = p.parse(line)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
41 if fields: break
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
42 line = fileobj.readline()
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
43 if not line:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
44 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
45 result.append(fields)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
46 return result
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
47 class writer:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
48 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
49 self.fileobj = fileobj
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
50 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
51 def writerow(self, fields):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
52 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
53 def writerows(self, rows):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
54 for fields in rows:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
55 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
56
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
57 except ImportError:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
58 class excel:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
59 pass
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
60
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
61 class colon_separated(excel):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
62 delimiter = ':'
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
63 class comma_separated(excel):
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
64 delimiter = ','
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
65
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
66
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
67 if __name__ == "__main__":
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
68 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
69 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
70 remember = []
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
71 for record in r:
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
72 print record
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
73 remember.append(record)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
74 f.close()
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
75 import sys
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
76 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
77 w.writerows(remember)
fdaa0b751355 python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
78

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