Skip to content

Commit 32fb3f0

Browse files
author
Richard Jones
committed
merge CSV fix from HEAD
1 parent 151a6c3 commit 32fb3f0

File tree

5 files changed

+38
-64
lines changed

5 files changed

+38
-64
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Fixed:
1010
- fix CGI editCSV action to handle metakit's integer itemids
1111
- apply fix for "remove" links from Klamer Schutte
1212
- added permission check on "remove" link while I was there..
13+
- applied CSV fix for python2.3 (thanks Paul Dubois for the patch)
1314

1415

1516
2003-08-08 0.6.0

doc/index.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ Duncan Booth,
5656
Seb Brezel,
5757
Titus Brown,
5858
Roch'e Compaan,
59+
Paul F. Dubois,
5960
Jeff Epler,
6061
Hernan Martinez Foffani,
6162
Ajit George,
63+
Johannes Gijsbers,
6264
Gus Gollings,
6365
Dan Grassi,
6466
Engelbert Gruber,

roundup/admin.py

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1717
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1818
#
19-
# $Id: admin.py,v 1.55 2003-06-23 08:05:30 neaj Exp $
19+
# $Id: admin.py,v 1.55.2.1 2003-08-28 04:53:04 richard Exp $
2020

2121
'''Administration commands for maintaining Roundup trackers.
2222
'''
2323

2424
import sys, os, getpass, getopt, re, UserDict, shutil, rfc822
25-
try:
26-
import csv
27-
except ImportError:
28-
csv = None
29-
from roundup import date, hyperdb, roundupdb, init, password, token
25+
from roundup import date, hyperdb, roundupdb, init, password, token, rcsv
3026
from roundup import __version__ as roundup_version
3127
import roundup.instance
3228
from roundup.i18n import _
@@ -1063,15 +1059,12 @@ def do_export(self, args):
10631059
colon-separated-value files that are placed in the nominated
10641060
destination directory. The journals are not exported.
10651061
'''
1066-
# we need the CSV module
1067-
if csv is None:
1068-
raise UsageError, \
1069-
_('Sorry, you need the csv module to use this function.\n'
1070-
'Get it from: http://www.object-craft.com.au/projects/csv/')
1071-
10721062
# grab the directory to export to
10731063
if len(args) < 1:
10741064
raise UsageError, _('Not enough arguments supplied')
1065+
if rcsv.error:
1066+
raise UsageError, _(rcsv.error)
1067+
10751068
dir = args[-1]
10761069

10771070
# get the list of classes to export
@@ -1080,26 +1073,24 @@ def do_export(self, args):
10801073
else:
10811074
classes = self.db.classes.keys()
10821075

1083-
# use the csv parser if we can - it's faster
1084-
p = csv.parser(field_sep=':')
1085-
10861076
# do all the classes specified
10871077
for classname in classes:
10881078
cl = self.get_class(classname)
10891079
f = open(os.path.join(dir, classname+'.csv'), 'w')
1080+
writer = rcsv.writer(f, rcsv.colon_separated)
10901081
properties = cl.getprops()
10911082
propnames = properties.keys()
10921083
propnames.sort()
1093-
l = propnames[:]
1094-
l.append('is retired')
1095-
print >> f, p.join(l)
1084+
fields = propnames[:]
1085+
fields.append('is retired')
1086+
writer.writerow(fields)
10961087

10971088
# all nodes for this class (not using list() 'cos it doesn't
10981089
# include retired nodes)
10991090

11001091
for nodeid in self.db.getclass(classname).getnodeids():
11011092
# get the regular props
1102-
print >>f, p.join(cl.export_list(propnames, nodeid))
1093+
writer.writerow (cl.export_list(propnames, nodeid))
11031094

11041095
# close this file
11051096
f.close()
@@ -1122,11 +1113,8 @@ class to import.
11221113
'''
11231114
if len(args) < 1:
11241115
raise UsageError, _('Not enough arguments supplied')
1125-
if csv is None:
1126-
raise UsageError, \
1127-
_('Sorry, you need the csv module to use this function.\n'
1128-
'Get it from: http://www.object-craft.com.au/projects/csv/')
1129-
1116+
if rcsv.error:
1117+
raise UsageError, _(rcsv.error)
11301118
from roundup import hyperdb
11311119

11321120
for file in os.listdir(args[0]):
@@ -1141,8 +1129,9 @@ class to import.
11411129

11421130
# ensure that the properties and the CSV file headings match
11431131
cl = self.get_class(classname)
1144-
p = csv.parser(field_sep=':')
1145-
file_props = p.parse(f.readline())
1132+
reader = rcsv.reader(f, rcsv.colon_separated)
1133+
file_props = None
1134+
maxid = 1
11461135

11471136
# XXX we don't _really_ need to do this...
11481137
# properties = cl.getprops()
@@ -1155,22 +1144,15 @@ class to import.
11551144
# 'properties as "%(arg0)s".')%{'arg0': args[0]}
11561145

11571146
# loop through the file and create a node for each entry
1158-
maxid = 1
1159-
while 1:
1160-
line = f.readline()
1161-
if not line: break
1162-
1163-
# parse lines until we get a complete entry
1164-
while 1:
1165-
l = p.parse(line)
1166-
if l: break
1167-
line = f.readline()
1168-
if not line:
1169-
raise ValueError, "Unexpected EOF during CSV parse"
1147+
for r in reader:
1148+
if file_props is None:
1149+
file_props = r
1150+
continue
11701151

11711152
# do the import and figure the current highest nodeid
1172-
maxid = max(maxid, int(cl.import_list(file_props, l)))
1153+
maxid = max(maxid, int(cl.import_list(file_props, r)))
11731154

1155+
# set the id counter
11741156
print 'setting', classname, maxid+1
11751157
self.db.setid(classname, str(maxid+1))
11761158
return 0

roundup/cgi/client.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: client.py,v 1.130.2.1 2003-08-28 04:21:22 richard Exp $
1+
# $Id: client.py,v 1.130.2.2 2003-08-28 04:53:04 richard Exp $
22

33
__doc__ = """
44
WWW request handler (also used in the stand-alone server).
@@ -8,7 +8,7 @@
88
import binascii, Cookie, time, random, MimeWriter, smtplib, socket, quopri
99
import stat, rfc822, string
1010

11-
from roundup import roundupdb, date, hyperdb, password, token
11+
from roundup import roundupdb, date, hyperdb, password, token, rcsv
1212
from roundup.i18n import _
1313
from roundup.cgi.templating import Templates, HTMLRequest, NoTemplate
1414
from roundup.cgi import cgitb
@@ -1219,12 +1219,8 @@ def editCSVAction(self):
12191219
_('You do not have permission to edit %s' %self.classname))
12201220

12211221
# get the CSV module
1222-
try:
1223-
import csv
1224-
except ImportError:
1225-
self.error_message.append(_(
1226-
'Sorry, you need the csv module to use this function.<br>\n'
1227-
'Get it from: <a href="http://www.object-craft.com.au/projects/csv/">http://www.object-craft.com.au/projects/csv/'))
1222+
if rcsv.error:
1223+
self.error_message.append(_(rcsv.error))
12281224
return
12291225

12301226
cl = self.db.classes[self.classname]
@@ -1233,16 +1229,13 @@ def editCSVAction(self):
12331229
props = ['id'] + idlessprops
12341230

12351231
# do the edit
1236-
rows = self.form['rows'].value.splitlines()
1237-
p = csv.parser()
1232+
rows = StringIO.StringIO(self.form['rows'].value)
1233+
reader = rcsv.reader(rows, rcsv.comma_separated)
12381234
found = {}
12391235
line = 0
1240-
for row in rows[1:]:
1236+
for values in reader:
12411237
line += 1
1242-
values = p.parse(row)
1243-
# not a complete row, keep going
1244-
if not values: continue
1245-
1238+
if line == 1: continue
12461239
# skip property names header
12471240
if values == props:
12481241
continue

roundup/cgi/templating.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys, cgi, urllib, os, re, os.path, time, errno
22

3-
from roundup import hyperdb, date
3+
from roundup import hyperdb, date, rcsv
44
from roundup.i18n import _
55

66
try:
@@ -395,17 +395,13 @@ def list(self):
395395
def csv(self):
396396
''' Return the items of this class as a chunk of CSV text.
397397
'''
398-
# get the CSV module
399-
try:
400-
import csv
401-
except ImportError:
402-
return 'Sorry, you need the csv module to use this function.\n'\
403-
'Get it from: http://www.object-craft.com.au/projects/csv/'
398+
if rcsv.error:
399+
return rcsv.error
404400

405401
props = self.propnames()
406-
p = csv.parser()
407402
s = StringIO.StringIO()
408-
s.write(p.join(props) + '\n')
403+
writer = rcsv.writer(s, rcsv.comma_separated)
404+
writer.writerow(props)
409405
for nodeid in self._klass.list():
410406
l = []
411407
for name in props:
@@ -416,7 +412,7 @@ def csv(self):
416412
l.append(':'.join(map(str, value)))
417413
else:
418414
l.append(str(self._klass.get(nodeid, name)))
419-
s.write(p.join(l) + '\n')
415+
writer.writerow(l)
420416
return s.getvalue()
421417

422418
def propnames(self):

0 commit comments

Comments
 (0)