Colin Watson has proposed merging lp:~cjwatson/launchpad/remove-services-unicode-csv into lp:launchpad.
Requested reviews: Launchpad code reviewers (launchpad-reviewers) For more details, see: https://code.launchpad.net/~cjwatson/launchpad/remove-services-unicode-csv/+merge/101315 lp.services.unicode_csv was introduced in r4551 (as canonical.launchpad.utilities.unicode_csv) to support entitlements. However, the entitlements code was removed in r14793, and there are no other users of this class in Launchpad. Therefore, it should be removed. -- https://code.launchpad.net/~cjwatson/launchpad/remove-services-unicode-csv/+merge/101315 Your team Launchpad code reviewers is requested to review the proposed merge of lp:~cjwatson/launchpad/remove-services-unicode-csv into lp:launchpad.
=== removed file 'lib/lp/services/doc/unicode_csv.txt' --- lib/lp/services/doc/unicode_csv.txt 2011-12-19 15:07:12 +0000 +++ lib/lp/services/doc/unicode_csv.txt 1970-01-01 00:00:00 +0000 @@ -1,118 +0,0 @@ -= Unicode CSV = - -The Python library for supporting comma-separated value (CSV) files -does not support unicode. In the examples section (9.1.5) of the -Python Libray documentation for version 2.5[1] they provide the code for -a UnicodeReader and UnicodeWriter. - -In order to support direct reading of unicode into dictionaries, the -standard DictReader and DictWriter have been extended to be -UnicodeDictReader and UnicodeDictWriter. These two classes are -identical to their base classes except for they utilize the -UnicodeReader and UnicodeWriter as their underlying workers. - -[1] http://docs.python.org/lib/csv-examples.html - - -== UnicodeCSVReader and UnicodeCSVWriter == - - >>> from cStringIO import StringIO - >>> from tempfile import mkstemp - >>> from lp.services.unicode_csv import ( - ... UnicodeCSVReader, UnicodeCSVWriter) - -Create a test string with some non-ASCII content representing a row of -CSV data. - - >>> test_data = ["100", "A-101", u'La Pe\xf1a'] - >>> test_string = ','.join(test_data) - >>> fd, fname = mkstemp() - -The data cannot be written using the standard csv.writer class as it -raises a UnicodeEncodeError. - - >>> import csv - >>> temp_file = open(fname, "wb") - >>> writer = csv.writer(temp_file) - >>> writer.writerow(test_data) - Traceback (most recent call last): - ... - UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1'... - -Using the UnicodeWriter, write the single row to the temporary file. - - >>> temp_file = open(fname, "wb") - >>> writer = UnicodeCSVWriter(temp_file, encoding="utf-8") - >>> writer.writerow(test_data) - >>> temp_file.close() - -The data in the file has been converted to UTF-8. If the file is read -in and compared to the original string they will not match due to the -encoding difference -- in fact, you should not compare them directly, and -trying to convert the data read into unicode will raise an error. - - >>> file_data = open(fname, "rb").readline() - >>> file_data == test_string - Traceback (most recent call last): - ... - UnicodeWarning: Unicode equal comparison failed to convert... - >>> unicode(file_data) == test_string - Traceback (most recent call last): - ... - UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3... - -The data can be read back using the UnicodeCSVReader. - - >>> temp_file = open(fname, "rb") - >>> reader = UnicodeCSVReader(temp_file, encoding="utf-8") - >>> file_data = reader.next() - >>> len(file_data) - 3 - - >>> for orig, stored in zip(test_data, file_data): - ... orig == stored - True - True - True - -== UnicodeDictReader and UnicodeDictWriter == - -The dictionary-based versions of the unicode reader and writer work -exactly like the standard versions, they just take a dictionary as -input and present a dictionary as output as a convenience. - - >>> from lp.services.unicode_csv import ( - ... UnicodeDictReader, UnicodeDictWriter) - -Construct a dict using the same data as before. - - >>> test_dict = dict( - ... id="100", - ... ext_id="A-101", - ... name=u'La Pe\xf1a') - >>> fieldnames = test_dict.keys() - >>> temp_file = open(fname, "wb") - >>> writer = UnicodeDictWriter( - ... temp_file, fieldnames=fieldnames, encoding="utf-8") - >>> writer.writerow(test_dict) - >>> temp_file.close() - -Read the dictionary back in using the UnicodeDictReader. - - >>> temp_file = open(fname, "rb") - >>> reader = UnicodeDictReader( - ... temp_file, fieldnames=fieldnames, encoding="utf-8") - >>> read_dict = reader.next() - >>> len(read_dict) - 3 - - >>> for key in fieldnames: - ... test_dict[key] == read_dict[key] - True - True - True - - >>> temp_file.close() - >>> import os - >>> os.unlink(fname) - === removed file 'lib/lp/services/unicode_csv.py' --- lib/lp/services/unicode_csv.py 2011-12-19 15:07:12 +0000 +++ lib/lp/services/unicode_csv.py 1970-01-01 00:00:00 +0000 @@ -1,114 +0,0 @@ -# Copyright 2009 Canonical Ltd. This software is licensed under the -# GNU Affero General Public License version 3 (see the file LICENSE). - -"""Unicode support for CSV files. - -Adapted from the Python documentation: -http://docs.python.org/lib/csv-examples.html - -Modified to work for Python 2.4. -""" - -__metaclass__ = type -__all__ = ['UnicodeReader', - 'UnicodeWriter', - 'UnicodeDictReader', - 'UnicodeDictWriter'] - - -import codecs -import cStringIO -import csv - - -class UTF8Recoder: - """Iterator that reads a stream and re-encodes to UTF-8. - - A stream of the given encoding is read and then re-encoded to UTF-8 - before being returned. - """ - - def __init__(self, f, encoding): - self.reader = codecs.getreader(encoding)(f) - - def __iter__(self): - return self - - def next(self): - return self.reader.next().encode("utf-8") - - -class UnicodeCSVReader: - """A CSV reader that reads encoded files and yields unicode.""" - - class DelegateLineNumAccessDescriptor: - """The Python 2.5 DictReader expects its reader to support access to a - line_num attribute, therefore to keep UnicodeCSVReader capable of being - used within a DictReader we provide a line_num attribute which - delegates to the real reader.""" - - def __get__(self, obj, type): - return obj.reader.line_num - - line_num = DelegateLineNumAccessDescriptor() - - def __init__(self, file_, dialect=csv.excel, encoding="utf-8", **kwds): - file_ = UTF8Recoder(file_, encoding) - self.reader = csv.reader(file_, dialect=dialect, **kwds) - - def next(self): - row = self.reader.next() - return [unicode(element, "utf-8") for element in row] - - def __iter__(self): - return self - - -class UnicodeCSVWriter: - """A CSV writer that encodes unicode and writes to the file.""" - - def __init__(self, file_, 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 = file_ - self.encoder = codecs.getencoder(encoding) - - def writerow(self, row): - self.writer.writerow([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 re-encode it into the target encoding - (data,len_encoded) = self.encoder(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) - - -class UnicodeDictReader(csv.DictReader): - """A CSV dict reader that reads encoded files and yields unicode.""" - - def __init__(self, file_, fieldnames=None, restkey=None, restval=None, - dialect="excel", encoding="utf-8", *args, **kwds): - csv.DictReader.__init__(self, file_, fieldnames, restkey, restval, - dialect, *args, **kwds) - # overwrite the reader with a UnicodeCSVReader - self.reader = UnicodeCSVReader(file_, dialect, encoding, *args, **kwds) - - -class UnicodeDictWriter(csv.DictWriter): - """A CSV dict writer that encodes unicode and writes to the file.""" - - def __init__(self, file_, fieldnames, restval="", extrasaction="raise", - dialect="excel", encoding="utf-8", - *args, **kwds): - csv.DictWriter.__init__(self, file_, fieldnames, restval, - extrasaction, dialect, *args, **kwds) - # overwrite the writer with a UnicodeCSVWriter - self.writer = UnicodeCSVWriter(file_, dialect, encoding, *args, **kwds)
_______________________________________________ Mailing list: https://launchpad.net/~launchpad-reviewers Post to : [email protected] Unsubscribe : https://launchpad.net/~launchpad-reviewers More help : https://help.launchpad.net/ListHelp

