home · contact · privacy
Server/py: Apply pep8 style.
[plomrogue] / plomrogue-server.py
1 import errno
2 import os
3 import time
4
5
6 def setup_server_io(io_db):
7     """Fill IO files DB with proper file( path)s. Write process IO test string.
8
9     Ensure IO files directory at server/. Remove any old in file if found. Set
10     up new in file (io_db["file_in"]) for reading at io_db["path_in"], and new
11     out file (io_db["file_out"]) for writing at io_db["path_out"]. Start out
12     file with process hash line of format PID + " " + floated UNIX time
13     (io_db["teststring"]). Set worldstate file path io_db["path_worldstate"].
14     """
15     io_dir = "server/"
16     io_db["path_in"] = io_dir + "in"
17     io_db["path_out"] = io_dir + "out"
18     io_db["path_worldstate"] = io_dir + "worldstate"
19     io_db["teststring"] = str(os.getpid()) + " " + str(time.time())
20     os.makedirs(io_dir, exist_ok=True)
21     io_db["file_out"] = open(io_db["path_out"], "w")
22     io_db["file_out"].write(io_db["teststring"] + "\n")
23     if os.access(io_db["path_in"], os.F_OK):
24         os.remove(io_db["path_in"])
25     io_db["file_in"] = open(io_db["path_in"], "w")
26     io_db["file_in"].close()
27     io_db["file_in"] = open(io_db["path_in"], "r")
28
29
30 def cleanup_server_io(io_db):
31     """Close and remove all files in IO files DB."""
32     io_db["file_out"].close()
33     os.remove(io_db["path_out"])
34     io_db["file_in"].close()
35     os.remove(io_db["path_in"])
36     if "file_worldstate" in io_db:                    # This file's only set up
37         io_db["file_worldstate"].close()              # properly when the game
38     if os.access(io_db["path_worldstate"], os.F_OK):  # world is active, which
39         os.remove(io_db["path_worldstate"])           # is not guaranteed.
40
41 io_db = {}
42 try:
43     print("DUMMY: Obey command-line arguments.")
44     print("DUMMY: Open files.")
45     setup_server_io(io_db)
46     print("DUMMY: Run game.")
47 except:
48     print("SOMETHING WENT WRONG\n")
49     raise
50 finally:
51     cleanup_server_io(io_db)
52     print("DUMMY: (Clean up C heap.)")