2023-09-11 20:35:54 +00:00
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger('app.logger')
|
|
|
|
|
|
|
|
class Console:
|
|
|
|
def __init__(self, file):
|
|
|
|
self.file = file
|
|
|
|
self.base_dir = os.path.dirname(self.file)
|
|
|
|
self.parent_dir = os.path.dirname(self.base_dir)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Console output: %s>" % self.file
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if not os.path.isfile(self.file):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(self.file, 'r') as f:
|
|
|
|
return f.read()
|
|
|
|
except IOError:
|
|
|
|
logger.warn("Cannot read console file: %s" % self.file)
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
self.append(other)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def output(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
def append(self, text):
|
|
|
|
if os.path.isdir(self.parent_dir):
|
2023-09-11 21:28:47 +00:00
|
|
|
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)
|
2023-09-11 20:35:54 +00:00
|
|
|
|
|
|
|
def reset(self, text = ""):
|
|
|
|
if os.path.isdir(self.parent_dir):
|
2023-09-11 21:28:47 +00:00
|
|
|
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)
|