|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import absolute_import, division, print_function, unicode_literals, generators, with_statement, nested_scopes |
| 4 | +import argparse |
| 5 | +import sys |
| 6 | +from parse_message_file import ( |
| 7 | + parse_file, display_services, to_json, from_json, load_from_json_file, save_to_json_file, |
| 8 | + services_to_string, save_services_to_file, services_to_html, save_services_to_html_file |
| 9 | +) |
| 10 | + |
| 11 | +def main(): |
| 12 | + parser = argparse.ArgumentParser(description="Parse and display message file content.") |
| 13 | + parser.add_argument("filename", help="Path to the file to be parsed") |
| 14 | + parser.add_argument("--validate-only", "-v", action="store_true", help="Only validate the file without displaying") |
| 15 | + parser.add_argument("--verbose", "-V", action="store_true", help="Enable verbose mode") |
| 16 | + parser.add_argument("--debug", "-d", action="store_true", help="Enable debug mode") |
| 17 | + parser.add_argument("--to-json", "-j", help="Convert the parsed data to JSON and save to a file") |
| 18 | + parser.add_argument("--from-json", "-J", help="Load the services data structure from a JSON file") |
| 19 | + parser.add_argument("--json-string", "-s", type=str, help="JSON string to parse if --from-json is specified") |
| 20 | + parser.add_argument("--to-html", "-H", help="Convert the parsed data to HTML and save to a file") |
| 21 | + parser.add_argument("--to-original", "-o", help="Convert the parsed data back to the original format and save to a file") |
| 22 | + parser.add_argument("--line-ending", "-l", choices=["lf", "cr", "crlf"], default="lf", help="Specify the line ending format for the output file") |
| 23 | + args = parser.parse_args() |
| 24 | + |
| 25 | + try: |
| 26 | + if args.from_json: |
| 27 | + if args.json_string: |
| 28 | + services = from_json(args.json_string) |
| 29 | + else: |
| 30 | + services = load_from_json_file(args.from_json) |
| 31 | + display_services(services) |
| 32 | + else: |
| 33 | + if args.validate_only: |
| 34 | + is_valid, error_message, error_line = parse_file(args.filename, validate_only=True, verbose=args.verbose) |
| 35 | + if is_valid: |
| 36 | + print("The file '{0}' is valid.".format(args.filename)) |
| 37 | + else: |
| 38 | + print("Validation Error: {0}".format(error_message)) |
| 39 | + print("Line: {0}".format(error_line.strip())) |
| 40 | + else: |
| 41 | + services = parse_file(args.filename, verbose=args.verbose) |
| 42 | + if args.debug: |
| 43 | + import pdb; pdb.set_trace() |
| 44 | + if args.to_json: |
| 45 | + save_to_json_file(services, args.to_json) |
| 46 | + print("Saved JSON to {0}".format(args.to_json)) |
| 47 | + elif args.to_html: |
| 48 | + save_services_to_html_file(services, args.to_html) |
| 49 | + print("Saved HTML to {0}".format(args.to_html)) |
| 50 | + elif args.to_original: |
| 51 | + save_services_to_file(services, args.to_original, line_ending=args.line_ending) |
| 52 | + print("Saved original format to {0}".format(args.to_original)) |
| 53 | + else: |
| 54 | + display_services(services) |
| 55 | + except Exception as e: |
| 56 | + print("An error occurred: {0}".format(e), file=sys.stderr) |
| 57 | + sys.exit(1) |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + main() |
0 commit comments