-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtransform.py
More file actions
304 lines (248 loc) · 10.5 KB
/
Copy pathtransform.py
File metadata and controls
304 lines (248 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
# -*- coding: utf8 -*-
# ============================================================================
# Copyright (c) 2013-2018 nexB Inc. http://www.nexb.com/ - All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from collections import Counter
from collections import OrderedDict
import io
import attr
from attributecode import CRITICAL
from attributecode import Error
from attributecode import saneyaml
from attributecode.util import python2
if python2: # pragma: nocover
from itertools import izip_longest as zip_longest # NOQA
import backports.csv as csv # NOQA
else: # pragma: nocover
from itertools import zip_longest # NOQA
import csv # NOQA
def transform_csv_to_csv(location, output, transformer):
"""
Read a CSV file at `location` and write a new CSV file at `output`. Apply
transformations using the `transformer` Tranformer.
Return a list of Error objects.
"""
if not transformer:
raise ValueError('Cannot transform without Transformer')
rows = read_csv_rows(location)
column_names, data, errors = transform_data(rows, transformer)
if errors:
return errors
else:
write_csv(output, data, column_names)
return []
def transform_data(rows, transformer):
"""
Read a list of list of CSV-like data `rows` and apply transformations using the
`transformer` Tranformer.
Return a tuple of:
([column names...], [transformed ordered mappings...], [Error objects..])
"""
if not transformer:
return rows
errors = []
rows = iter(rows)
column_names = next(rows)
column_names = transformer.clean_columns(column_names)
dupes = check_duplicate_columns(column_names)
if dupes:
msg = 'Duplicated column name: {name}'
errors.extend(Error(CRITICAL, msg.format(name)) for name in dupes)
return column_names, [], errors
column_names = transformer.apply_renamings(column_names)
# convert to mappings using the renamed columns
data = [OrderedDict(zip_longest(column_names, row)) for row in rows]
if transformer.column_filters:
data = list(transformer.filter_columns(data))
column_names = [c for c in column_names if c in transformer.column_filters]
errors = transformer.check_required_columns(data)
if errors:
return column_names, data, errors
if transformer.row_filters:
data = list(transformer.filter_rows(data))
return column_names, data, errors
tranformer_config_help = '''
A transform configuration file is used to describe which transformations and
validations to apply to a source CSV file. This is a simple text file using YAML
format, using the same format as an .ABOUT file.
The attributes that can be set in a configuration file are:
* column_renamings:
An optional mapping of source CSV column name to target CSV new column name that
is used to rename CSV columns.
For instance with this configuration the columns "Directory/Location" will be
renamed to "about_resource" and "foo" to "bar":
renamings:
'Directory/Location' : about_resource
foo : bar
The renaming is always applied first before other transforms and checks. All
other column names referenced below are these that exist AFTER the renamings
have been applied to the existing column names.
* required_columns:
An optional list of required column names that must have a value, beyond the
standard columns names. If a source CSV does not have such a column or a row is
missing a value for a required column, an error is reported.
For instance with this configuration an error will be reported if the columns
"name" and "version" are missing or if any row does not have a value set for
these columns:
required_columns:
- name
- version
* column_filters:
An optional list of column names that should be kept in the transformed CSV. If
this list is provided, all the columns from the source CSV that should be kept
in the target CSV must be listed be even if they are standard or required
columns. If this list is not provided, all source CSV columns are kept in the
transformed target CSV.
For instance with this configuration the target CSV will only contains the "name"
and "version" columns and no other column:
column_filters:
- name
- version
* row_filters:
An optional list of mappings of <column name>: <value> that a source CSV row
should match to be added to the transformed target CSV. If any column value of a
row matches any such filter it is kept. Otherwise it is skipped. Filters are
applied last after all renamings, checks and tranforms and can therefore onlu
use remaining column names.
For instance with this configuration the target CSV will only contain rows that
have a "path" equal to "/root/user/lib":
row_filters:
path : /root/user/lib
'''
@attr.attributes
class Transformer(object):
__doc__ = tranformer_config_help
column_renamings = attr.attrib(default=attr.Factory(dict))
required_columns = attr.attrib(default=attr.Factory(list))
column_filters = attr.attrib(default=attr.Factory(list))
row_filters = attr.attrib(default=attr.Factory(list))
# TODO: populate these!
# a list of all the standard columns from AboutCode toolkit
standard_columns = attr.attrib(default=attr.Factory(list), init=False)
# a list of the subset of standard columns that are essential and MUST be
# present for AboutCode toolkit to work
essential_columns = attr.attrib(default=attr.Factory(list), init=False)
@classmethod
def default(cls):
"""
Return a default Transformer with built-in transforms.
"""
return cls(
column_renamings={},
required_columns=[],
column_filters=[],
row_filters=[],
)
@classmethod
def from_file(cls, location):
"""
Load and return a Transformer instance from a YAML configuration file at
`location`.
"""
with io.open(location, encoding='utf-8') as conf:
data = saneyaml.load(conf.read())
return cls(
column_renamings=data.get('column_renamings', {}),
required_columns=data.get('required_columns', []),
column_filters=data.get('column_filters', []),
row_filters=data.get('row_filters', []),
)
def check_required_columns(self, data):
"""
Return a list of Error for a `data` list of ordered mappings where a
mapping is missing a value for a required column name.
"""
errors = []
required = set(self.essential_columns + self.required_columns)
if not required:
return []
for rn, item in enumerate(data):
missings = [rk for rk in required if not item.get(rk)]
if not missings:
continue
missings = ', '.join(missings)
msg = 'Row {rn} is missing required values for columns: {missings}'
errors.append(Error(CRITICAL, msg.format(**locals())))
return errors
def apply_renamings(self, column_names):
"""
Return a tranformed list of `column_names` where columns are renamed
based on this Transformer configuration.
"""
renamings = self.column_renamings
if not renamings:
return column_names
renamings = {n.lower(): rn.lower() for n, rn in renamings.items()}
renamed = []
for name in column_names:
name = name.lower()
new_name = renamings.get(name, name)
renamed.append(new_name)
return renamed
def clean_columns(self, column_names):
"""
Apply standard cleanups to a list of columns and return these.
"""
if not column_names:
return column_names
return [c.strip().lower() for c in column_names]
def filter_columns(self, data):
"""
Yield transformed mappings from a `data` list of mappings keeping only
columns with a name in the `column_filters`of this Transformer.
Return the data unchanged if no `column_filters` exists.
"""
column_filters = set(self.clean_columns(self.column_filters))
for entry in data:
items = ((k, v) for k, v in entry.items() if k in column_filters)
yield OrderedDict(items)
def filter_rows(self, data):
"""
Yield a filtered list of mappings from a `data` list of mappings keeping
only items that match any one of the `row_filters` of this Transformer.
Return the data unchanged if no `row_filters` is avilable in this
Transformer.
"""
filters = self.row_filters
for entry in data:
for filt in filters:
for filtered_column_name, filtered_column_value in filt.items():
if entry.get(filtered_column_name) == filtered_column_value:
yield entry
def check_duplicate_columns(column_names):
"""
Check that there are no duplicate in the `column_names` list of column name
strings, ignoring case. Return a list of unique duplicated column names.
"""
counted = Counter(c.lower() for c in column_names)
return [column for column, count in sorted(counted.items()) if count > 1]
def read_csv_rows(location):
"""
Yield rows (as a list of values) from a CSV file at `location`.
"""
with io.open(location, encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
yield row
def write_csv(location, data, column_names): # NOQA
"""
Write a CSV file at `location` the `data` list of ordered mappings using the
`column_names`.
"""
with io.open(location, 'w', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=column_names)
writer.writeheader()
writer.writerows(data)