|
29 | 29 | from attributecode.util import csv |
30 | 30 | from attributecode.util import python2 |
31 | 31 | from attributecode.util import replace_tab_with_spaces |
| 32 | +from __builtin__ import True |
32 | 33 |
|
33 | 34 |
|
34 | 35 | if python2: # pragma: nocover |
@@ -76,24 +77,28 @@ def transform_data(rows, transformer): |
76 | 77 | dupes = check_duplicate_columns(column_names) |
77 | 78 |
|
78 | 79 | if dupes: |
79 | | - msg = 'Duplicated column name: {name}' |
80 | | - errors.extend(Error(CRITICAL, msg.format(name)) for name in dupes) |
| 80 | + msg = u'Duplicated column name: %(name)s' |
| 81 | + for name in dupes: |
| 82 | + errors.append(Error(CRITICAL, msg % locals())) |
81 | 83 | return column_names, [], errors |
82 | 84 |
|
83 | | - column_names = transformer.apply_renamings(column_names) |
84 | | - |
85 | | - # convert to dicts using the renamed columns |
| 85 | + # Convert to dicts |
86 | 86 | data = [OrderedDict(zip_longest(column_names, row)) for row in rows] |
| 87 | + |
| 88 | + #column_names = transformer.apply_renamings(column_names) |
| 89 | + renamed_column_data = transformer.apply_renamings(data) |
| 90 | + |
| 91 | + column_names = renamed_column_data[0].keys() |
87 | 92 |
|
88 | 93 | if transformer.column_filters: |
89 | | - data = list(transformer.filter_columns(data)) |
| 94 | + renamed_column_data = list(transformer.filter_columns(renamed_column_data)) |
90 | 95 | column_names = [c for c in column_names if c in transformer.column_filters] |
91 | 96 |
|
92 | | - errors = transformer.check_required_columns(data) |
| 97 | + errors = transformer.check_required_columns(renamed_column_data) |
93 | 98 | if errors: |
94 | 99 | return column_names, data, errors |
95 | 100 |
|
96 | | - return column_names, data, errors |
| 101 | + return column_names, renamed_column_data, errors |
97 | 102 |
|
98 | 103 |
|
99 | 104 | tranformer_config_help = ''' |
@@ -210,22 +215,29 @@ def check_required_columns(self, data): |
210 | 215 | errors.append(Error(CRITICAL, msg.format(**locals()))) |
211 | 216 | return errors |
212 | 217 |
|
213 | | - def apply_renamings(self, column_names): |
| 218 | + def apply_renamings(self, data): |
214 | 219 | """ |
215 | | - Return a tranformed list of `column_names` where columns are renamed |
| 220 | + Return a transformed dictionary list where columns are renamed |
216 | 221 | based on this Transformer configuration. |
217 | 222 | """ |
218 | 223 | renamings = self.column_renamings |
219 | 224 | if not renamings: |
220 | | - return column_names |
221 | | - renamings = {n.lower(): rn.lower() for n, rn in renamings.items()} |
222 | | - |
223 | | - renamed = [] |
224 | | - for name in column_names: |
225 | | - name = name.lower() |
226 | | - new_name = renamings.get(name, name) |
227 | | - renamed.append(new_name) |
228 | | - return renamed |
| 225 | + return data |
| 226 | + renamings = {n.lower(): rn.lower() for n,rn in renamings.items()} |
| 227 | + |
| 228 | + renamed_list = [] |
| 229 | + for row in data: |
| 230 | + renamed = OrderedDict() |
| 231 | + for key in row: |
| 232 | + matched = False |
| 233 | + for renamed_key in renamings: |
| 234 | + if key == renamings[renamed_key]: |
| 235 | + renamed[renamed_key] = row[key] |
| 236 | + matched = True |
| 237 | + if not matched: |
| 238 | + renamed[key] = row[key] |
| 239 | + renamed_list.append(renamed) |
| 240 | + return renamed_list |
229 | 241 |
|
230 | 242 | def clean_columns(self, column_names): |
231 | 243 | """ |
|
0 commit comments