Skip to content

Commit 1a06663

Browse files
committed
Commit code update for #108
It's now able to log custom fields from ABOUT file to CSV. ToDo: write and update test cases
1 parent 41dd664 commit 1a06663

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

about_code_tool/about.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ def repr_problem(obj):
212212

213213
HEADER_ROW_FIELDS = (('about_file',)
214214
+ MANDATORY_FIELDS
215-
+ OPTIONAL_FIELDS
216-
+ ERROR_WARN_FIELDS)
215+
+ OPTIONAL_FIELDS)
217216

218217

219218
# SPDX License Identifiers from http://spdx.org/licenses/
@@ -687,10 +686,9 @@ def validate(self):
687686

688687
for field_name, value in self.validated_fields.items():
689688
self.check_is_ascii(self.validated_fields.get(field_name))
690-
self.validate_known_optional_fields(field_name)
689+
#self.validate_known_optional_fields(field_name)
691690
self.validate_file_field_exists(field_name, value)
692691
self.validate_url_field(field_name, network_check=False)
693-
694692
self.validate_spdx_license(field_name, value)
695693
self.check_date_format(field_name)
696694

@@ -920,17 +918,32 @@ def check_url_reachable(host, path):
920918
# http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
921919
return conn.getresponse().status
922920

923-
def get_row_data(self, updated_path):
921+
def get_custom_field_keys(self):
922+
custom_key = []
923+
for key in self.validated_fields:
924+
if key not in MANDATORY_FIELDS + OPTIONAL_FIELDS:
925+
custom_key.append(key)
926+
return custom_key
927+
928+
def get_row_data(self, updated_path, custom_keys):
924929
"""
925930
Create a csv compatible row of data for this object.
926931
"""
927932
row = [updated_path]
933+
custom_field = []
928934
for field in MANDATORY_FIELDS + OPTIONAL_FIELDS:
929935
if field in self.validated_fields:
930936
row += [self.validated_fields[field]]
931937
else:
932938
row += ['']
933939

940+
# Add custom field value
941+
for key in custom_keys:
942+
try:
943+
row += [self.validated_fields[key]]
944+
except:
945+
row += ['']
946+
934947
warnings = [repr(w) for w in self.warnings]
935948
errors = [repr(e) for e in self.errors]
936949
row += ['\n'.join(warnings), '\n'.join(errors)]
@@ -1123,18 +1136,33 @@ def get_relative_path(self, about_object_location):
11231136
else:
11241137
return user_provided_path.replace('\\', '/')
11251138

1139+
def custom_keys(self):
1140+
custom_keys = []
1141+
for about_object in self:
1142+
keys = about_object.get_custom_field_keys()
1143+
for key in keys:
1144+
if key not in custom_keys:
1145+
custom_keys.append(key)
1146+
return custom_keys
1147+
11261148
def write_to_csv(self, output_path):
11271149
"""
11281150
Build a row for each about instance and write results in CSV file
11291151
located at `output_path`.
11301152
"""
1153+
custom_keys = self.custom_keys()
11311154
with open(output_path, 'wb') as output_file:
11321155
csv_writer = csv.writer(output_file)
1133-
csv_writer.writerow(HEADER_ROW_FIELDS)
1156+
header_row = HEADER_ROW_FIELDS
1157+
# Add the non-supported fields if exist
1158+
for key in custom_keys:
1159+
header_row += (key, )
1160+
header_row += ERROR_WARN_FIELDS
1161+
csv_writer.writerow(header_row)
11341162

11351163
for about_object in self:
11361164
relative_path = self.get_relative_path(about_object.location)
1137-
row_data = about_object.get_row_data(relative_path)
1165+
row_data = about_object.get_row_data(relative_path, custom_keys)
11381166
csv_writer.writerow(row_data)
11391167

11401168
def generate_attribution(self, template_path=None, limit_to=None):

about_code_tool/tests/test_about.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def test_collector_errors_encapsulation(self):
126126
def test_collector_warnings_encapsulation(self):
127127
input_path = 'about_code_tool/tests/testdata/allAboutInOneDir'
128128
collector = about.AboutCollector(input_path)
129+
print("#####################################################")
130+
print(collector.warnings)
131+
print(collector.errors)
129132
self.assertEqual(4, len(collector.warnings))
130133

131134

0 commit comments

Comments
 (0)