From 222dda50bcf6bb7819541a579f16533b643198a4 Mon Sep 17 00:00:00 2001 From: Arksine Date: Thu, 12 Nov 2020 14:37:21 -0500 Subject: [PATCH] extract_metadata: parse "gcode_start_byte" and "gcode_end_byte" Determine the byte location of the first and last gcode command (Either Mxxx or Gxxx commands). This is useful for determining the portion of the file that consists of actual commands. Signed-off-by: Eric Callahan --- scripts/extract_metadata.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/scripts/extract_metadata.py b/scripts/extract_metadata.py index 4c9f584..e1fa0c6 100644 --- a/scripts/extract_metadata.py +++ b/scripts/extract_metadata.py @@ -56,9 +56,10 @@ class BaseSlicer(object): self.path = file_path self.header_data = self.footer_data = self.log = None - def set_data(self, header_data, footer_data, log): + def set_data(self, header_data, footer_data, fsize, log): self.header_data = header_data self.footer_data = footer_data + self.size = fsize self.log = log def _parse_min_float(self, pattern, data, strict=False): @@ -78,6 +79,19 @@ class BaseSlicer(object): def check_identity(self, data): return None + def parse_gcode_start_byte(self): + m = re.search(r"\n[MG]\d+\s.*\n", self.header_data) + if m is None: + return None + return m.start() + + def parse_gcode_end_byte(self): + rev_data = self.footer_data[::-1] + m = re.search(r"\n.*\s\d+[MG]\n", rev_data) + if m is None: + return None + return self.size - m.start() + def parse_first_layer_height(self): return None @@ -518,7 +532,8 @@ SUPPORTED_SLICERS = [ SUPPORTED_DATA = [ 'first_layer_height', 'layer_height', 'object_height', 'filament_total', 'estimated_time', 'thumbnails', - 'first_layer_bed_temp', 'first_layer_extr_temp'] + 'first_layer_bed_temp', 'first_layer_extr_temp', + 'gcode_start_byte', 'gcode_end_byte'] def extract_metadata(file_path, log): metadata = {} @@ -548,7 +563,7 @@ def extract_metadata(file_path, log): footer_data = header_data[remaining - READ_SIZE:] + f.read() else: footer_data = header_data - slicer.set_data(header_data, footer_data, log) + slicer.set_data(header_data, footer_data, size, log) for key in SUPPORTED_DATA: func = getattr(slicer, "parse_" + key) result = func()