Add files via upload
[LoveSoStrong.git] / display_message_file.py
blob5f7f8edb6f920116e82bf0153d70868f9ce53e0d
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, to_xml, from_xml,
6 load_from_json_file, save_to_json_file, load_from_xml_file, save_to_xml_file,
7 services_to_string, save_services_to_file
10 def main():
11 parser = argparse.ArgumentParser(description="Parse and display message file content.")
12 parser.add_argument("filename", help="Path to the file to be parsed")
13 parser.add_argument("--validate-only", "-v", action="store_true", help="Only validate the file without displaying")
14 parser.add_argument("--verbose", "-V", action="store_true", help="Enable verbose mode")
15 parser.add_argument("--debug", "-d", action="store_true", help="Enable debug mode")
16 parser.add_argument("--to-json", "-j", help="Convert the parsed data to JSON and save to a file")
17 parser.add_argument("--from-json", "-J", help="Load the services data structure from a JSON file")
18 parser.add_argument("--json-string", "-s", type=str, help="JSON string to parse if --from-json is specified")
19 parser.add_argument("--to-xml", "-x", help="Convert the parsed data to XML and save to a file")
20 parser.add_argument("--from-xml", "-X", help="Load the services data structure from an XML file")
21 parser.add_argument("--xml-string", "-S", type=str, help="XML string to parse if --from-xml is specified")
22 parser.add_argument("--to-original", "-o", help="Convert the parsed data back to the original format and save to a file")
23 parser.add_argument("--line-ending", "-l", choices=["lf", "cr", "crlf"], default="lf", help="Specify the line ending format for the output file")
25 args = parser.parse_args()
27 try:
28 if args.from_json:
29 if args.json_string:
30 services = from_json(args.json_string)
31 else:
32 services = load_from_json_file(args.from_json)
33 display_services(services)
34 elif args.from_xml:
35 if args.xml_string:
36 services = from_xml(args.xml_string)
37 else:
38 services = load_from_xml_file(args.from_xml)
39 display_services(services)
40 else:
41 if args.validate_only:
42 is_valid, error_message, error_line = parse_file(args.filename, validate_only=True, verbose=args.verbose)
43 if is_valid:
44 print("The file '{0}' is valid.".format(args.filename))
45 else:
46 print("Validation Error: {0}".format(error_message))
47 print("Line: {0}".format(error_line.strip()))
48 else:
49 services = parse_file(args.filename, verbose=args.verbose)
50 if args.debug:
51 import pdb; pdb.set_trace()
52 if args.to_json:
53 save_to_json_file(services, args.to_json)
54 print("Saved JSON to {0}".format(args.to_json))
55 elif args.to_xml:
56 save_to_xml_file(services, args.to_xml)
57 print("Saved XML to {0}".format(args.to_xml))
58 elif args.to_original:
59 save_services_to_file(services, args.to_original, line_ending=args.line_ending)
60 print("Saved original format to {0}".format(args.to_original))
61 else:
62 display_services(services)
63 except Exception as e:
64 print("An error occurred: {0}".format(e), file=sys.stderr)
65 sys.exit(1)
67 if __name__ == "__main__":
68 main()