Skip to content

Commit 0059953

Browse files
committed
Do not require backports.csv on Python3 #280
Signed-off-by: Philippe Ombredanne <[email protected]> Do not subclass csv.DictReader on Python 3 #280 Signed-off-by: Philippe Ombredanne <[email protected]> Do not require backports.csv on Python3 #280 Signed-off-by: Philippe Ombredanne <[email protected]> Do not require backports.csv on Python3 #280 Signed-off-by: Philippe Ombredanne <[email protected]> Use proper subclass of csv.DictReader on Python 2 #280 Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent c0fdb95 commit 0059953

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

etc/conf/base.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ six
1616

1717
pyyaml==3.12
1818

19-
backports.csv==1.0.5
20-
2119
# license expression support
2220
license_expression
2321
boolean.py

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,14 @@ def read(*names, **kwargs):
8484
install_requires=[
8585
'jinja2 >= 2.9, < 3.0',
8686
'click >= 6.7, < 7.0',
87-
'backports.csv;python_version<"3.6"',
87+
"backports.csv ; python_version<'3.6'",
8888
'pyyaml >= 3.11, < 3.13',
8989
'boolean.py >= 3.5, < 4.0',
9090
'license_expression >= 0.94, < 1.0',
9191
],
92+
extras_require={
93+
":python_version < '3.6'": ['backports.csv'],
94+
},
9295
entry_points={
9396
'console_scripts': [
9497
'about-code=attributecode.cmd:cli',

src/attributecode/util.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,40 @@ def resource_name(path):
235235
return right.strip()
236236

237237

238-
class OrderedDictReader(csv.DictReader):
239-
"""
240-
A DictReader that return OrderedDicts
241-
"""
242-
def next(self):
243-
row_dict = next(self)
244-
result = OrderedDict()
245-
# reorder based on fieldnames order
246-
for name in self.fieldnames:
247-
result[name] = row_dict[name]
248-
return result
238+
# Python 3
239+
OrderedDictReader = csv.DictReader
240+
241+
if sys.version_info[0] < 3:
242+
# Python 2
243+
class OrderedDictReader(csv.DictReader):
244+
"""
245+
A DictReader that return OrderedDicts
246+
Copied from csv.DictReader itself backported from Python 3
247+
license: python
248+
"""
249+
def __next__(self):
250+
if self.line_num == 0:
251+
# Used only for its side effect.
252+
self.fieldnames
253+
row = next(self.reader)
254+
self.line_num = self.reader.line_num
255+
256+
# unlike the basic reader, we prefer not to return blanks,
257+
# because we will typically wind up with a dict full of None
258+
# values
259+
while row == []:
260+
row = next(self.reader)
261+
d = OrderedDict(zip(self.fieldnames, row))
262+
lf = len(self.fieldnames)
263+
lr = len(row)
264+
if lf < lr:
265+
d[self.restkey] = row[lf:]
266+
elif lf > lr:
267+
for key in self.fieldnames[lr:]:
268+
d[key] = self.restval
269+
return d
270+
271+
next = __next__
249272

250273

251274
def get_mapping(location=None):

0 commit comments

Comments
 (0)