- """Close and remove all files in IO files DB."""
- io_db["file_out"].close()
- os.remove(io_db["path_out"])
- io_db["file_in"].close()
- os.remove(io_db["path_in"])
- if "file_worldstate" in io_db: # This file is only set up
- io_db["file_worldstate"].close() # properly when the game
- if os.access(io_db["path_worldstate"], os.F_OK): # world is active, which is
- os.remove(io_db["path_worldstate"]) # not guaranteed.
+ """Close and remove all files open in IO files DB."""
+ def helper(file_key, path_key):
+ if file_key in io_db:
+ io_db[file_key].close()
+ os.remove(io_db[path_key])
+ helper("file_out", "path_out")
+ helper("file_in", "path_in")
+ helper("file_worldstate", "path_worldstate")
+
+
+def detect_atomic_leftover(path):
+ """Raise explained SystemExit if file is found at path + "_tmp"."""
+ path_tmp = path + "_tmp"
+ msg = "Found file '" + path_tmp + "' that may be a leftover from an " \
+ "aborted previous attempt to write '" + path + "'. Aborting until " \
+ "the matter is resolved by removing it from its current path."
+ if os.access(path_tmp, os.F_OK):
+ raise SystemExit(msg)
+
+
+def obey(cmd, io_db, path_recordfile):
+ """"""
+ print("Input: " + cmd)
+ try:
+ tokens = shlex.split(cmd, comments=True)
+ except ValueError as err:
+ print("Can't tokenize command string: " + str(err) + ".")
+ return
+ if 0 == len(tokens):
+ pass
+ elif "PING" == tokens[0] and 1 == len(tokens):
+ io_db["file_out"].write("PONG\n")
+ elif "QUIT" == tokens[0] and 1 == len(tokens):
+ record("# " + cmd, path_recordfile)
+ raise SystemExit("received QUIT command")
+ elif "MAKE_WORLD" == tokens[0] and 2 == len(tokens):
+ print("I would generate a new world now, if only I knew how.")
+ record(cmd, path_recordfile)
+ else:
+ print("Invalid command/argument, or bad number of tokens.")
+
+
+def record(cmd, path_recordfile):
+ """Append cmd string plus newline to file at path_recordfile."""
+ file = open(path_recordfile, "a")
+ file.write(cmd + "\n")
+ file.close()
+
+