Mercurial > p > roundup > code
annotate roundup/rcsv.py @ 2565:89c5e8564dad
passs the message translator to cgitb functions
| author | Alexander Smishlajev <a1s@users.sourceforge.net> |
|---|---|
| date | Tue, 13 Jul 2004 10:19:13 +0000 |
| parents | c20c9d62cf99 |
| children |
| 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 |
|
2073
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
8 error = """ |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
9 Sorry, you need a csv module. Either upgrade your Python to 2.3 or later, |
|
261c2e6ceb1e
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
10 or get and install the csv module from: |
|
1767
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 try: |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
14 import csv |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
15 try: |
|
1862
0afdf96ea9d7
Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1767
diff
changeset
|
16 _reader = csv.reader |
|
1767
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
17 writer = csv.writer |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
18 excel = csv.excel |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
19 error = '' |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
20 except AttributeError: |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
21 # 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
|
22 class excel: |
|
2190
c20c9d62cf99
really fix the missing delimeter problem
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
23 delimiter = ':' |
|
1767
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
24 if hasattr(csv, 'parser'): |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
25 error = '' |
|
1862
0afdf96ea9d7
Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1767
diff
changeset
|
26 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
|
27 # 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
|
28 # 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
|
29 result = [] |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
30 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
|
31 |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
32 while 1: |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
33 line = fileobj.readline() |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
34 if not line: break |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
35 |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
36 # 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
|
37 while 1: |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
38 fields = p.parse(line) |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
39 if fields: break |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
40 line = fileobj.readline() |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
41 if not line: |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
42 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
|
43 result.append(fields) |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
44 return result |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
45 class writer: |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
46 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
|
47 self.fileobj = fileobj |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
48 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
|
49 def writerow(self, fields): |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
50 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
|
51 def writerows(self, rows): |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
52 for fields in rows: |
|
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 |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
55 except ImportError: |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
56 class excel: |
|
2190
c20c9d62cf99
really fix the missing delimeter problem
Richard Jones <richard@users.sourceforge.net>
parents:
2073
diff
changeset
|
57 delimiter = ':' |
|
1767
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
58 |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
59 class colon_separated(excel): |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
60 delimiter = ':' |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
61 class comma_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 |
|
1862
0afdf96ea9d7
Add a wrapper around the two different reader() functions...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1767
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
68 if __name__ == "__main__": |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
69 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
|
70 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
|
71 remember = [] |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
72 for record in r: |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
73 print record |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
74 remember.append(record) |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
75 f.close() |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
76 import sys |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
77 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
|
78 w.writerows(remember) |
|
fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
79 |
