move UnicodeWriter to utils

pull/1/head
eric 2016-09-28 18:42:55 -04:00
parent fc56744371
commit 4ba5104e06
2 changed files with 46 additions and 40 deletions

View File

@ -1,4 +1,8 @@
#!/usr/bin/python #!/usr/bin/python
import codecs
import cStringIO
import csv
from django.conf import settings from django.conf import settings
try: try:
use_session = settings.QUESTIONNAIRE_USE_SESSION use_session = settings.QUESTIONNAIRE_USE_SESSION
@ -55,3 +59,34 @@ def get_runid_from_request(request):
if __name__ == "__main__": if __name__ == "__main__":
import doctest import doctest
doctest.testmod() doctest.testmod()
class UnicodeWriter:
"""
COPIED from http://docs.python.org/library/csv.html example:
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
def writerow(self, row):
self.writer.writerow([unicode(s).encode("utf-8") for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)

View File

@ -1,5 +1,14 @@
#!/usr/bin/python #!/usr/bin/python
# vim: set fileencoding=utf-8 # vim: set fileencoding=utf-8
import logging
import random
import re
import tempfile
from compat import commit_on_success, commit, rollback
from hashlib import md5
from uuid import uuid4
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext from django.template import RequestContext
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
@ -12,6 +21,7 @@ from django.conf import settings
from datetime import datetime from datetime import datetime
from django.utils import translation from django.utils import translation
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from . import QuestionProcessors from . import QuestionProcessors
from . import questionnaire_start, questionset_start, questionset_done, questionnaire_done from . import questionnaire_start, questionset_start, questionset_done, questionnaire_done
from . import AnswerException from . import AnswerException
@ -21,15 +31,9 @@ from .models import *
from .parsers import * from .parsers import *
from .parsers import BoolNot, BoolAnd, BoolOr, Checker from .parsers import BoolNot, BoolAnd, BoolOr, Checker
from .emails import _send_email, send_emails from .emails import _send_email, send_emails
from .utils import numal_sort, split_numal from .utils import numal_sort, split_numal, UnicodeWriter
from .request_cache import request_cache from .request_cache import request_cache
from .dependency_checker import dep_check from .dependency_checker import dep_check
from compat import commit_on_success, commit, rollback
import logging
import random
from hashlib import md5
import re
from uuid import uuid4
try: try:
@ -833,39 +837,6 @@ def export_csv(request, qid): # questionnaire_id
For a given questionnaire id, generaete a CSV containing all the For a given questionnaire id, generaete a CSV containing all the
answers for all subjects. answers for all subjects.
""" """
import tempfile, csv, cStringIO, codecs
class UnicodeWriter:
"""
COPIED from http://docs.python.org/library/csv.html example:
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
def writerow(self, row):
self.writer.writerow([unicode(s).encode("utf-8") for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)
fd = tempfile.TemporaryFile() fd = tempfile.TemporaryFile()
questionnaire = get_object_or_404(Questionnaire, pk=int(qid)) questionnaire = get_object_or_404(Questionnaire, pk=int(qid))