Skip to content

Commit 5f2a8f2

Browse files
committed
Fixed the filtering of dicts with empty values.
1 parent 05fef35 commit 5f2a8f2

File tree

2 files changed

+52
-33
lines changed

2 files changed

+52
-33
lines changed

about_code_tool/genabout.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ def __init__(self):
163163
self.extract_dje_license_error = False
164164

165165
@staticmethod
166-
# FIXME: why use a static and not a regular function?
167166
def get_duplicated_keys(input_file):
167+
# FIXME: why use a static and not a regular function?
168168
csv_context = csv.reader(open(input_file, 'rU'))
169169
keys_row = csv_context.next()
170170
lower_case_keys_row = [k.lower() for k in keys_row]
@@ -173,40 +173,49 @@ def get_duplicated_keys(input_file):
173173
def validate(self, input_list):
174174
if not self.validate_mandatory_fields(input_list):
175175
required_keys = MANDATORY_FIELDS + ('about_file',)
176-
print("Required keys not found.")
176+
print('ERROR: Required column/keys not found in the <input_path> CSV.')
177177
print(required_keys)
178178
print("Use the '--mapping' option to map the input keys and verify the mapping information are correct.")
179-
print("OR correct the header keys from the input CSV.")
179+
print("OR correct the column names in the <input_path> CSV.")
180180
sys.exit(errno.EINVAL)
181181

182182
elif not self.validate_value_in_essential_fields(input_list):
183-
print("Some of the essential fields value are missing.")
183+
print("ERROR: Some of the essential column/fields value are missing in the <input_path> CSV.")
184184
print(ESSENTIAL_FIELDS)
185-
print("Please check the input CSV.")
186-
print("No ABOUT file is created.")
185+
print("Please check the the <input_path> CSV.")
186+
print("No ABOUT file was generated.")
187187
sys.exit(errno.EINVAL)
188188

189189
elif has_duplicate_about_file_paths(input_list):
190-
print("The input has duplicated 'about_file'.")
191-
print("Duplication is not supported. Please correct the input and rerun the tool.")
192-
print("No ABOUT file is created.")
190+
print("ERROR: The <input_path> CSV has duplicated 'about_file' values.")
191+
print("Duplication is not supported. Please correct the the <input_path> CSV and rerun the tool.")
192+
print("No ABOUT file was generated.")
193193
sys.exit(errno.EINVAL)
194194

195195
@staticmethod
196-
def validate_mandatory_fields(input_list):
196+
def validate_mandatory_fields(abouts):
197+
"""
198+
Given a list of About data dictionaries, validate the presence of
199+
mandatory fields. Return True if they are present.
200+
"""
197201
# FIXME: why use a static and not a regular function?
198-
for line in input_list:
202+
203+
for about in abouts:
199204
for key in MANDATORY_FIELDS + ESSENTIAL_FIELDS:
200-
if not key in line.keys():
205+
if not key in about:
201206
return False
202207
return True
203208

204209
@staticmethod
205-
def validate_value_in_essential_fields(input_list):
210+
def validate_value_in_essential_fields(abouts):
211+
"""
212+
Given a list of About data dictionaries, validate the presence of
213+
essential fields. Return True if they are present.
214+
"""
206215
# FIXME: why use a static and not a regular function?
207-
for line in input_list:
216+
for about in abouts:
208217
for key in ESSENTIAL_FIELDS:
209-
if not line[key]:
218+
if not about.get(key):
210219
return False
211220
return True
212221

@@ -604,16 +613,15 @@ def warnings_errors_summary(self):
604613
file_logger.warning(warning_msg)
605614

606615

607-
def filter_empty_values(abouts):
616+
def filter_dicts_of_empty_values(abouts):
608617
"""
609-
Return a list of About data dictionaries without any empty value for all
610-
key/value pairs.
618+
Return a list of About data dictionaries filtering dictionaries composed
619+
entirely of empty values.
611620
"""
612621
filtered = []
613622
for about in abouts:
614-
about_items = [(key, value,) for key, value in about.items() if value and value.strip()]
615-
if about_items:
616-
filtered.append(dict(about_items))
623+
if any(v and v.strip() for v in about.values()):
624+
filtered.append(dict(about))
617625
return filtered
618626

619627

@@ -767,7 +775,7 @@ def main(parser, options, args):
767775
file_logger.addHandler(file_handler)
768776

769777
input_data = load_data_from_csv(input_path)
770-
input_data = filter_empty_values(input_data)
778+
input_data = filter_dicts_of_empty_values(input_data)
771779

772780
user_keys = []
773781
if mapping_config:
@@ -777,7 +785,7 @@ def main(parser, options, args):
777785

778786
gen.validate(input_data)
779787

780-
ignored_fields_list = gen.get_unknown_fields(input_data, user_keys)
788+
ignored_fields_list = get_unknown_fields(input_data, user_keys)
781789
if ignored_fields_list:
782790
input_list = gen.get_only_supported_fields(input_data, ignored_fields_list)
783791

about_code_tool/tests/test_genabout.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
from os.path import join
2525

2626
from about_code_tool import genabout
27-
2827
from about_code_tool.tests.tstutil import get_temp_dir
29-
3028
from about_code_tool.tests import test_about
3129

3230

@@ -66,7 +64,7 @@ def test_load_data_from_csv_does_converts_all_keys_to_lower(self):
6664
result = genabout.load_data_from_csv(test_input)
6765
self.assertEqual(expected, result)
6866

69-
def test_filter_empty_values(self):
67+
def test_filter_dicts_of_empty_values(self):
7068
test_abouts = [
7169
{'about_file': '/about.ABOUT',
7270
'about_resource': '.',
@@ -83,8 +81,23 @@ def test_filter_empty_values(self):
8381
'name': 'ABOUT tool',
8482
'version': '0.8.1'}]
8583

86-
result = genabout.filter_empty_values(test_abouts)
87-
self.assertEqual(result, expected)
84+
result = genabout.filter_dicts_of_empty_values(test_abouts)
85+
self.assertEqual(expected, result)
86+
87+
def test_filter_dicts_of_empty_values_does_not_filter_data_with_value(self):
88+
test_abouts = [{'asaszname': '', 'version': '1'}]
89+
result = genabout.filter_dicts_of_empty_values(test_abouts)
90+
self.assertEqual(test_abouts, result)
91+
92+
def test_filter_dicts_of_empty_values_does_filter_data_with_empty_string_values(self):
93+
test_abouts = [{'asaszname': '', 'version': ''}]
94+
result = genabout.filter_dicts_of_empty_values(test_abouts)
95+
self.assertEqual([], result)
96+
97+
def test_filter_dicts_of_empty_values_does_filter_data_with_space_only_values(self):
98+
test_abouts = [{'asaszname': ' ', 'version': ' '}]
99+
result = genabout.filter_dicts_of_empty_values(test_abouts)
100+
self.assertEqual([], result)
88101

89102
def test_validate_value_in_essential_missing_about_file(self):
90103
gen = genabout.GenAbout()
@@ -523,15 +536,13 @@ def test_format_output(self):
523536

524537
def test_format_output_with_continuation(self):
525538
gen = genabout.GenAbout()
526-
test_fields = [[join(TESTDATA_DIR,
527-
'test_files_for_genabout/about.py.ABOUT'),
539+
test_fields = [[join(TESTDATA_DIR, 'test_files_for_genabout/about.py.ABOUT'),
528540
{'about_file': '/about.py.ABOUT',
529541
'version': '0.8.1',
530542
'about_resource': '.',
531543
'name': 'ABOUT Tool',
532544
'readme': 'This is a readme test with \nline continuation.'}]]
533-
expected = [[join(TESTDATA_DIR,
534-
'test_files_for_genabout/about.py.ABOUT'),
545+
expected = [[join(TESTDATA_DIR, 'test_files_for_genabout/about.py.ABOUT'),
535546
'about_resource: .\nname: ABOUT Tool\n'
536547
'version: 0.8.1\n\n'
537548
'readme: This is a readme test with \n line continuation.\n']]
@@ -672,7 +683,7 @@ def test_process_dje_licenses(self):
672683
test_path = '/test'
673684
expected = [[join(u'/test', 'test_key.LICENSE'), 'This is a test license.']]
674685
result = gen.process_dje_licenses(test_license_list, test_license_dict, test_path)
675-
self.assertEqual(result, expected)
686+
self.assertEqual(expected, result)
676687

677688
def test_update_about_resource_about_file_and_field_exist(self):
678689
gen = genabout.GenAbout()

0 commit comments

Comments
 (0)