Replace unicode with str, duck type encode

This commit is contained in:
Stefan Wehrmeyer 2013-12-18 11:31:41 +01:00
parent ba7109a6b9
commit 426095634c
3 changed files with 16 additions and 8 deletions

View File

@ -1,6 +1,11 @@
import json
import yaml
try:
str = unicode
except NameError:
pass
from dataset.util import FreezeException
@ -35,7 +40,7 @@ class Configuration(object):
raise FreezeException("Invalid freeze file: %s" % ve)
fh.close()
except IOError as ioe:
raise FreezeException(unicode(ioe))
raise FreezeException(str(ioe))
@property
def exports(self):
@ -59,7 +64,7 @@ class Export(object):
def get_normalized(self, name, default=None):
value = self.get(name, default=default)
if not value in [None, default]:
value = unicode(value).lower().strip()
value = str(value).lower().strip()
return value
def get_bool(self, name, default=False):

View File

@ -3,6 +3,11 @@ import logging
import re
import locale
try:
str = unicode
except NameError:
pass
from dataset.util import FreezeException
from slugify import slugify
@ -11,7 +16,7 @@ TMPL_KEY = re.compile("{{([^}]*)}}")
OPERATIONS = {
'identity': lambda x: x,
'lower': lambda x: unicode(x).lower(),
'lower': lambda x: str(x).lower(),
'slug': slugify
}
@ -39,7 +44,7 @@ class Serializer(object):
op, key = 'identity', m.group(1)
if ':' in key:
op, key = key.split(':', 1)
return unicode(OPERATIONS.get(op)(data.get(key, '')))
return str(OPERATIONS.get(op)(data.get(key, '')))
path = TMPL_KEY.sub(repl, self._basepath)
enc = locale.getpreferredencoding()
return os.path.realpath(path.encode(enc, 'replace'))
@ -77,5 +82,3 @@ class Serializer(object):
self.write(self.file_name(row), row)
self.close()

View File

@ -7,7 +7,7 @@ from dataset.freeze.format.common import Serializer
def value_to_str(value):
if isinstance(value, datetime):
return value.isoformat()
if isinstance(value, unicode):
if hasattr(value, 'encode'):
return value.encode('utf-8')
if value is None:
return ''