Safer console writes

pull/1387/head
Piero Toffanin 2023-09-11 17:28:47 -04:00
rodzic ba2d42b3e5
commit df245905c5
1 zmienionych plików z 17 dodań i 12 usunięć

Wyświetl plik

@ -31,18 +31,23 @@ class Console:
def append(self, text):
if os.path.isdir(self.parent_dir):
# Write
if not os.path.isdir(self.base_dir):
os.makedirs(self.base_dir, exist_ok=True)
try:
# Write
if not os.path.isdir(self.base_dir):
os.makedirs(self.base_dir, exist_ok=True)
with open(self.file, "a") as f:
f.write(text)
except IOError:
logger.warn("Cannot append to console file: %s" % self.file)
with open(self.file, "a") as f:
f.write(text)
def reset(self, text = ""):
if os.path.isdir(self.parent_dir):
if not os.path.isdir(self.base_dir):
os.makedirs(self.base_dir, exist_ok=True)
with open(self.file, "w") as f:
f.write(text)
try:
if not os.path.isdir(self.base_dir):
os.makedirs(self.base_dir, exist_ok=True)
with open(self.file, "w") as f:
f.write(text)
except IOError:
logger.warn("Cannot reset console file: %s" % self.file)