From 14c95045ba95713cd41c50f8cb0f35bb428d5237 Mon Sep 17 00:00:00 2001 From: Daniel Valentine Date: Thu, 17 Nov 2022 21:41:53 -0700 Subject: [PATCH] Wait until after generating markdown before opening output file. --- generate-instances-markdown.py | 35 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/generate-instances-markdown.py b/generate-instances-markdown.py index 6251005..7df3cb3 100755 --- a/generate-instances-markdown.py +++ b/generate-instances-markdown.py @@ -97,19 +97,6 @@ WARNING: This script will overwrite the output file if it exists. sys.stderr.write("\t" + e.__str__() + "\n") return 1 - if parsed_args.OUTPUT_FILE == "-": - out = sys.stdout - else: - try: - mode="x" - if os.path.exists(parsed_args.OUTPUT_FILE): - mode="w" - out = open(parsed_args.OUTPUT_FILE, mode) - except Exception as e: - sys.stderr.write("Error opening '{}' for writing:\n".format(parsed_args.OUTPUT_FILE)) - sys.stderr.write("\t" + e.__str__() + "\n") - return 1 - table_preamble = "|URL|Network|Version|Location|Behind Cloudflare?|Comment|\n|-|-|-|-|-|-|\n" table_rows = [] for instance in instances["instances"]: @@ -158,14 +145,24 @@ WARNING: This script will overwrite the output file if it exists. location = country table_rows.append("|{0}|{1}|{2}|{3}|{4}|{5}|\n".format( - url, - network, - version, - location, - "\u2705" if cloudflare else "", - description + url, + network, + version, + location, + "\u2705" if cloudflare else "", + description )) + if parsed_args.OUTPUT_FILE == "-": + out = sys.stdout + else: + try: + out = open(parsed_args.OUTPUT_FILE, "w" if os.path.exists(parsed_args.OUTPUT_FILE) else "x") + except Exception as e: + sys.stderr.write("Error opening '{}' for writing:\n".format(parsed_args.OUTPUT_FILE)) + sys.stderr.write("\t" + e.__str__() + "\n") + return 1 + out.write(table_preamble) for row in table_rows: out.write(row)