From 560e13d1c28a32e5ca8af5862d6d7da7eaa5739f Mon Sep 17 00:00:00 2001 From: Michal Zoubek Date: Mon, 28 Aug 2023 16:02:43 +0200 Subject: [PATCH] Obtaining metadata from path rewritten to function with separated regexes --- gcode_metadata/metadata.py | 64 +++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/gcode_metadata/metadata.py b/gcode_metadata/metadata.py index 1b5c7b6..4fc5590 100644 --- a/gcode_metadata/metadata.py +++ b/gcode_metadata/metadata.py @@ -99,6 +99,53 @@ def same_or_nothing(value_list): return value_list[0] +def extract_data(input_string): + """Extracts metadata from the filename + >>> extract_data("MP_PLA,PLA_MK3SMMU3_3h22m.gcode") + {'name': 'MP_', 'nozzle': None, 'height': None, 'material': 'PLA', 'printer': 'MK3SMMU3', 'time': '3h22m'} + >>> extract_data("sh_bn_0.6n_0.32mm_PETG_MK4_8h55m.gcode") + {'name': 'sh_bn_', 'nozzle': '0.6n', 'height': '0.32mm', 'material': 'PETG', 'printer': 'MK4', 'time': '8h55m'} + >>> extract_data("PLA_0.6n 0.32mm_MK3S_1d1h42m.gcode") + {'name': '', 'nozzle': '0.6n', 'height': '0.32mm', 'material': 'PLA', 'printer': 'MK3S', 'time': '1d1h42m'} + """ + printer_enum = [ + 'MK4IS', 'MK4MMU3', 'MK4', 'MK3SMMU3', 'MK3MMU3', 'MK3SMMU2S', + 'MK3MMU2', 'MK3S', 'MK3', 'MK2.5SMMU2S', 'MK2.5MMU2', 'MK2.5S', + 'MK2.5', 'MINI', 'XL5', 'XL4', 'XL3', 'XL2', 'XL', 'iX', 'SL1', + 'SHELF', 'DeltiQ 2 Plus', 'DeltiQ 2', 'AzteQ Industrial Plus', + 'AzteQ Industrial', 'AzteQ', 'EXTRACTOR', 'HARVESTER' + ] + + printer_enum.sort(key=len, reverse=True) + + material_enum = [ + 'PLA', 'PETG', 'ABS', 'ASA', 'FLEX', 'HIPS', 'EDGE', 'NGEN', 'PA', + 'PVA', 'PCTG', 'PP', 'PC', 'TPU', 'PEBA', 'CPE', 'PVB', 'PET' + ] + + patterns = [ + (r"(.*?)(?=[0-9.]+n|mm|{material_enum_pattern}|{printer_enum_pattern}|\d+[dhm]+)", + 'name'), + (r"([0-9.]+)n", 'nozzle'), + (r"([0-9.]+)mm", 'height'), + (r"(?:" + "|".join(material_enum) + r")", 'material'), + (r"(?:" + "|".join(printer_enum) + r")", 'printer'), + (r"(\d+[dhm]+(?:\d*[dhm]+)*)(?!\w)", 'time') + ] + + material_enum_pattern = "|".join(material_enum) + printer_enum_pattern = "|".join(printer_enum) + + data = {} + for pattern, key in patterns: + pattern = pattern.format(material_enum_pattern=material_enum_pattern, + printer_enum_pattern=printer_enum_pattern) + match = re.search(pattern, input_string) + data[key] = match.group() if match else None + + return data + + class MetaData: """Base MetaData class""" @@ -363,10 +410,6 @@ def set_attr(self, name, value): "normal_change_in": "normal_change_in_present" } - FDM_FILENAME_PAT = re.compile( - r"^(?P.*?)_(?P[0-9.]+)mm_(?P[A-Za-z]+)_" - r"(?P[A-Za-z0-9]+)_(?P