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