Skip to content

Commit 4140fc8

Browse files
committed
Fixed #456 - Handled trailing spaces in field name when doing
`transform`
1 parent 4235889 commit 4140fc8

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

docs/CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Fixed #442 no special characters allowed for `license_key`, `license_name` and `license_expression`
77
* Fixed #446 Better error handling
88
* Add support to collect redistributable sources #22
9+
* Handle trailing spaces in field names during `transform` #456
910

1011
2020-08-11
1112
Release 5.0.0

src/attributecode/cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def transform(location, output, configuration, quiet, verbose): # NOQA
578578
errors = unique(errors)
579579
errors_count = report_errors(errors, quiet, verbose, log_file_loc=output + '-error.log')
580580
if not quiet and not errors:
581-
msg = 'Transformed file written to {output}.'.format(**locals())
581+
msg = 'Transformed file is written to {output}.'.format(**locals())
582582
click.echo(msg)
583583
sys.exit(errors_count)
584584

src/attributecode/transform.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def transform_csv_to_csv(location, output, transformer):
5151

5252
errors = []
5353
data = iter(rows)
54-
field_names = next(rows)
55-
54+
names = next(rows)
55+
field_names = strip_trailing_fields_csv(names)
5656
dupes = check_duplicate_fields(field_names)
5757

5858
if dupes:
@@ -82,7 +82,8 @@ def transform_json_to_json(location, output, transformer):
8282
if not transformer:
8383
raise ValueError('Cannot transform without Transformer')
8484

85-
data = read_json(location)
85+
items = read_json(location)
86+
data = strip_trailing_fields_json(items)
8687
new_data = normalize_dict_data(data)
8788

8889
field_names, updated_data, errors = transform_data(new_data, transformer)
@@ -93,6 +94,28 @@ def transform_json_to_json(location, output, transformer):
9394
write_json(output, updated_data)
9495
return []
9596

97+
def strip_trailing_fields_csv(names):
98+
"""
99+
Strip trailing spaces for field names #456
100+
"""
101+
field_names = []
102+
for name in names:
103+
field_names.append(name.strip())
104+
return field_names
105+
106+
107+
def strip_trailing_fields_json(items):
108+
"""
109+
Strip trailing spaces for field name #456
110+
"""
111+
data = []
112+
od = OrderedDict()
113+
for item in items:
114+
for field in item:
115+
stripped_field_name = field.strip()
116+
od[stripped_field_name] = item[field]
117+
data.append(od)
118+
return data
96119

97120
def normalize_dict_data(data):
98121
"""

tests/test_transform.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
from attributecode.transform import read_json
3535
from attributecode.transform import transform_data
3636
from attributecode.transform import normalize_dict_data
37+
from attributecode.transform import strip_trailing_fields_csv
38+
from attributecode.transform import strip_trailing_fields_json
3739
from attributecode.transform import Transformer
3840

3941
from attributecode.util import python2
@@ -160,3 +162,14 @@ def test_check_duplicate_fields(self):
160162
dups = check_duplicate_fields(field_name)
161163
assert dups == expected
162164

165+
def test_strip_trailing_fields_csv(self):
166+
test = [u'about_resource', u'name ', u' version ']
167+
expected = [u'about_resource', u'name', u'version']
168+
result = strip_trailing_fields_csv(test)
169+
assert result == expected
170+
171+
def test_strip_trailing_fields_json(self):
172+
test = [OrderedDict([(u'about_resource', u'/this.c'), (u'name ', u'this.c'), (u' version ', u'0.11.0')])]
173+
expected = [OrderedDict([(u'about_resource', u'/this.c'), (u'name', u'this.c'), (u'version', u'0.11.0')])]
174+
result = strip_trailing_fields_json(test)
175+
assert result == expected

0 commit comments

Comments
 (0)