Replace unicode with str, duck type encode
This commit is contained in:
parent
ba7109a6b9
commit
426095634c
@ -1,6 +1,11 @@
|
|||||||
import json
|
import json
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
try:
|
||||||
|
str = unicode
|
||||||
|
except NameError:
|
||||||
|
pass
|
||||||
|
|
||||||
from dataset.util import FreezeException
|
from dataset.util import FreezeException
|
||||||
|
|
||||||
|
|
||||||
@ -35,7 +40,7 @@ class Configuration(object):
|
|||||||
raise FreezeException("Invalid freeze file: %s" % ve)
|
raise FreezeException("Invalid freeze file: %s" % ve)
|
||||||
fh.close()
|
fh.close()
|
||||||
except IOError as ioe:
|
except IOError as ioe:
|
||||||
raise FreezeException(unicode(ioe))
|
raise FreezeException(str(ioe))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def exports(self):
|
def exports(self):
|
||||||
@ -59,7 +64,7 @@ class Export(object):
|
|||||||
def get_normalized(self, name, default=None):
|
def get_normalized(self, name, default=None):
|
||||||
value = self.get(name, default=default)
|
value = self.get(name, default=default)
|
||||||
if not value in [None, default]:
|
if not value in [None, default]:
|
||||||
value = unicode(value).lower().strip()
|
value = str(value).lower().strip()
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def get_bool(self, name, default=False):
|
def get_bool(self, name, default=False):
|
||||||
|
|||||||
@ -3,6 +3,11 @@ import logging
|
|||||||
import re
|
import re
|
||||||
import locale
|
import locale
|
||||||
|
|
||||||
|
try:
|
||||||
|
str = unicode
|
||||||
|
except NameError:
|
||||||
|
pass
|
||||||
|
|
||||||
from dataset.util import FreezeException
|
from dataset.util import FreezeException
|
||||||
from slugify import slugify
|
from slugify import slugify
|
||||||
|
|
||||||
@ -11,7 +16,7 @@ TMPL_KEY = re.compile("{{([^}]*)}}")
|
|||||||
|
|
||||||
OPERATIONS = {
|
OPERATIONS = {
|
||||||
'identity': lambda x: x,
|
'identity': lambda x: x,
|
||||||
'lower': lambda x: unicode(x).lower(),
|
'lower': lambda x: str(x).lower(),
|
||||||
'slug': slugify
|
'slug': slugify
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +44,7 @@ class Serializer(object):
|
|||||||
op, key = 'identity', m.group(1)
|
op, key = 'identity', m.group(1)
|
||||||
if ':' in key:
|
if ':' in key:
|
||||||
op, key = key.split(':', 1)
|
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)
|
path = TMPL_KEY.sub(repl, self._basepath)
|
||||||
enc = locale.getpreferredencoding()
|
enc = locale.getpreferredencoding()
|
||||||
return os.path.realpath(path.encode(enc, 'replace'))
|
return os.path.realpath(path.encode(enc, 'replace'))
|
||||||
@ -74,8 +79,6 @@ class Serializer(object):
|
|||||||
|
|
||||||
for field, operation in transforms.items():
|
for field, operation in transforms.items():
|
||||||
row[field] = OPERATIONS.get(operation)(row.get(field))
|
row[field] = OPERATIONS.get(operation)(row.get(field))
|
||||||
|
|
||||||
self.write(self.file_name(row), row)
|
self.write(self.file_name(row), row)
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@ from dataset.freeze.format.common import Serializer
|
|||||||
def value_to_str(value):
|
def value_to_str(value):
|
||||||
if isinstance(value, datetime):
|
if isinstance(value, datetime):
|
||||||
return value.isoformat()
|
return value.isoformat()
|
||||||
if isinstance(value, unicode):
|
if hasattr(value, 'encode'):
|
||||||
return value.encode('utf-8')
|
return value.encode('utf-8')
|
||||||
if value is None:
|
if value is None:
|
||||||
return ''
|
return ''
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user