Skip to content

Commit

Permalink
Port code to Python 3.8.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed Apr 25, 2020
1 parent 355cdaf commit 05a80bf
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,6 +7,7 @@
.installed.cfg
.mr.developer.cfg
.pytest_cache
.tox/
bin/
cache/*.py
develop-eggs/
Expand Down
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -2,3 +2,7 @@ version: ~> 1.0
language: python
import:
- source: icemac/icemac.addressbook:.travis-blueprint.yml
mode: merge
python:
- 2.7
- 3.8
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -7,6 +7,8 @@

- Adapt code to `icemac.addressbook >= 9.5`.

- Port code to Python 3.8.


2.12 (2020-03-24)
=================
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Expand Up @@ -45,9 +45,9 @@ or fork me at https://github.com/icemac/icemac.ab.importer.
Running Tests
=============

To run the tests yourself call::
To run the tests yourself call inside the checkout::

$ virtualenv-2.7 .
$ python3 -m venv .
$ bin/pip install zc.buildout
$ bin/buildout
$ bin/py.test
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Expand Up @@ -29,8 +29,10 @@ def read(*path_elements):
'Natural Language :: English',
'Natural Language :: German',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2 :: Only',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: Implementation :: CPython',
],
packages=setuptools.find_packages('src'),
Expand All @@ -46,6 +48,7 @@ def read(*path_elements):
'icemac.truncatetext >= 0.2',
'pytz',
'setuptools',
'six',
'z3c.wizard',
'zc.sourcefactory',
'zope.container',
Expand Down
2 changes: 1 addition & 1 deletion src/icemac/__init__.py
@@ -1 +1 @@
__import__('pkg_resources').declare_namespace(__name__)
__import__('pkg_resources').declare_namespace(__name__) # pragma: no cover
2 changes: 1 addition & 1 deletion src/icemac/ab/__init__.py
@@ -1 +1 @@
__import__('pkg_resources').declare_namespace(__name__)
__import__('pkg_resources').declare_namespace(__name__) # pragma: no cover
4 changes: 2 additions & 2 deletions src/icemac/ab/importer/browser/conftest.py
@@ -1,12 +1,12 @@
from StringIO import StringIO
from io import BytesIO
import pytest


@pytest.fixture('session')
def import_file():
"""Get a function to create a file like object for import."""
def create_file(data):
file = StringIO()
file = BytesIO()
file.write(data)
file.seek(0)
return file
Expand Down
2 changes: 1 addition & 1 deletion src/icemac/ab/importer/browser/importer.py
Expand Up @@ -52,7 +52,7 @@ def setUpColumns(self):

@property
def values(self):
return self.context.values()
return list(self.context.values())


def provide_IImportFile(file):
Expand Down
12 changes: 6 additions & 6 deletions src/icemac/ab/importer/browser/tests/test_importer.py
Expand Up @@ -7,8 +7,8 @@
@pytest.fixture('function')
def importer_link(browser):
"""Generate the importer link rendered on the master data view."""
return '<a href="{}"><span>Import data</span></a>'.format(
browser.IMPORTER_OVERVIEW_URL)
return b'<a href="%s"><span>Import data</span></a>' % (
browser.IMPORTER_OVERVIEW_URL.encode('ascii'))


# Tests
Expand All @@ -26,7 +26,7 @@ def test_importer__CRUD__1(address_book, browser, import_file):
browser.getLink('file').click()
assert browser.IMPORTER_FILE_ADD_URL == browser.url
browser.getControl('file').add_file(
import_file('Import data file'), 'text/plain', 'file.txt')
import_file(b'Import data file'), 'text/plain', 'file.txt')
browser.getControl('Add').click()
assert '"file.txt" added.' == browser.message
# List the file
Expand All @@ -44,12 +44,12 @@ def test_importer__CRUD__1(address_book, browser, import_file):
assert 'text/plain' == browser.headers['content-type']
assert ('attachment; filename=file.txt' ==
browser.headers['content-disposition'])
assert 'Import data file' == browser.contents
assert b'Import data file' == browser.contents
# It is possible to upload a new file instead of the previously uploaded
# one:
browser.open(browser.IMPORTER_FILE_EDIT_URL)
browser.getControl('file', index=1).add_file(
import_file('Import2 data2 file2'), 'text/csv', 'file2.csv')
import_file(b'Import2 data2 file2'), 'text/csv', 'file2.csv')
browser.getControl('Save').click()
assert 'Data successfully updated.' == browser.message
assert browser.IMPORTER_OVERVIEW_URL == browser.url
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_importer__Add__2(address_book, browser, import_file):
browser.login('mgr')
browser.open(browser.IMPORTER_FILE_ADD_URL)
browser.getControl('file').add_file(
import_file('Import data file'), 'text/plain', 'file.txt')
import_file(b'Import data file'), 'text/plain', 'file.txt')
browser.getControl('Add').click()
assert '"file.txt" added.' == browser.message
assert IImportFile.providedBy(address_book.importer['File'])
Expand Down
27 changes: 16 additions & 11 deletions src/icemac/ab/importer/browser/wizard/map.py
Expand Up @@ -16,6 +16,8 @@
import zope.interface
import zope.schema
import zope.security.proxy
import six
from six.moves import range


NONE_REPLACEMENT = object()
Expand Down Expand Up @@ -47,7 +49,7 @@ class ImportFields(zc.sourcefactory.contextual.BasicContextualSourceFactory):
"""Where to put the imported data in the addressbook."""

def getValues(self, context):
return xrange(len(list(get_reader(context).getFieldNames())))
return range(len(list(get_reader(context).getFieldNames())))

def getTitle(self, context, value):
reader = get_reader(context)
Expand Down Expand Up @@ -127,7 +129,7 @@ def bool_field(value, field):
if value is None:
# We can't return None, as this means that the adapter can't adapt.
return NONE_REPLACEMENT
if not isinstance(value, unicode):
if not isinstance(value, six.text_type):
return value # produces an error in validation
value = value.lower()
if value in TRUE_VALUES:
Expand Down Expand Up @@ -158,7 +160,10 @@ def uri_field(value, field):
if value is None:
# We can't return None, as this means that the adapter can't adapt.
return NONE_REPLACEMENT
return value.strip().encode('ascii')
value = value.strip()
if six.PY2: # pragma: no cover
value = value.encode('ascii')
return value


@zope.component.adapter(None, zope.schema.interfaces.IChoice)
Expand Down Expand Up @@ -356,10 +361,10 @@ def __init__(self, user_data, address_book, entries_number):
icemac.addressbook.interfaces.IEntities)
self.import_entities = entities.getMainEntities()
for entity in self.import_entities:
for index in xrange(self.entries_number):
for index in range(self.entries_number):
key = "%s-%s" % (entity.name, index)
setattr(self, key, {})
for field_desc, index in user_data.iteritems():
for field_desc, index in six.iteritems(user_data):
if index is None:
continue # field was not selected for import
prefix, field_name = field_desc.split('.')
Expand All @@ -380,7 +385,7 @@ def create(self, data):
if entity.class_name == 'icemac.addressbook.person.Person':
# already handled
continue
for index in xrange(self.entries_number):
for index in range(self.entries_number):
prefix = "%s-%s" % (entity.name, index)
main_entry = (index == 0)
obj = self._create(prefix, person, data, main_entry)
Expand Down Expand Up @@ -412,11 +417,11 @@ def _create(self, prefix, parent, data, creation_required):
obj = icemac.addressbook.utils.create_obj(entity.getClass())

# set the values
for field_name, index in field_mapping.iteritems():
for field_name, index in six.iteritems(field_mapping):
field = entity.getField(field_name)
try:
value = data[index]
except IndexError, e:
except IndexError as e:
self.errors.add(render_error(entity, '', e))
else:
# try named converter first
Expand All @@ -431,7 +436,7 @@ def _create(self, prefix, parent, data, creation_required):
context = field.interface(obj)
try:
field.set(context, conv_value)
except zope.interface.Invalid, e:
except zope.interface.Invalid as e:
self.errors.add(render_error(entity, field_name, e))

icemac.addressbook.utils.add(parent, obj)
Expand All @@ -444,7 +449,7 @@ def _validate(self, entity, obj):
value = field.get(context)
try:
field.validate(value)
except zope.schema.ValidationError, exc:
except zope.schema.ValidationError as exc:
self.errors.add(render_error(entity, field_name, exc))


Expand Down Expand Up @@ -498,7 +503,7 @@ def __init__(self, *args, **kw):
else:
entries_number = session.get('entries_number', 0)
main_prefix = _(u'main')
for index in xrange(entries_number):
for index in range(entries_number):
if index == 0:
row_title_prefix = main_prefix
else:
Expand Down
6 changes: 4 additions & 2 deletions src/icemac/ab/importer/browser/wizard/review.py
Expand Up @@ -12,6 +12,8 @@
import zope.interface
import zope.schema
import zope.security.proxy
import six
from six.moves import range


class ContainerColumn(z3c.table.column.GetAttrColumn):
Expand Down Expand Up @@ -124,7 +126,7 @@ def setUpColumns(self):
else:
entries_number = self.session['entries_number']
main_prefix = _(u'main')
for index in xrange(entries_number):
for index in range(entries_number):
first = True
for field_name, field in entity.getFields():
weight += 1
Expand Down Expand Up @@ -181,7 +183,7 @@ def _renderErrors(self, item, cssClass):
u'<ul class="errors">'])
for error in errors:
result.append(
u'<li>%s</li>' % xml.sax.saxutils.escape(unicode(
u'<li>%s</li>' % xml.sax.saxutils.escape(six.text_type(
zope.i18n.translate(error, context=self.request))))
result.extend([u'</ul>',
u'</td>',
Expand Down
4 changes: 2 additions & 2 deletions src/icemac/ab/importer/browser/wizard/tests/test_edit.py
Expand Up @@ -4,7 +4,7 @@
def test_edit__EditFile__1(
address_book, browser, import_file, ImportFileFactory):
"""It allows to replace the import file."""
ImportFileFactory(address_book, '1.csv', 'last_name')
ImportFileFactory(address_book, '1.csv', ['last_name'])
browser.login('mgr')
browser.open(browser.IMPORTER_FILE_IMPORT_URL)
browser.getControl('Back').click()
Expand All @@ -18,4 +18,4 @@ def test_edit__EditFile__1(
assert browser.IMPORTER_IMPORT_EDIT_URL == browser.url
assert 'header.csv' == browser.getControl('name').value
browser.getLink('Download file').click()
assert 'first_name' == browser.contents
assert b'first_name' == browser.contents
28 changes: 14 additions & 14 deletions src/icemac/ab/importer/browser/wizard/tests/test_review.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from StringIO import StringIO
from io import BytesIO
from icemac.addressbook.interfaces import IKeywords
from mock import patch
import gocept.country.db
Expand Down Expand Up @@ -28,19 +28,19 @@ def example_data():
is used as an example here.)
"""
file_data = StringIO()
file_data.write('\n'.join([
'last_name,birth_date,e_mail_addr,www,asdf,countries,pnotes',
',1999-09-09,,,asdf,,', # missing last name
'"wrong date",1981,,,,,,',
'"wrong e-mail",,i@me@de,,,,',
'"wrong home page address",,i@me.de,asdf,,,',
'"wrong country",,,,,D,',
'"country ISO code",,,,,AT,',
'"not enough fields",,',
'" nice "," 2006-06-06", r6@ab.info," http://www.r6.sf.net",,'
'" Switzerland ",'
'" my r e a l l y long, but also really nice notes "']))
file_data = BytesIO()
file_data.write(b'\n'.join([
b'last_name,birth_date,e_mail_addr,www,asdf,countries,pnotes',
b',1999-09-09,,,asdf,,', # missing last name
b'"wrong date",1981,,,,,,',
b'"wrong e-mail",,i@me@de,,,,',
b'"wrong home page address",,i@me.de,asdf,,,',
b'"wrong country",,,,,D,',
b'"country ISO code",,,,,AT,',
b'"not enough fields",,',
b'" nice "," 2006-06-06", r6@ab.info," http://www.r6.sf.net",,'
b'" Switzerland ",'
b'" my r e a l l y long, but also really nice notes "']))
file_data.seek(0)
return file_data

Expand Down
4 changes: 3 additions & 1 deletion src/icemac/ab/importer/conftest.py
Expand Up @@ -33,8 +33,10 @@ def ImportFileFactory(FileFactory):
data ... list of the lines in the import file.
"""
def create_file(address_book, filename, data, **kw):

file = FileFactory(
address_book.importer, filename, data='\n'.join(data), **kw)
address_book.importer, filename,
data=b'\n'.join(x.encode('utf-8') for x in data), **kw)
provide_IImportFile(file)
return file
return create_file
Expand Down

0 comments on commit 05a80bf

Please sign in to comment.