Mercurial > p > roundup > code
annotate roundup/rcsv.py @ 2005:fc52d57c6c3e
documentation cleanup
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Wed, 11 Feb 2004 23:55:10 +0000 |
| parents | 0afdf96ea9d7 |
| children | 261c2e6ceb1e |
| rev | line source |
|---|---|
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1862
diff
changeset
|
1 """Supplies a Python-2.3 Object Craft csv module work-alike to the extent |
|
1767
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
2 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
|
3 """ |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1862
diff
changeset
|
4 __docformat__ = 'restructuredtext' |
|
1767
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 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
|
7 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
|
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: |
|
1862
0afdf96ea9d7
Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1767
diff
changeset
|
18 _reader = csv.reader |
|
1767
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 = '' |
|
1862
0afdf96ea9d7
Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1767
diff
changeset
|
28 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
|
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 |
|
1862
0afdf96ea9d7
Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1767
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
70 if __name__ == "__main__": |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
71 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
|
72 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
|
73 remember = [] |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
74 for record in r: |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
75 print record |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
76 remember.append(record) |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
77 f.close() |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
78 import sys |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
79 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
|
80 w.writerows(remember) |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
81 |
