Add files via upload
[LoveSoStrong.git] / display_message_file.py
blob898d22146d133c1e416e5a89e2ed2aab08952807
1 import argparse
2 import sys
3 from parse_message_file import parse_file, display_services, to_json, from_json, load_from_json_file, save_to_json_file
5 def main():
6 parser = argparse.ArgumentParser(description="Parse and display message file content.")
7 parser.add_argument("filename", help="Path to the file to be parsed")
8 parser.add_argument("--validate-only", "-v", action="store_true", help="Only validate the file without displaying")
9 parser.add_argument("--verbose", "-V", action="store_true", help="Enable verbose mode")
10 parser.add_argument("--debug", "-d", action="store_true", help="Enable debug mode")
11 parser.add_argument("--to-json", "-j", help="Convert the parsed data to JSON and save to a file")
12 parser.add_argument("--from-json", "-J", help="Load the services data structure from a JSON file")
13 parser.add_argument("--json-string", "-s", type=str, help="JSON string to parse if --from-json is specified")
15 args = parser.parse_args()
17 try:
18 if args.from_json:
19 if args.json_string:
20 services = from_json(args.json_string)
21 else:
22 services = load_from_json_file(args.from_json)
23 display_services(services)
24 else:
25 if args.validate_only:
26 is_valid, error_message, error_line = parse_file(args.filename, validate_only=True, verbose=args.verbose)
27 if is_valid:
28 print(f"The file '{args.filename}' is valid.")
29 else:
30 print(f"Validation Error: {error_message}")
31 print(f"Line: {error_line.strip()}")
32 else:
33 services = parse_file(args.filename, verbose=args.verbose)
34 if args.debug:
35 import pdb; pdb.set_trace()
36 if args.to_json:
37 save_to_json_file(services, args.to_json)
38 print(f"Saved JSON to {args.to_json}")
39 else:
40 display_services(services)
41 except Exception as e:
42 print(f"An error occurred: {e}", file=sys.stderr)
43 sys.exit(1)
45 if __name__ == "__main__":
46 main()