Skip to content
Snippets Groups Projects
Commit a4b3759b authored by mads's avatar mads
Browse files

divided into loginterpreter and logfile

parent 97a9b23c
No related branches found
No related tags found
1 merge request!5Clustertools
Pipeline #
...@@ -23,35 +23,13 @@ INITIALIZATION = 'Initializing simulation' ...@@ -23,35 +23,13 @@ INITIALIZATION = 'Initializing simulation'
SIMULATING = "Simulating" SIMULATING = "Simulating"
DONE = "Simulation succeded" DONE = "Simulation succeded"
#def is_file_open(filename): class LogInterpreter(object):
# try: def __init__(self, time_stop):
# os.rename(filename, filename + "_")
# os.rename(filename + "_", filename)
# return False
# except OSError as e:
# if "The process cannot access the file because it is being used by another process" not in str(e):
# raise
#
# if os.path.isfile(filename + "_"):
# os.remove(filename + "_")
# return True
class LogFile(object):
def __init__(self, log_filename, time_stop):
self.filename = log_filename
self.time_stop = time_stop self.time_stop = time_stop
self.hawc2version = "Unknown" self.hawc2version = "Unknown"
self.reset() self.reset()
self.update_status() self.update_status()
@staticmethod
def from_htcfile(htcfile, modelpath):
logfilename = htcfile.simulation.logfile[0]
if not os.path.isabs(logfilename):
logfilename = os.path.join(modelpath, logfilename)
return LogFile(logfilename, htcfile.simulation.time_stop[0])
def reset(self): def reset(self):
self.position = 0 self.position = 0
self.lastline = "" self.lastline = ""
...@@ -66,12 +44,8 @@ class LogFile(object): ...@@ -66,12 +44,8 @@ class LogFile(object):
def __str__(self): def __str__(self):
return self.txt return self.txt
def clear(self): def clear(self):
# exist_ok does not exist in Python27
if not os.path.exists(os.path.dirname(self.filename)):
os.makedirs(os.path.dirname(self.filename))#, exist_ok=True)
with open(self.filename, 'w', encoding='utf-8'):
pass
self.reset() self.reset()
def extract_time(self, txt): def extract_time(self, txt):
...@@ -89,17 +63,13 @@ class LogFile(object): ...@@ -89,17 +63,13 @@ class LogFile(object):
print ("Cannot extract time from #" + time_line + "#") print ("Cannot extract time from #" + time_line + "#")
pass pass
def update_status(self): def update_status(self, new_lines=""):
if not os.path.isfile(self.filename): if self.txt == "" and new_lines == "":
self.status = MISSING self.status = MISSING
else: else:
if self.status == UNKNOWN or self.status == MISSING: if self.status == UNKNOWN or self.status == MISSING:
self.status = PENDING self.status = PENDING
with open(self.filename, 'rb') as fid: txt = new_lines
fid.seek(self.position)
txt = fid.read()
self.position += len(txt)
txt = txt.decode(encoding='utf_8', errors='strict')
self.txt += txt self.txt += txt
if self.status == PENDING and self.position > 0: if self.status == PENDING and self.position > 0:
self.status = INITIALIZATION self.status = INITIALIZATION
...@@ -162,6 +132,42 @@ class LogFile(object): ...@@ -162,6 +132,42 @@ class LogFile(object):
return "--:--" return "--:--"
class LogFile(LogInterpreter):
def __init__(self, log_filename, time_stop):
self.filename = log_filename
LogInterpreter.__init__(self, time_stop)
@staticmethod
def from_htcfile(htcfile, modelpath):
logfilename = htcfile.simulation.logfile[0]
if not os.path.isabs(logfilename):
logfilename = os.path.join(modelpath, logfilename)
return LogFile(logfilename, htcfile.simulation.time_stop[0])
def clear(self):
# exist_ok does not exist in Python27
if not os.path.exists(os.path.dirname(self.filename)):
os.makedirs(os.path.dirname(self.filename)) #, exist_ok=True)
with open(self.filename, 'w', encoding='utf-8'):
pass
LogInterpreter.clear(self)
def update_status(self):
if not os.path.isfile(self.filename):
self.status = MISSING
else:
if self.status == UNKNOWN or self.status == MISSING:
self.status = PENDING
s = self.status
with open(self.filename, 'rb') as fid:
fid.seek(self.position)
txt = fid.read()
self.position += len(txt)
txt = txt.decode(encoding='utf_8', errors='strict')
if txt != "":
LogInterpreter.update_status(self, txt)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment