|
21 | 21 | import types
|
22 | 22 | import time
|
23 | 23 | import sys
|
| 24 | +import argparse |
24 | 25 |
|
25 | 26 | import schema_remarks
|
26 | 27 | import get_schema
|
@@ -868,21 +869,36 @@ def make_tables(first, last):
|
868 | 869 | return (header, body, footer)
|
869 | 870 |
|
870 | 871 |
|
871 |
| -def write_file(first, last, filename): |
872 |
| - file = open(filename, 'w') |
| 872 | +def write_file(first, last, file): |
| 873 | + # file = open(filename, 'w') |
| 874 | + # file is an already-open filehandle |
873 | 875 | (header, body, footer) = make_tables(first, last)
|
874 | 876 | file.write(header)
|
875 | 877 | file.write(body)
|
876 | 878 | file.write(footer)
|
877 | 879 | file.close()
|
878 | 880 |
|
879 | 881 |
|
880 |
| -def make_body(first, last): |
881 |
| - (header, body, footer) = make_tables(first, last) |
882 |
| - return body |
| 882 | +def test_schema_remarks(args): |
| 883 | + first = args.first |
| 884 | + last = args.last |
| 885 | + file = args.file |
| 886 | + if last == None: |
| 887 | + last = first |
| 888 | + if file: |
| 889 | + write_file(first, last, file) |
| 890 | + else: |
| 891 | + try: |
| 892 | + (header, body, footer) = make_tables(first, last) |
| 893 | + except BzSchemaProcessingException as e: |
| 894 | + message = e.message |
| 895 | + message = message.replace('<br/>', '') |
| 896 | + print(message) |
| 897 | + sys.exit() |
| 898 | + print("Succeeded!") |
883 | 899 |
|
884 | 900 |
|
885 |
| -def validate_schema_remarks(): |
| 901 | +def validate_schema_remarks(*args, **kwargs): |
886 | 902 | for v in schema_remarks.version_order:
|
887 | 903 | if not v in schema_remarks.version_schema_map:
|
888 | 904 | errors.append(
|
@@ -912,32 +928,56 @@ def validate_schema_remarks():
|
912 | 928 | f"Version {v} found in version_remark is not listed in version_schema_map"
|
913 | 929 | )
|
914 | 930 | if errors:
|
915 |
| - e = str.join('<br/>\n', errors) |
916 |
| - raise BzSchemaProcessingException(e) |
| 931 | + print(str.join('\n', errors)) |
| 932 | + sys.exit() |
917 | 933 | print("Versions validated.")
|
918 | 934 |
|
919 | 935 |
|
920 | 936 | if __name__ == "__main__":
|
921 |
| - if len(sys.argv) == 2 and sys.argv[1] == '--validate': |
922 |
| - validate_schema_remarks() |
923 |
| - elif len(sys.argv) == 3: |
924 |
| - (first, last) = sys.argv[1:] |
925 |
| - try: |
926 |
| - (header, body, footer) = make_tables(first, last) |
927 |
| - except BzSchemaProcessingException as e: |
928 |
| - print(e.message) |
929 |
| - sys.exit() |
930 |
| - print("Succeeded!") |
931 |
| - else: |
932 |
| - try: |
933 |
| - (first, last, filename) = sys.argv[1:] |
934 |
| - except ValueError: |
935 |
| - print( |
936 |
| - "Please pass the starting and ending schema versions and an optional filename to output to," |
937 |
| - ) |
938 |
| - print("or pass --validate to check for version number sanity.") |
939 |
| - sys.exit() |
940 |
| - write_file(first, last, filename) |
| 937 | + parser = argparse.ArgumentParser( |
| 938 | + description="A utility for generating schema comparison documents." |
| 939 | + ) |
| 940 | + subparsers = parser.add_subparsers( |
| 941 | + required=True, |
| 942 | + metavar='subcommand', |
| 943 | + help='Type `%(prog)s {subcommand} -h` for additional help', |
| 944 | + title='Available subcommands', |
| 945 | + ) |
| 946 | + parser_validate = subparsers.add_parser( |
| 947 | + 'validate', |
| 948 | + help='Validate that the version-related lists are in sync with each other', |
| 949 | + description='Validate that the version-related lists are in sync with each other', |
| 950 | + ) |
| 951 | + parser_validate.set_defaults(func=validate_schema_remarks) |
| 952 | + parser_test = subparsers.add_parser( |
| 953 | + 'test', |
| 954 | + help='Test schema document generation. By default it only prints errors or a success message.', |
| 955 | + description='Test schema document generation. By default it only prints errors or a success message.', |
| 956 | + ) |
| 957 | + parser_test.add_argument( |
| 958 | + 'first', |
| 959 | + metavar="first", |
| 960 | + choices=schema_remarks.version_order, |
| 961 | + help="The starting version of the schemas to compare, or the single version to display if 'last' is not provided.", |
| 962 | + ) |
| 963 | + parser_test.add_argument( |
| 964 | + 'last', |
| 965 | + metavar="last", |
| 966 | + choices=schema_remarks.version_order, |
| 967 | + nargs="?", |
| 968 | + default=None, |
| 969 | + help="The destination version of the schema to compare", |
| 970 | + ) |
| 971 | + parser_test.add_argument( |
| 972 | + '-f', |
| 973 | + dest="file", |
| 974 | + metavar='FILENAME', |
| 975 | + type=argparse.FileType('w'), |
| 976 | + help="A file to write the generated schema doc to. Passing - will write it to standard out.", |
| 977 | + ) |
| 978 | + parser_test.set_defaults(func=test_schema_remarks) |
| 979 | + args = parser.parse_args() |
| 980 | + args.func(args) |
941 | 981 |
|
942 | 982 | # A. REFERENCES
|
943 | 983 | #
|
|
0 commit comments