Skip to content

Commit 20ba225

Browse files
committed
Several cosmetic and PEP8 fixes.
1 parent ee42bbb commit 20ba225

File tree

5 files changed

+904
-671
lines changed

5 files changed

+904
-671
lines changed

about_code_tool/about.py

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ def pre_process(self, file_in):
621621

622622
# invalid field characters
623623
_invalid_chars, warn = (
624-
self.check_invalid_chars_in_field_name(field_name, line))
624+
check_invalid_chars(field_name, line))
625625
if warn:
626626
warnings.append(warn)
627627
last_line_is_field_or_continuation = False
@@ -661,26 +661,6 @@ def check_invalid_space_characters(field_name, line):
661661
warnings = Warn(IGNORED, field_name, line, msg)
662662
return warnings
663663

664-
@staticmethod
665-
def check_invalid_chars_in_field_name(field_name, line):
666-
"""
667-
Return a sequence of invalid characters in a field name.
668-
From spec 0.8.0:
669-
A field name can contain only these US-ASCII characters:
670-
<li> digits from 0 to 9 </li>
671-
<li> uppercase and lowercase letters from A to Z</li>
672-
<li> the _ underscore sign. </li>
673-
"""
674-
supported = string.digits + string.ascii_letters + '_'
675-
warnings = ''
676-
invalid_chars = [char for char in field_name
677-
if char not in supported]
678-
if invalid_chars:
679-
msg = ('Field name contains invalid characters: %r: line ignored.'
680-
% (''.join(invalid_chars)))
681-
682-
warnings = Warn(IGNORED, field_name, line, msg)
683-
return invalid_chars, warnings
684664

685665
def normalize(self):
686666
"""
@@ -719,7 +699,7 @@ def validate(self):
719699

720700
for field_name, value in self.validated_fields.items():
721701
self.check_is_ascii(self.validated_fields.get(field_name))
722-
#self.validate_known_optional_fields(field_name)
702+
# self.validate_known_optional_fields(field_name)
723703
self.validate_file_field_exists(field_name, value)
724704
self.validate_url_field(field_name, network_check=False)
725705
self.validate_spdx_license(field_name, value)
@@ -963,6 +943,7 @@ def get_row_data(self, updated_path, custom_keys):
963943
Create a csv compatible row of data for this object.
964944
"""
965945
row = [updated_path]
946+
# FIXME: this list is not used? what was the intent?
966947
custom_field = []
967948
for field in MANDATORY_FIELDS + OPTIONAL_FIELDS:
968949
if field in self.validated_fields:
@@ -1013,7 +994,7 @@ def duplicate_file_names_when_lowercased(file_location):
1013994
files stored in the same directory have the same lowercase file
1014995
name.
1015996
"""
1016-
# TODO: Add a test
997+
# TODO: Add a test, only for a case sensitive FS, such as on Linux
1017998
names = []
1018999
for name in os.listdir(os.path.dirname(file_location)):
10191000
if name.lower() in names:
@@ -1069,6 +1050,27 @@ def get_about_name(self):
10691050
return self.parsed.get('name', '')
10701051

10711052

1053+
def check_invalid_chars(field_name, line):
1054+
"""
1055+
Return a sequence of invalid characters in a field name.
1056+
From spec 0.8.0:
1057+
A field name can contain only these US-ASCII characters:
1058+
<li> digits from 0 to 9 </li>
1059+
<li> uppercase and lowercase letters from A to Z</li>
1060+
<li> the _ underscore sign. </li>
1061+
"""
1062+
supported = string.digits + string.ascii_letters + '_'
1063+
warnings = ''
1064+
invalid_chars = [char for char in field_name
1065+
if char not in supported]
1066+
if invalid_chars:
1067+
msg = ('Field name contains invalid characters: %r: line ignored.'
1068+
% (''.join(invalid_chars)))
1069+
1070+
warnings = Warn(IGNORED, field_name, line, msg)
1071+
return invalid_chars, warnings
1072+
1073+
10721074
class Collector(object):
10731075
"""
10741076
Collect ABOUT files.
@@ -1213,7 +1215,7 @@ def write_to_csv(self, output_path):
12131215
header_row = HEADER_ROW_FIELDS
12141216
# Add the non-supported fields if exist
12151217
for key in custom_keys:
1216-
header_row += (key, )
1218+
header_row += (key,)
12171219
header_row += ERROR_WARN_FIELDS
12181220
csv_writer.writerow(header_row)
12191221

@@ -1349,13 +1351,16 @@ def get_genattrib_errors(self):
13491351
"""
13501352
)
13511353

1354+
1355+
ERROR = 0
1356+
OK = 1
13521357
def main(parser, options, args):
13531358
overwrite = options.overwrite
13541359
verbosity = options.verbosity
13551360

13561361
if options.version:
13571362
print('ABOUT tool {0}\n{1}'.format(__version__, __copyright__))
1358-
sys.exit(0)
1363+
return ERROR
13591364

13601365
if verbosity == 1:
13611366
handler.setLevel(logging.ERROR)
@@ -1366,7 +1371,7 @@ def main(parser, options, args):
13661371
print('Input and Output paths are required.')
13671372
print()
13681373
parser.print_help()
1369-
sys.exit(errno.EEXIST)
1374+
return errno.EEXIST
13701375

13711376
input_path, output_path = args
13721377
output_path = os.path.abspath(output_path)
@@ -1375,26 +1380,26 @@ def main(parser, options, args):
13751380
print('Input path does not exist.')
13761381
print()
13771382
parser.print_help()
1378-
sys.exit(errno.EEXIST)
1383+
return errno.EEXIST
13791384

13801385
if os.path.isdir(output_path):
13811386
print('Output must be a file, not a directory.')
13821387
print()
13831388
parser.print_help()
1384-
sys.exit(errno.EISDIR)
1389+
return errno.EISDIR
13851390

13861391
if not output_path.endswith('.csv'):
13871392
print('Output file name must end with ".csv".')
13881393
print()
13891394
parser.print_help()
1390-
sys.exit(errno.EINVAL)
1395+
return errno.EINVAL
13911396

13921397
if os.path.exists(output_path) and not overwrite:
13931398
print('Output file already exists. Select a different file name '
13941399
'or use the --overwrite option.')
13951400
print()
13961401
parser.print_help()
1397-
sys.exit(errno.EEXIST)
1402+
return errno.EEXIST
13981403

13991404
if (not os.path.exists(output_path)
14001405
or (os.path.exists(output_path) and overwrite)):
@@ -1404,6 +1409,7 @@ def main(parser, options, args):
14041409
print('%d errors detected.' % len(collector.errors))
14051410
if collector.warnings:
14061411
print('%d warnings detected.' % len(collector.warnings))
1412+
return OK
14071413
else:
14081414
# we should never reach this
14091415
assert False, 'Unsupported option(s).'
@@ -1464,4 +1470,4 @@ def format_option(self, option):
14641470
if __name__ == '__main__':
14651471
parser = get_parser()
14661472
options, args = parser.parse_args()
1467-
main(parser, options, args)
1473+
sys.exit(main(parser, options, args))

0 commit comments

Comments
 (0)