ported freezing as csv to python 3.4

This commit is contained in:
xrotwang
2014-07-25 12:15:08 +02:00
parent 9a91f3d113
commit a14f0bbde2
4 changed files with 102 additions and 13 deletions
+8 -7
View File
@@ -3,10 +3,7 @@ import re
import sys
import locale
try:
str = unicode
except NameError:
pass
from six import binary_type, text_type
from dataset.util import FreezeException
from slugify import slugify
@@ -16,7 +13,7 @@ TMPL_KEY = re.compile("{{([^}]*)}}")
OPERATIONS = {
'identity': lambda x: x,
'lower': lambda x: str(x).lower(),
'lower': lambda x: text_type(x).lower(),
'slug': slugify
}
@@ -24,6 +21,7 @@ OPERATIONS = {
class Serializer(object):
def __init__(self, export, query):
self._encoding = locale.getpreferredencoding()
self.export = export
self.query = query
self._paths = []
@@ -35,10 +33,14 @@ class Serializer(object):
def _get_basepath(self):
prefix = self.export.get('prefix', '')
if isinstance(prefix, binary_type):
prefix = text_type(prefix, encoding=self._encoding)
prefix = os.path.abspath(prefix)
prefix = os.path.realpath(prefix)
self._prefix = prefix
filename = self.export.get('filename')
if isinstance(filename, binary_type):
filename = text_type(filename, encoding=self._encoding)
if filename is None:
raise FreezeException("No 'filename' is specified")
self._basepath = os.path.join(prefix, filename)
@@ -50,8 +52,7 @@ class Serializer(object):
op, key = key.split(':', 1)
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'))
return os.path.realpath(path)
def file_name(self, row):
# signal that there is a fileobj available:
+22 -6
View File
@@ -1,13 +1,23 @@
from __future__ import unicode_literals
import csv
from datetime import datetime
from datetime import datetime, date
from six import PY3, binary_type, text_type
from sqlalchemy import DateTime
from dataset.freeze.format.common import Serializer
def value_to_str(value):
if isinstance(value, datetime):
return value.isoformat()
if hasattr(value, 'encode'):
if isinstance(value, (datetime, date)):
#
# FIXME: this check does not work for values returned from a db query!
# As a workaround, we make sure, the isoformat call returns the regular
# str representation.
#
sep = ' ' if PY3 else str(' ')
return text_type(value.isoformat(sep=sep))
if not PY3 and hasattr(value, 'encode'):
return value.encode('utf-8')
if value is None:
return ''
@@ -25,12 +35,18 @@ class CSVSerializer(Serializer):
# handle fileobj that has been passed in:
if path is not None:
fh = open(path, 'wb')
if PY3:
fh = open(path, 'wt', encoding='utf8', newline='')
else:
fh = open(path, 'wb')
else:
fh = self.fileobj
writer = csv.writer(fh)
writer.writerow([k.encode('utf-8') for k in keys])
if PY3:
writer.writerow(keys)
else:
writer.writerow([k.encode('utf-8') for k in keys])
self.handles[path] = (writer, fh)
writer, fh = self.handles[path]
values = [value_to_str(result.get(k)) for k in keys]