Skip to content

Commit df525be

Browse files
authored
Merge pull request #422 from srthkdb/417_json_transform
420: added `exclude_fields` to transform
2 parents dd5eae9 + a34904e commit df525be

File tree

9 files changed

+78
-8
lines changed

9 files changed

+78
-8
lines changed

docs/UsingAboutCodetoDocumentYourSoftwareAssets.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,19 @@ and "version" columns and no other column:
287287
- name
288288
- version
289289

290+
* exclude_fields:
291+
An optional list of field names that should be excluded in the transformed CSV/JSON. If
292+
this list is provided, all the fields from the source CSV/JSON that should be excluded
293+
in the target CSV/JSON must be listed. Excluding standard or required fields will cause
294+
an error. If this list is not provided, all source CSV/JSON fields are kept in the
295+
transformed target CSV/JSON.
296+
297+
For instance with this configuration the target CSV/JSON will not contain the "type"
298+
and "temp" fields:
299+
exclude_fields:
300+
- type
301+
- temp
302+
290303

291304
## <a name="RungentoGenerateAboutCodeToolkitFiles">Run gen to Generate AboutCode Toolkit Files</a>
292305

etc/scripts/irc-notify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def appveyor_vars():
139139
response = line.split()
140140

141141
if response[0] == 'PING':
142-
irc_file.send('PONG {}\r\n'.format(reponse[1]).encode())
142+
irc_file.send('PONG {}\r\n'.format(response[1]).encode())
143143

144144
elif response[1] == '433':
145145
irc_sock.send('NICK {}\r\n'.format(irc_nick).encode())

src/attributecode/transform.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ def transform_csv(rows, transformer):
109109
data = list(transformer.filter_fields(data))
110110
field_names = [c for c in field_names if c in transformer.field_filters]
111111

112+
if transformer.exclude_fields:
113+
data = list(transformer.filter_excluded(data))
114+
field_names = [c for c in field_names if c not in transformer.exclude_fields]
115+
112116
errors = transformer.check_required_fields(data)
113117

114118
return field_names, data, errors
@@ -162,6 +166,11 @@ def process_json_keys(data, renamings, transformer):
162166
new_data = list(transformer.filter_fields(new_data))
163167
else:
164168
new_data = list(new_data)
169+
170+
if transformer.exclude_fields:
171+
new_data = list(transformer.filter_excluded(new_data))
172+
else:
173+
new_data = list(new_data)
165174

166175
errors = transformer.check_required_fields(new_data)
167176
return new_data, errors
@@ -212,6 +221,19 @@ def process_json_keys(data, renamings, transformer):
212221
field_filters:
213222
- name
214223
- version
224+
225+
* exclude_fields:
226+
An optional list of field names that should be excluded in the transformed CSV/JSON. If
227+
this list is provided, all the fields from the source CSV/JSON that should be excluded
228+
in the target CSV/JSON must be listed. Excluding standard or required fields will cause
229+
an error. If this list is not provided, all source CSV/JSON fields are kept in the
230+
transformed target CSV/JSON.
231+
232+
For instance with this configuration the target CSV/JSON will not contain the "type"
233+
and "temp" fields:
234+
exclude_fields:
235+
- type
236+
- temp
215237
'''
216238

217239

@@ -222,6 +244,7 @@ class Transformer(object):
222244
field_renamings = attr.attrib(default=attr.Factory(dict))
223245
required_fields = attr.attrib(default=attr.Factory(list))
224246
field_filters = attr.attrib(default=attr.Factory(list))
247+
exclude_fields = attr.attrib(default=attr.Factory(list))
225248

226249
# a list of all the standard fields from AboutCode toolkit
227250
standard_fields = attr.attrib(default=attr.Factory(list), init=False)
@@ -245,6 +268,7 @@ def default(cls):
245268
field_renamings={},
246269
required_fields=[],
247270
field_filters=[],
271+
exclude_fields=[],
248272
)
249273

250274
@classmethod
@@ -259,6 +283,7 @@ def from_file(cls, location):
259283
field_renamings=data.get('field_renamings', {}),
260284
required_fields=data.get('required_fields', []),
261285
field_filters=data.get('field_filters', []),
286+
exclude_fields=data.get('exclude_fields', []),
262287
)
263288

264289
def check_required_fields(self, data):
@@ -317,6 +342,17 @@ def filter_fields(self, data):
317342
items = ((k, v) for k, v in entry.items() if k in field_filters)
318343
yield OrderedDict(items)
319344

345+
def filter_excluded(self, data):
346+
"""
347+
Yield transformed dicts from a `data` list of dicts excluding
348+
fields with names in the `exclude_fields`of this Transformer.
349+
Return the data unchanged if no `exclude_fields` exists.
350+
"""
351+
exclude_fields = set(self.clean_fields(self.exclude_fields))
352+
for entry in data:
353+
items = ((k, v) for k, v in entry.items() if k not in exclude_fields)
354+
yield OrderedDict(items)
355+
320356

321357
def check_duplicate_fields(field_names):
322358
"""

tests/testdata/test_cmd/help/about_transform_config_help.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,16 @@ and "version" fields and no other field:
4444
- name
4545
- version
4646

47+
* exclude_fields:
48+
An optional list of field names that should be excluded in the transformed CSV/JSON. If
49+
this list is provided, all the fields from the source CSV/JSON that should be excluded
50+
in the target CSV/JSON must be listed. Excluding standard or required fields will cause
51+
an error. If this list is not provided, all source CSV/JSON fields are kept in the
52+
transformed target CSV/JSON.
53+
54+
For instance with this configuration the target CSV/JSON will not contain the "type"
55+
and "temp" fields:
56+
exclude_fields:
57+
- type
58+
- temp
59+

tests/testdata/test_transform/configuration

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ field_filters:
55
- about_resource
66
- name
77
- version
8+
- temp
89
required_fields:
910
- name
10-
- version
11+
- version
12+
exclude_fields:
13+
- temp

tests/testdata/test_transform/configuration_scancode

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ field_filters:
55
- name
66
- new_extension
77
- about_resource
8+
- type
89
required_fields:
910
- name
10-
11+
exclude_fields:
12+
- type
1113

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Directory/Filename,Component,version,notes
2-
/tmp/test.c, test.c,1,test
1+
Directory/Filename,Component,version,notes,temp
2+
/tmp/test.c, test.c,1,test,foo

tests/testdata/test_transform/input.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"Directory/Filename": "/aboutcode-toolkit/",
33
"Component": "AboutCode-toolkit",
44
"version": "1.2.3",
5-
"note": "test"
5+
"note": "test",
6+
"temp": "foo"
67
}

tests/testdata/test_transform/input_as_array.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
{
33
"Directory/Filename": "/aboutcode-toolkit/",
44
"Component": "AboutCode-toolkit",
5-
"version": "1.0"
5+
"version": "1.0",
6+
"temp": "fpp"
67
},
78
{
89
"Directory/Filename": "/aboutcode-toolkit1/",
910
"Component": "AboutCode-toolkit1",
10-
"version": "1.1"
11+
"version": "1.1",
12+
"temp": "foo"
1113
}
1214
]

0 commit comments

Comments
 (0)