Refactor get_finish_type to improve color suffix extraction and handling

This commit is contained in:
2025-10-29 21:28:52 +01:00
parent 11b0d06f50
commit 2eb1b38f25

58
main.py
View File

@@ -18,33 +18,51 @@ def normalize(text):
return "" return ""
return re.sub(r"\s+", "", text) return re.sub(r"\s+", "", text)
def get_finish_type(row_data):
try:
color_top = row_data["color_top"].strip()
color_body = row_data["color_body"].strip()
if not color_top or not color_body: def get_finish_suffix(color):
"""Extracts the finish suffix (e.g., 'G', 'S', 'M') from a color string."""
if not color:
return None
return color.strip().split('-')[-1].upper()
def get_finish_type(row_data):
"""Determines the finish type (GLOSS, SATIN, MAT, MIX) based on color suffixes."""
try:
top_suffix = get_finish_suffix(row_data.get("color_top"))
body_suffix = get_finish_suffix(row_data.get("color_body"))
if not top_suffix and not body_suffix:
return None return None
# pobierz część po ostatnim myślniku # If one suffix is missing, assume it's the same as the other.
top_suffix = color_top.split('-')[-1].upper() top_suffix = top_suffix or body_suffix
body_suffix = color_body.split('-')[-1].upper() body_suffix = body_suffix or top_suffix
if (top_suffix == 'G' or top_suffix == None) and body_suffix == 'G': suffix_to_category = {
return "GLOSS" 'G': 'gloss',
elif (top_suffix == 'S' or top_suffix == None) and body_suffix == 'S': 'S': 'satin',
return "SATIN" 'M': 'mat',
elif top_suffix in ('G', 'S') and body_suffix == 'M': 'R': 'mat',
return "MIX" 'MAT': 'mat',
elif top_suffix in ('M', 'R', 'MAT', 'RAW') and body_suffix in ('M', 'R', 'MAT'): 'RAW': 'mat',
return "MAT" }
elif top_suffix in ('M', 'R', 'MAT', 'RAW') and body_suffix in ('G', 'S'):
return "MIX" top_category = suffix_to_category.get(top_suffix)
elif top_suffix is None and body_suffix in ('M', 'R', 'MAT'): body_category = suffix_to_category.get(body_suffix)
return "MAT"
if not top_category or not body_category:
return None # Suffix not in our map
if top_category == body_category:
return top_category.upper()
if 'mat' in {top_category, body_category}:
return 'MIX'
except (KeyError, AttributeError): except (KeyError, AttributeError):
# This will catch if row_data is not a dict or keys are missing
return None return None
return None return None
def main(): def main():