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