Skip to content

Commit

Permalink
Merge pull request #4035 from cclauss/unicode-to-six.text_type
Browse files Browse the repository at this point in the history
Change unicode() --> six.text_type() for Python 3
  • Loading branch information
amercader committed Mar 1, 2018
2 parents 49e0099 + 8cd8551 commit 555e096
Show file tree
Hide file tree
Showing 66 changed files with 281 additions and 189 deletions.
29 changes: 15 additions & 14 deletions ckan/controllers/api.py
Expand Up @@ -9,6 +9,7 @@

from webob.multidict import UnicodeMultiDict
from paste.util.multidict import MultiDict
from six import text_type

import ckan.model as model
import ckan.logic as logic
Expand Down Expand Up @@ -217,14 +218,14 @@ def action(self, logic_function, ver=None):
'message': _('Access denied')}
return_dict['success'] = False

if unicode(e):
if text_type(e):
return_dict['error']['message'] += u': %s' % e

return self._finish(403, return_dict, content_type='json')
except NotFound as e:
return_dict['error'] = {'__type': 'Not Found Error',
'message': _('Not found')}
if unicode(e):
if text_type(e):
return_dict['error']['message'] += u': %s' % e
return_dict['success'] = False
return self._finish(404, return_dict, content_type='json')
Expand Down Expand Up @@ -297,9 +298,9 @@ def list(self, ver=None, register=None, subregister=None, id=None):
try:
return self._finish_ok(action(context, {'id': id}))
except NotFound as e:
return self._finish_not_found(unicode(e))
return self._finish_not_found(text_type(e))
except NotAuthorized as e:
return self._finish_not_authz(unicode(e))
return self._finish_not_authz(text_type(e))

def show(self, ver=None, register=None, subregister=None,
id=None, id2=None):
Expand All @@ -326,9 +327,9 @@ def show(self, ver=None, register=None, subregister=None,
try:
return self._finish_ok(action(context, data_dict))
except NotFound as e:
return self._finish_not_found(unicode(e))
return self._finish_not_found(text_type(e))
except NotAuthorized as e:
return self._finish_not_authz(unicode(e))
return self._finish_not_authz(text_type(e))

def create(self, ver=None, register=None, subregister=None,
id=None, id2=None):
Expand Down Expand Up @@ -369,9 +370,9 @@ def create(self, ver=None, register=None, subregister=None,
return self._finish_ok(response_data,
resource_location=location)
except NotAuthorized as e:
return self._finish_not_authz(unicode(e))
return self._finish_not_authz(text_type(e))
except NotFound as e:
return self._finish_not_found(unicode(e))
return self._finish_not_found(text_type(e))
except ValidationError as e:
# CS: nasty_string ignore
log.info('Validation error (REST create): %r', str(e.error_dict))
Expand Down Expand Up @@ -425,9 +426,9 @@ def update(self, ver=None, register=None, subregister=None,
response_data = action(context, data_dict)
return self._finish_ok(response_data)
except NotAuthorized as e:
return self._finish_not_authz(unicode(e))
return self._finish_not_authz(text_type(e))
except NotFound as e:
return self._finish_not_found(unicode(e))
return self._finish_not_found(text_type(e))
except ValidationError as e:
# CS: nasty_string ignore
log.info('Validation error (REST update): %r', str(e.error_dict))
Expand Down Expand Up @@ -472,9 +473,9 @@ def delete(self, ver=None, register=None, subregister=None,
response_data = action(context, data_dict)
return self._finish_ok(response_data)
except NotAuthorized as e:
return self._finish_not_authz(unicode(e))
return self._finish_not_authz(text_type(e))
except NotFound as e:
return self._finish_not_found(unicode(e))
return self._finish_not_found(text_type(e))
except ValidationError as e:
# CS: nasty_string ignore
log.info('Validation error (REST delete): %r', str(e.error_dict))
Expand Down Expand Up @@ -671,7 +672,7 @@ def dataset_autocomplete(self):

def tag_autocomplete(self):
q = request.str_params.get('incomplete', '')
q = unicode(urllib.unquote(q), 'utf-8')
q = text_type(urllib.unquote(q), 'utf-8')
limit = request.params.get('limit', 10)
tag_names = []
if q:
Expand Down Expand Up @@ -758,7 +759,7 @@ def _get_request_data(cls, try_url_params=False):
def make_unicode(entity):
'''Cast bare strings and strings in lists or dicts to Unicode. '''
if isinstance(entity, str):
return unicode(entity)
return text_type(entity)
elif isinstance(entity, list):
new_items = []
for item in entity:
Expand Down
3 changes: 2 additions & 1 deletion ckan/controllers/feed.py
Expand Up @@ -24,6 +24,7 @@
import logging
import urlparse

from six import text_type
import webhelpers.feedgenerator

import ckan.lib.base as base
Expand Down Expand Up @@ -417,7 +418,7 @@ def output_feed(self, results, feed_title, feed_description,
id=pkg['name'],
ver='3',
qualified=True),
unicode(len(json.dumps(pkg))), # TODO fix this
text_type(len(json.dumps(pkg))), # TODO fix this
u'application/json'),
**additional_fields
)
Expand Down
4 changes: 2 additions & 2 deletions ckan/controllers/group.py
Expand Up @@ -5,7 +5,7 @@
from urllib import urlencode

from pylons.i18n import get_lang
from six import string_types
from six import string_types, text_type

import ckan.lib.base as base
import ckan.lib.helpers as h
Expand Down Expand Up @@ -789,7 +789,7 @@ def history(self, id):
id=c.group_dict['name']),
description=_(u'Recent changes to CKAN Group: ') +
c.group_dict['display_name'],
language=unicode(get_lang()),
language=text_type(get_lang()),
)
for revision_dict in c.group_revisions:
revision_date = h.date_str_to_datetime(
Expand Down
12 changes: 6 additions & 6 deletions ckan/controllers/package.py
Expand Up @@ -9,7 +9,7 @@
from ckan.common import config
from paste.deploy.converters import asbool
import paste.fileapp
from six import string_types
from six import string_types, text_type

import ckan.logic as logic
import ckan.lib.base as base
Expand Down Expand Up @@ -462,7 +462,7 @@ def history(self, id):
id=c.pkg_dict['name']),
description=_(u'Recent changes to CKAN Dataset: ') +
(c.pkg_dict['title'] or ''),
language=unicode(i18n.get_lang()),
language=text_type(i18n.get_lang()),
)
for revision_dict in c.pkg_revisions:
revision_date = h.date_str_to_datetime(
Expand Down Expand Up @@ -941,9 +941,9 @@ def _save_new(self, context, package_type=None):
abort(400, _(u'Integrity Error'))
except SearchIndexError as e:
try:
exc_str = unicode(repr(e.args))
exc_str = text_type(repr(e.args))
except Exception: # We don't like bare excepts
exc_str = unicode(str(e))
exc_str = text_type(str(e))
abort(500, _(u'Unable to add package to search index.') + exc_str)
except ValidationError as e:
errors = e.error_dict
Expand Down Expand Up @@ -989,9 +989,9 @@ def _save_edit(self, name_or_id, context, package_type=None):
abort(400, _(u'Integrity Error'))
except SearchIndexError as e:
try:
exc_str = unicode(repr(e.args))
exc_str = text_type(repr(e.args))
except Exception: # We don't like bare excepts
exc_str = unicode(str(e))
exc_str = text_type(str(e))
abort(500, _(u'Unable to update search index.') + exc_str)
except ValidationError as e:
errors = e.error_dict
Expand Down
3 changes: 2 additions & 1 deletion ckan/controllers/revision.py
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime, timedelta

from pylons.i18n import get_lang
from six import text_type

import ckan.logic as logic
import ckan.lib.base as base
Expand Down Expand Up @@ -44,7 +45,7 @@ def list(self):
title=_(u'CKAN Repository Revision History'),
link=h.url_for(controller='revision', action='list', id=''),
description=_(u'Recent changes to the CKAN repository.'),
language=unicode(get_lang()),
language=text_type(get_lang()),
)
# TODO: make this configurable?
# we do not want the system to fall over!
Expand Down
5 changes: 3 additions & 2 deletions ckan/controllers/user.py
Expand Up @@ -4,6 +4,7 @@

from ckan.common import config
from paste.deploy.converters import asbool
from six import text_type

import ckan.lib.base as base
import ckan.model as model
Expand Down Expand Up @@ -499,7 +500,7 @@ def request_reset(self):
h.redirect_to('/')
except mailer.MailerException as e:
h.flash_error(_('Could not send reset link: %s') %
unicode(e))
text_type(e))
return render('user/request_reset.html')

def perform_reset(self, id):
Expand Down Expand Up @@ -551,7 +552,7 @@ def perform_reset(self, id):
except ValidationError as e:
h.flash_error(u'%r' % e.error_dict)
except ValueError as ve:
h.flash_error(unicode(ve))
h.flash_error(text_type(ve))
user_dict['state'] = user_state

c.user_dict = user_dict
Expand Down
5 changes: 3 additions & 2 deletions ckan/lib/alphabet_paginate.py
Expand Up @@ -18,6 +18,7 @@
from itertools import dropwhile
import re

from six import text_type
from sqlalchemy import __version__ as sqav
from sqlalchemy.orm.query import Query
from webhelpers.html.builder import HTML
Expand Down Expand Up @@ -54,7 +55,7 @@ def __init__(self, collection, alpha_attribute, page, other_text, paging_thresho
# because we grey-out those which aren't.
self.available = dict( (c,0,) for c in self.letters )
for c in self.collection:
if isinstance(c, unicode):
if isinstance(c, text_type):
x = c[0]
elif isinstance(c, dict):
x = c[self.alpha_attribute][0]
Expand Down Expand Up @@ -131,7 +132,7 @@ def items(self):
if self.page != self.other_text:
if isinstance(self.collection[0], dict):
items = [x for x in self.collection if x[self.alpha_attribute][0:1].lower() == self.page.lower()]
elif isinstance(self.collection[0], unicode):
elif isinstance(self.collection[0], text_type):
items = [x for x in self.collection if x[0:1].lower() == self.page.lower()]
else:
items = [x for x in self.collection if getattr(x,self.alpha_attribute)[0:1].lower() == self.page.lower()]
Expand Down
12 changes: 7 additions & 5 deletions ckan/lib/cli.py
Expand Up @@ -16,10 +16,12 @@
from optparse import OptionConflictError
import traceback

from six import text_type
from six.moves import input, xrange
from six.moves.urllib.error import HTTPError
from six.moves.urllib.parse import urljoin, urlparse
from six.moves.urllib.request import urlopen

import sqlalchemy as sa
import routes
import paste.script
Expand Down Expand Up @@ -810,13 +812,13 @@ def add(self):
return
username = self.args[1]

user = model.User.by_name(unicode(username))
user = model.User.by_name(text_type(username))
if not user:
print('User "%s" not found' % username)
makeuser = input('Create new user: %s? [y/n]' % username)
if makeuser == 'y':
user_add(self.args[1:])
user = model.User.by_name(unicode(username))
user = model.User.by_name(text_type(username))
else:
print('Exiting ...')
return
Expand All @@ -834,7 +836,7 @@ def remove(self):
return
username = self.args[1]

user = model.User.by_name(unicode(username))
user = model.User.by_name(text_type(username))
if not user:
print('Error: user "%s" not found!' % username)
return
Expand Down Expand Up @@ -902,7 +904,7 @@ def show(self):
import ckan.model as model

username = self.args[0]
user = model.User.get(unicode(username))
user = model.User.get(text_type(username))
print('User: \n', user)

def setpass(self):
Expand Down Expand Up @@ -1005,7 +1007,7 @@ def list(self):

def _get_dataset(self, dataset_ref):
import ckan.model as model
dataset = model.Package.get(unicode(dataset_ref))
dataset = model.Package.get(text_type(dataset_ref))
assert dataset, 'Could not find dataset matching reference: %r' % dataset_ref
return dataset

Expand Down

0 comments on commit 555e096

Please sign in to comment.