Skip to content

Commit

Permalink
finished python 3 conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
edcrewe committed Oct 29, 2014
1 parent 7c32660 commit 7802e07
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
9 changes: 4 additions & 5 deletions csvimport/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,21 @@ def open_csvfile(self, datafile):
except:
self.loglist.append('Failed to open file %s' % datafile)
if content:
content = str(content[0])
content = content[0]
if pyversion == 3:
content = content.decode(self.charset)
for ending in ('\r\n', '\r', '\\r', '\n'):
if content.find(ending) > -1:
rows = content.split(ending)
break
# Python 3 handling
if rows and rows[0].startswith("b'"):
rows[0] = rows[0][2:]
if rows:
for row in rows:
if type(row) == type(''):
#FIXME: Works for test fixtures - but rather hacky csvreader replacement regex splitter
row = row.replace(',,', ', ,')
row = csvsplit.split(row)
row = [item for item in row if item and item not in (',', '"', "'")]
if pyversion == 2: # Its all unicode if its 3
if pyversion == 2:
try:
row = [unicode(item, self.charset) for item in row]
except:
Expand Down
10 changes: 6 additions & 4 deletions csvimport/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Meta:
db_table = u'csvtests_country'
managed = True

def __unicode__(self):
def __str__(self):
return u"%s (%s)" % (self.name, self.code)


Expand All @@ -29,15 +29,14 @@ class Meta:
db_table = u'csvtests_unitofmeasure'
managed = True

def __unicode__(self):
def __str__(self):
return self.name


class Organisation(models.Model):
name = models.CharField(max_length=255)


def __unicode__(self):
def __str__(self):
return self.name

class Meta:
Expand Down Expand Up @@ -68,3 +67,6 @@ class Meta:
db_table = u'csvtests_item'
managed = True

def __str__(self):
return self.description

8 changes: 6 additions & 2 deletions csvimport/tests/parse_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Use unicode source code to make test character string writing easier
from csvimport.tests.testcase import CommandTestCase
from csvimport.tests.models import Item

import sys
pyversion = sys.version_info[0] # python 2 or 3

class CommandParseTest(CommandTestCase):
""" Run test of file parsing """
Expand Down Expand Up @@ -35,7 +36,10 @@ def test_char2(self, filename='test_char2.csv'):
self.assertEqual(item.description,
"TENTE FAMILIALE, 12 m_, COMPLETE (tapis de sol/double toit)")
self.assertEqual(item.quantity, 101)
self.assertEqual(unicode(item.uom), u'删除当前图片')
if pyversion == 2:
self.assertEqual(unicode(item.uom), u'删除当前图片')
else:
self.assertEqual(str(item.uom), '删除当前图片')
self.assertEqual(item.organisation.name, 'AID-France')
Item.objects.all().delete()

Expand Down
2 changes: 1 addition & 1 deletion csvimport/tests/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ def get_item(self, code_share='sheeting'):
item = Item.objects.get(code_share__exact=code_share)
except ObjectDoesNotExist:
item = None
self.assertTrue(item, 'Failed to get row from imported test.csv Items')
self.assertTrue(item, 'Failed to get row from imported test.csv Items')
return item

0 comments on commit 7802e07

Please sign in to comment.