Skip to content

Commit 7d8ecb6

Browse files
committed
#35 first draft of interface swap
1 parent b370da5 commit 7d8ecb6

File tree

1 file changed

+48
-71
lines changed

1 file changed

+48
-71
lines changed

about.py

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
from __future__ import print_function
2525
from __future__ import with_statement
2626

27+
import argparse
2728
import codecs
2829
import csv
2930
import errno
3031
import fnmatch
31-
import getopt
3232
import httplib
3333
import posixpath
3434
import socket
@@ -553,7 +553,7 @@ def license_text(self):
553553
license_text_path = self.file_fields_locations["license_text_file"]
554554
with open(license_text_path, 'rU') as f:
555555
return f.read()
556-
except Exception as e:
556+
except Exception as e:
557557
pass
558558
#return empty string if the license file does not exist
559559
return ""
@@ -567,7 +567,7 @@ def notice_text(self):
567567
notice_text_path = self.file_fields_locations["notice_file"]
568568
with open(notice_text_path, 'rU') as f:
569569
return f.read()
570-
except Exception as e:
570+
except Exception as e:
571571
pass
572572
#return empty string if the notice file does not exist
573573
return ""
@@ -881,7 +881,7 @@ def resource_name(resource_path):
881881

882882

883883
class AboutCollector(object):
884-
def __init__(self, input_path, output_path, opt_arg_num):
884+
def __init__(self, input_path, output_path, verbosity):
885885
# Setup the input and output paths
886886
self.original_input_path = input_path
887887
self.input_path = abspath(input_path)
@@ -891,9 +891,9 @@ def __init__(self, input_path, output_path, opt_arg_num):
891891

892892
# Setup the verbosity
893893
self.display_error = self.display_error_and_warning = False
894-
if opt_arg_num == '1':
894+
if verbosity == 1:
895895
self.display_error = True
896-
elif opt_arg_num == '2':
896+
elif verbosity == 2:
897897
self.display_error_and_warning = True
898898

899899
self.about_files = []
@@ -1015,21 +1015,21 @@ def generate_attribution(self, template_path='templates/default.html',
10151015
return
10161016

10171017
# We only need the fields names and values to render the template
1018-
about_validated_fields = [about_object.validated_fields
1019-
for about_object in self.about_objects
1020-
if not sublist
1018+
about_validated_fields = [about_object.validated_fields
1019+
for about_object in self.about_objects
1020+
if not sublist
10211021
or about_object.about_resource_path in sublist]
10221022

1023-
about_license_text = [about_object.license_text()
1024-
for about_object in self.about_objects
1025-
if not sublist
1023+
about_license_text = [about_object.license_text()
1024+
for about_object in self.about_objects
1025+
if not sublist
10261026
or about_object.about_resource_path in sublist]
10271027
about_notice_text = [about_object.notice_text()
10281028
for about_object in self.about_objects
10291029
if not sublist
10301030
or about_object.about_resource_path in sublist]
10311031

1032-
return template.render(about_objects = about_validated_fields,
1032+
return template.render(about_objects = about_validated_fields,
10331033
license_texts = about_license_text,
10341034
notice_texts = about_notice_text)
10351035

@@ -1040,13 +1040,14 @@ def isvalid_about_file(file_name):
10401040
return fnmatch.fnmatch(file_name.lower(), "*.about")
10411041

10421042

1043-
def syntax():
1044-
print("""
1045-
Syntax:
1046-
about.py [Options] [Input] [Output]
1043+
SYNTAX = """
10471044
Input can be a file or directory.
10481045
Output must be a file with a .csv extension.
1049-
""")
1046+
"""
1047+
1048+
1049+
def syntax():
1050+
print(SYNTAX)
10501051

10511052

10521053
def option_usage():
@@ -1062,10 +1063,8 @@ def option_usage():
10621063
2 - Print error and warning messages
10631064
""")
10641065

1065-
1066-
def version():
1067-
print("""
1068-
ABOUT CODE: Version: %s
1066+
VERSION = """
1067+
ABOUT CODE: Version: {0}
10691068
Copyright (c) 2013 nexB Inc. All rights reserved.
10701069
http://dejacode.org
10711070
Licensed under the Apache License, Version 2.0 (the "License");
@@ -1076,48 +1075,18 @@ def version():
10761075
software distributed under the License is distributed on an "AS IS" BASIS,
10771076
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10781077
See the License for the specific language governing permissions and limitations
1079-
under the License.""" % __version__)
1080-
1081-
1082-
def main(args, opts):
1083-
overwrite = False
1084-
opt_arg_num = '0'
1085-
for opt, opt_arg in opts:
1086-
invalid_opt = True
1087-
if opt in ('-h', '--help'):
1088-
syntax()
1089-
option_usage()
1090-
sys.exit(0)
1091-
1092-
if opt in ('-v', '--version'):
1093-
version()
1094-
sys.exit(0)
1095-
1096-
if opt in ('--verbosity'):
1097-
invalid_opt = False
1098-
valid_opt_args = ['0', '1', '2']
1099-
if not opt_arg or not opt_arg in valid_opt_args:
1100-
print("Invalid option argument.")
1101-
option_usage()
1102-
sys.exit(errno.EINVAL)
1103-
else:
1104-
opt_arg_num = opt_arg
1078+
under the License.""".format(__version__)
11051079

1106-
if opt in ('--overwrite'):
1107-
invalid_opt = False
1108-
overwrite = True
11091080

1110-
if invalid_opt:
1111-
assert False, 'Unsupported option.'
1081+
def version():
1082+
print(VERSION)
11121083

1113-
if not len(args) == 2:
1114-
print('Input and output parameters are mandatory.')
1115-
syntax()
1116-
option_usage()
1117-
sys.exit(errno.EINVAL)
11181084

1119-
input_path = args[0]
1120-
output_path = args[1]
1085+
def main(args):
1086+
overwrite = args.overwrite
1087+
verbosity = args.verbosity
1088+
input_path = args.input_path
1089+
output_path = args.output_path
11211090

11221091
# TODO: need more path normalization (normpath, expanduser)
11231092
# input_path = abspath(input_path)
@@ -1146,21 +1115,29 @@ def main(args, opts):
11461115
sys.exit(errno.EEXIST)
11471116

11481117
if not exists(output_path) or (exists(output_path) and overwrite):
1149-
collector = AboutCollector(input_path, output_path, opt_arg_num)
1118+
collector = AboutCollector(input_path, output_path, verbosity)
11501119
collector.extract_about_info()
11511120
else:
11521121
# we should never reach this
11531122
assert False, "Unsupported option(s)."
11541123

11551124

11561125
if __name__ == "__main__":
1157-
longopts = ['help', 'version', 'overwrite', 'verbosity=']
1158-
try:
1159-
opts, args = getopt.getopt(sys.argv[1:], 'hv', longopts)
1160-
except Exception as e:
1161-
print(repr(e))
1162-
syntax()
1163-
option_usage()
1164-
sys.exit(errno.EINVAL)
1165-
1166-
main(args, opts)
1126+
parser = argparse.ArgumentParser(
1127+
description=SYNTAX, formatter_class=argparse.RawTextHelpFormatter)
1128+
parser.add_argument('--overwrite', action='store_true',
1129+
help='Overwrites the output file if it exists')
1130+
parser.add_argument('-v', '--version', action='version', version=VERSION,
1131+
help='Display current version, license notice, and '
1132+
'copyright notice')
1133+
VERBOSITY = """Print more or fewer verbose messages while processing ABOUT files
1134+
0 - Do not print any warning or error messages, just a total count (default)
1135+
1 - Print error messages
1136+
2 - Print error and warning messages"""
1137+
parser.add_argument('--verbosity', type=int, choices=[0, 1, 2],
1138+
help=VERBOSITY)
1139+
parser.add_argument('input_path', help='The input path')
1140+
parser.add_argument('output_path', help='The output path')
1141+
args = parser.parse_args()
1142+
1143+
main(args)

0 commit comments

Comments
 (0)