Skip to content

Commit

Permalink
Obtaining metadata from path rewritten to function with separated reg…
Browse files Browse the repository at this point in the history
…exes
  • Loading branch information
Michal Zoubek committed Aug 29, 2023
1 parent 8215f0b commit bcb843d
Showing 1 changed file with 50 additions and 14 deletions.
64 changes: 50 additions & 14 deletions gcode_metadata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("M P 0.4n_0.2mm_PLA,PLA_MK3SMMU3_3h22m.gcode")
{'name': 'M P ', 'nozzle': '0.4n', 'height': '0.2mm', '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"""

Expand Down Expand Up @@ -363,10 +410,6 @@ def set_attr(self, name, value):
"normal_change_in": "normal_change_in_present"
}

FDM_FILENAME_PAT = re.compile(
r"^(?P<name>.*?)_(?P<height>[0-9.]+)mm_(?P<material>[A-Za-z]+)_"
r"(?P<printer>[A-Za-z0-9]+)_(?P<time>[A-Za-z0-9]+)_?\w*.")

METADATA_START_OFFSET = 400000 # Read 400KB from the start
METADATA_END_OFFSET = 40000 # Read 40KB at the end of the file
# Number of times the search for M73 is going to repeat if info
Expand All @@ -390,16 +433,9 @@ def __init__(self, path: str):
def load_from_path(self, path):
"""Try to obtain any usable metadata from the path itself"""
filename = os.path.basename(path)
match = self.FDM_FILENAME_PAT.match(filename)
if match:
data = {
"name": match.group("name"),
"layer_height": match.group("height"),
"filament_type": match.group("material"),
"printer_model": match.group("printer"),
"estimated printing time (normal mode)": match.group("time"),
}
self.set_data(data)

data = extract_data(filename)
self.set_data(data)

def from_comment_line(self, line):
"""Parses data from a line in the comments"""
Expand Down

0 comments on commit bcb843d

Please sign in to comment.