/ misc_txt_files / log_parser.py
log_parser.py
 1  import re, os, datetime
 2  
 3  MATCH_RE = re.compile(r"\[(\d{2})\:(\d{2}):(\d{2})\] \[(.*?)\]\: (.*)")
 4  START_MSGS = {"Game Start!", "[Decision Dome]: Start voting!"}
 5  END_MSGS = {"Game Over!", "[Decision Dome]: Prepare to be teleported..."}
 6  
 7  HL_FORMAT = "#"*80
 8  MSG_FORMAT = "<{m}:{s:0>2}> {message}\n"
 9  
10  filename = os.path.expanduser(input("Enter input file path: "))
11  outname = os.path.expanduser(input("Enter output file path: "))
12  
13  start_time = None
14  with open(filename) as f:
15      with open(outname, "w") as o:
16          for line in f.readlines():
17              match = MATCH_RE.match(line)
18              if not match:
19                  outline = line
20              else:
21                  time = datetime.timedelta(hours=int(match.group(1)), minutes=int(match.group(2)), seconds=int(match.group(3)))
22                  message = match.group(5)
23  
24                  if message in START_MSGS or start_time == None:
25                      start_time = time
26  
27                  time_elapsed = time - start_time
28  
29                  outline = MSG_FORMAT.format(h=time_elapsed.seconds//3600, m=(time_elapsed.seconds//60)%60, s=time_elapsed.seconds%60,
30                                              thread=match.group(4), message=message)
31                  if message in START_MSGS: outline = HL_FORMAT + f"\n Started at: {time}\n" + outline
32                  elif message in END_MSGS: outline = outline + HL_FORMAT + f"\n Ended at: {time}\n"
33  
34              print(outline, end="")
35              print(outline, end="", file=o)