-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotolua_out_utils.py
More file actions
39 lines (33 loc) · 1.1 KB
/
protolua_out_utils.py
File metadata and controls
39 lines (33 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Handles all protolua sim stdout utilities from `lua_template/protolua/out_utils.lua`
import re
import os
import sys
import typing
class ProtoLuaSimOutUtils:
def __init__(self):
self.fileAppend = None
self.files: "dict[str,typing.IO]" = {}
def __del__(self):
for f in self.files.values():
f.close()
def handle(self, line: str) -> bool:
line = re.sub(r"\[.*?\] \w+: ", "", line)
if self._handle_fileAppend(line): return True
return False
def _handle_fileAppend(self, line: str) -> bool:
if line.startswith("__PROTOLUA__:fileAppend:"):
self.state = "fileAppend"
split = line.split(":", 3)
name = split[2]
data = split[3] # We keep the newline here.
file_path = os.path.join("sim", name)
if file_path.find("..") >= 0:
print(f"Bad fileAppend name \"{name}\"", file=sys.stderr)
return True
if name not in self.files:
os.makedirs(os.path.dirname(file_path), exist_ok=True)
self.files[name] = open(file_path, "w")
self.files[name].write(data)
self.files[name].flush() # Sim can take time, it can be nice to see the output live.
return True
return False