Skip to content

Commit

Permalink
Merge pull request getredash#1839 from yamamanx/writer_encode
Browse files Browse the repository at this point in the history
UnicodeWriter character code to environment
  • Loading branch information
arikfr authored Jun 29, 2017
2 parents 0ab25c3 + f840681 commit 520a5f8
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions redash/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hashlib
import pytz
import pystache
import os

from funcy import distinct, select_values
from sqlalchemy.orm.query import Query
Expand All @@ -17,6 +18,7 @@
from redash import settings

COMMENTS_REGEX = re.compile("/\*.*?\*/")
WRITER_ENCODING = os.environ.get('REDASH_CSV_WRITER_ENCODING', 'utf-8')


def utcnow():
Expand Down Expand Up @@ -102,7 +104,7 @@ class UnicodeWriter:
which is encoded in the given encoding.
"""

def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
def __init__(self, f, dialect=csv.excel, encoding=WRITER_ENCODING, **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
Expand All @@ -111,15 +113,15 @@ def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):

def _encode_utf8(self, val):
if isinstance(val, (unicode, str)):
return val.encode('utf-8')
return val.encode(WRITER_ENCODING)

return val

def writerow(self, row):
self.writer.writerow([self._encode_utf8(s) for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
data = data.decode(WRITER_ENCODING)
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
Expand Down

0 comments on commit 520a5f8

Please sign in to comment.