Merge lp:~cjwatson/launchpad/remove-services-unicode-csv into lp:launchpad

Proposed by Colin Watson on 2012-04-10
Status: Merged
Approved by: Robert Collins on 2012-04-11
Approved revision: no longer in the source branch.
Merged at revision: 15080
Proposed branch: lp:~cjwatson/launchpad/remove-services-unicode-csv
Merge into: lp:launchpad
Diff against target: 241 lines (+0/-232)
2 files modified
lib/lp/services/doc/unicode_csv.txt (+0/-118)
lib/lp/services/unicode_csv.py (+0/-114)
To merge this branch: bzr merge lp:~cjwatson/launchpad/remove-services-unicode-csv
Reviewer Review Type Date Requested Status
Robert Collins (community) 2012-04-10 Approve on 2012-04-11
Review via email: mp+101315@code.launchpad.net

Commit Message

Remove unused lp.services.unicode_csv.

Description of the Change

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.

To post a comment you must log in.
Robert Collins (lifeless) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== removed file 'lib/lp/services/doc/unicode_csv.txt'
2--- lib/lp/services/doc/unicode_csv.txt 2011-12-19 15:07:12 +0000
3+++ lib/lp/services/doc/unicode_csv.txt 1970-01-01 00:00:00 +0000
4@@ -1,118 +0,0 @@
5-= Unicode CSV =
6-
7-The Python library for supporting comma-separated value (CSV) files
8-does not support unicode. In the examples section (9.1.5) of the
9-Python Libray documentation for version 2.5[1] they provide the code for
10-a UnicodeReader and UnicodeWriter.
11-
12-In order to support direct reading of unicode into dictionaries, the
13-standard DictReader and DictWriter have been extended to be
14-UnicodeDictReader and UnicodeDictWriter. These two classes are
15-identical to their base classes except for they utilize the
16-UnicodeReader and UnicodeWriter as their underlying workers.
17-
18-[1] http://docs.python.org/lib/csv-examples.html
19-
20-
21-== UnicodeCSVReader and UnicodeCSVWriter ==
22-
23- >>> from cStringIO import StringIO
24- >>> from tempfile import mkstemp
25- >>> from lp.services.unicode_csv import (
26- ... UnicodeCSVReader, UnicodeCSVWriter)
27-
28-Create a test string with some non-ASCII content representing a row of
29-CSV data.
30-
31- >>> test_data = ["100", "A-101", u'La Pe\xf1a']
32- >>> test_string = ','.join(test_data)
33- >>> fd, fname = mkstemp()
34-
35-The data cannot be written using the standard csv.writer class as it
36-raises a UnicodeEncodeError.
37-
38- >>> import csv
39- >>> temp_file = open(fname, "wb")
40- >>> writer = csv.writer(temp_file)
41- >>> writer.writerow(test_data)
42- Traceback (most recent call last):
43- ...
44- UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1'...
45-
46-Using the UnicodeWriter, write the single row to the temporary file.
47-
48- >>> temp_file = open(fname, "wb")
49- >>> writer = UnicodeCSVWriter(temp_file, encoding="utf-8")
50- >>> writer.writerow(test_data)
51- >>> temp_file.close()
52-
53-The data in the file has been converted to UTF-8. If the file is read
54-in and compared to the original string they will not match due to the
55-encoding difference -- in fact, you should not compare them directly, and
56-trying to convert the data read into unicode will raise an error.
57-
58- >>> file_data = open(fname, "rb").readline()
59- >>> file_data == test_string
60- Traceback (most recent call last):
61- ...
62- UnicodeWarning: Unicode equal comparison failed to convert...
63- >>> unicode(file_data) == test_string
64- Traceback (most recent call last):
65- ...
66- UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3...
67-
68-The data can be read back using the UnicodeCSVReader.
69-
70- >>> temp_file = open(fname, "rb")
71- >>> reader = UnicodeCSVReader(temp_file, encoding="utf-8")
72- >>> file_data = reader.next()
73- >>> len(file_data)
74- 3
75-
76- >>> for orig, stored in zip(test_data, file_data):
77- ... orig == stored
78- True
79- True
80- True
81-
82-== UnicodeDictReader and UnicodeDictWriter ==
83-
84-The dictionary-based versions of the unicode reader and writer work
85-exactly like the standard versions, they just take a dictionary as
86-input and present a dictionary as output as a convenience.
87-
88- >>> from lp.services.unicode_csv import (
89- ... UnicodeDictReader, UnicodeDictWriter)
90-
91-Construct a dict using the same data as before.
92-
93- >>> test_dict = dict(
94- ... id="100",
95- ... ext_id="A-101",
96- ... name=u'La Pe\xf1a')
97- >>> fieldnames = test_dict.keys()
98- >>> temp_file = open(fname, "wb")
99- >>> writer = UnicodeDictWriter(
100- ... temp_file, fieldnames=fieldnames, encoding="utf-8")
101- >>> writer.writerow(test_dict)
102- >>> temp_file.close()
103-
104-Read the dictionary back in using the UnicodeDictReader.
105-
106- >>> temp_file = open(fname, "rb")
107- >>> reader = UnicodeDictReader(
108- ... temp_file, fieldnames=fieldnames, encoding="utf-8")
109- >>> read_dict = reader.next()
110- >>> len(read_dict)
111- 3
112-
113- >>> for key in fieldnames:
114- ... test_dict[key] == read_dict[key]
115- True
116- True
117- True
118-
119- >>> temp_file.close()
120- >>> import os
121- >>> os.unlink(fname)
122-
123
124=== removed file 'lib/lp/services/unicode_csv.py'
125--- lib/lp/services/unicode_csv.py 2011-12-19 15:07:12 +0000
126+++ lib/lp/services/unicode_csv.py 1970-01-01 00:00:00 +0000
127@@ -1,114 +0,0 @@
128-# Copyright 2009 Canonical Ltd. This software is licensed under the
129-# GNU Affero General Public License version 3 (see the file LICENSE).
130-
131-"""Unicode support for CSV files.
132-
133-Adapted from the Python documentation:
134-http://docs.python.org/lib/csv-examples.html
135-
136-Modified to work for Python 2.4.
137-"""
138-
139-__metaclass__ = type
140-__all__ = ['UnicodeReader',
141- 'UnicodeWriter',
142- 'UnicodeDictReader',
143- 'UnicodeDictWriter']
144-
145-
146-import codecs
147-import cStringIO
148-import csv
149-
150-
151-class UTF8Recoder:
152- """Iterator that reads a stream and re-encodes to UTF-8.
153-
154- A stream of the given encoding is read and then re-encoded to UTF-8
155- before being returned.
156- """
157-
158- def __init__(self, f, encoding):
159- self.reader = codecs.getreader(encoding)(f)
160-
161- def __iter__(self):
162- return self
163-
164- def next(self):
165- return self.reader.next().encode("utf-8")
166-
167-
168-class UnicodeCSVReader:
169- """A CSV reader that reads encoded files and yields unicode."""
170-
171- class DelegateLineNumAccessDescriptor:
172- """The Python 2.5 DictReader expects its reader to support access to a
173- line_num attribute, therefore to keep UnicodeCSVReader capable of being
174- used within a DictReader we provide a line_num attribute which
175- delegates to the real reader."""
176-
177- def __get__(self, obj, type):
178- return obj.reader.line_num
179-
180- line_num = DelegateLineNumAccessDescriptor()
181-
182- def __init__(self, file_, dialect=csv.excel, encoding="utf-8", **kwds):
183- file_ = UTF8Recoder(file_, encoding)
184- self.reader = csv.reader(file_, dialect=dialect, **kwds)
185-
186- def next(self):
187- row = self.reader.next()
188- return [unicode(element, "utf-8") for element in row]
189-
190- def __iter__(self):
191- return self
192-
193-
194-class UnicodeCSVWriter:
195- """A CSV writer that encodes unicode and writes to the file."""
196-
197- def __init__(self, file_, dialect=csv.excel, encoding="utf-8", **kwds):
198- # Redirect output to a queue
199- self.queue = cStringIO.StringIO()
200- self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
201- self.stream = file_
202- self.encoder = codecs.getencoder(encoding)
203-
204- def writerow(self, row):
205- self.writer.writerow([s.encode("utf-8") for s in row])
206- # Fetch UTF-8 output from the queue ...
207- data = self.queue.getvalue()
208- data = data.decode("utf-8")
209- # ... and re-encode it into the target encoding
210- (data,len_encoded) = self.encoder(data)
211- # write to the target stream
212- self.stream.write(data)
213- # empty queue
214- self.queue.truncate(0)
215-
216- def writerows(self, rows):
217- for row in rows:
218- self.writerow(row)
219-
220-
221-class UnicodeDictReader(csv.DictReader):
222- """A CSV dict reader that reads encoded files and yields unicode."""
223-
224- def __init__(self, file_, fieldnames=None, restkey=None, restval=None,
225- dialect="excel", encoding="utf-8", *args, **kwds):
226- csv.DictReader.__init__(self, file_, fieldnames, restkey, restval,
227- dialect, *args, **kwds)
228- # overwrite the reader with a UnicodeCSVReader
229- self.reader = UnicodeCSVReader(file_, dialect, encoding, *args, **kwds)
230-
231-
232-class UnicodeDictWriter(csv.DictWriter):
233- """A CSV dict writer that encodes unicode and writes to the file."""
234-
235- def __init__(self, file_, fieldnames, restval="", extrasaction="raise",
236- dialect="excel", encoding="utf-8",
237- *args, **kwds):
238- csv.DictWriter.__init__(self, file_, fieldnames, restval,
239- extrasaction, dialect, *args, **kwds)
240- # overwrite the writer with a UnicodeCSVWriter
241- self.writer = UnicodeCSVWriter(file_, dialect, encoding, *args, **kwds)