@@ -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
321357def check_duplicate_fields (field_names ):
322358 """
0 commit comments