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 30, 2023
1 parent 8215f0b commit 2c7e2b4
Showing 1 changed file with 59 additions and 14 deletions.
73 changes: 59 additions & 14 deletions gcode_metadata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@
r"((?P<minutes>[0-9]+)m\s*)?"
r"((?P<seconds>[0-9]+)s)?")

PRINTERS = [
'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', 'EXTRACTOR', 'HARVESTER'
]

PRINTERS.sort(key=len, reverse=True)

MATERIALS = [
'PLA', 'PETG', 'ABS', 'ASA', 'FLEX', 'HIPS', 'EDGE', 'NGEN', 'PA',
'PVA', 'PCTG', 'PP', 'PC', 'TPU', 'PEBA', 'CPE', 'PVB', 'PET'
]


class UnknownGcodeFileType(ValueError):
# pylint: disable=missing-class-docstring
Expand Down Expand Up @@ -99,6 +113,39 @@ def same_or_nothing(value_list):
return value_list[0]


def extract_data(input_string):
"""Extracts metadata from the filename
>>> extract_data("Paladin_PLA,PLA_MK3SMMU3_3h22m.gcode")['printer']
'MK3SMMU3'
>>> extract_data("sh_bn_0.6n_0.32mm_PETG_MK4_8h55m.gcode")['material']
'PETG'
>>> extract_data("PLA_0.6n 0.32mm_MK3S_1d1h42m")['printer']
'MK3S'
>>> extract_data("42.gcode")['printer'] is None
True
>>> extract_data("Tisk tohoto souboru bude trvat 1d18h15m")['time']
'1d18h15m'
"""

patterns = [
(r"(.*?)(?=[0-9.]+n|mm|{mat_pat}|{prt_pat}|\d+[dhm]+)", 'name'),
(r"([0-9.]+)n", 'nozzle'),
(r"([0-9.]+)mm", 'height'),
(r"(?:" + "|".join(MATERIALS) + r")", 'material'),
(r"(?:" + "|".join(PRINTERS) + r")", 'printer'),
(r"(\d+[dhm]+(?:\d*[dhm]+)*)(?!\w)", 'time')
]

data = {}
for pattern, key in patterns:
pattern = pattern.format(mat_pat="|".join(MATERIALS),
prt_pat="|".join(PRINTERS))
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,18 @@ 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)

result = {
"name": data["name"],
"nozzle_diameter": data["nozzle"],
"layer_height": data["height"],
"filament_type": data["material"],
"printer_model": data["printer"],
"estimated printing time (normal mode)": data["time"]
}

self.set_data(result)

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

0 comments on commit 2c7e2b4

Please sign in to comment.