scripts: modernize logextract.py (with opens, generators)

Signed-off-by: Maël Kerbiriou <m431kerbiriou@gmail.com>
This commit is contained in:
Maël Kerbiriou 2023-07-02 17:36:13 +02:00 committed by KevinOConnor
parent fbc5589097
commit 7e93af2574
1 changed files with 37 additions and 35 deletions

View File

@ -4,7 +4,7 @@
# Copyright (C) 2017 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2017 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import sys, re, collections, ast import sys, re, collections, ast, itertools
def format_comment(line_num, line): def format_comment(line_num, line):
return "# %6d: %s" % (line_num, line) return "# %6d: %s" % (line_num, line)
@ -40,9 +40,10 @@ class GatherConfig:
if comment is not None: if comment is not None:
self.comments.append(comment) self.comments.append(comment)
def write_file(self): def write_file(self):
f = open(self.filename, 'wt') lines = itertools.chain(self.comments, self.config_lines)
f.write('\n'.join(self.comments + self.config_lines).strip() + '\n') lines = ('%s\n' % l for l in lines)
f.close() with open(self.filename, 'wt') as f:
f.writelines(lines)
###################################################################### ######################################################################
@ -383,10 +384,9 @@ class GCodeStream:
def get_lines(self): def get_lines(self):
# Produce output gcode stream # Produce output gcode stream
if self.gcode_stream: if self.gcode_stream:
data = [ast.literal_eval(gc) for gc in self.gcode_commands] data = (ast.literal_eval(gc) for gc in self.gcode_commands)
f = open(self.gcode_filename, 'wt') with open(self.gcode_filename, 'wt') as f:
f.write(self.gcode_state + ''.join(data)) f.write(self.gcode_state + ''.join(data))
f.close()
return self.gcode_stream return self.gcode_stream
api_cmd_r = re.compile(r"^Received " + time_s + r": \{.*\}$") api_cmd_r = re.compile(r"^Received " + time_s + r": \{.*\}$")
@ -566,10 +566,10 @@ class GatherShutdown:
# Produce output sorted by timestamp # Produce output sorted by timestamp
out = [i for s in streams for i in s] out = [i for s in streams for i in s]
out.sort() out.sort()
out = [i[2] for i in out] lines = itertools.chain(self.comments, (i[2] for i in out))
f = open(self.filename, 'wt') lines = ('%s\n' % l for l in lines)
f.write('\n'.join(self.comments + out)) with open(self.filename, 'wt') as f:
f.close() f.writelines(lines)
###################################################################### ######################################################################
@ -583,7 +583,7 @@ def main():
handler = None handler = None
recent_lines = collections.deque([], 200) recent_lines = collections.deque([], 200)
# Parse log file # Parse log file
f = open(logname, 'rt') with open(logname, 'rt') as f:
for line_num, line in enumerate(f): for line_num, line in enumerate(f):
line = line.rstrip() line = line.rstrip()
line_num += 1 line_num += 1
@ -599,11 +599,13 @@ def main():
elif line.startswith('Start printer at'): elif line.startswith('Start printer at'):
last_start = format_comment(line_num, line) last_start = format_comment(line_num, line)
elif line == '===== Config file =====': elif line == '===== Config file =====':
handler = GatherConfig(configs, line_num, recent_lines, logname) handler = GatherConfig(configs, line_num,
recent_lines, logname)
handler.add_comment(last_git) handler.add_comment(last_git)
handler.add_comment(last_start) handler.add_comment(last_start)
elif 'shutdown: ' in line or line.startswith('Dumping '): elif 'shutdown: ' in line or line.startswith('Dumping '):
handler = GatherShutdown(configs, line_num, recent_lines, logname) handler = GatherShutdown(configs, line_num,
recent_lines, logname)
handler.add_comment(last_git) handler.add_comment(last_git)
handler.add_comment(last_start) handler.add_comment(last_start)
if handler is not None: if handler is not None: