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 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
# pobierz część po ostatnim myślniku
top_suffix = color_top.split('-')[-1].upper()
body_suffix = color_body.split('-')[-1].upper()
# If one suffix is missing, assume it's the same as the other.
top_suffix = top_suffix or body_suffix
body_suffix = body_suffix or top_suffix
if (top_suffix == 'G' or top_suffix == None) and body_suffix == 'G':
return "GLOSS"
elif (top_suffix == 'S' or top_suffix == None) and body_suffix == 'S':
return "SATIN"
elif top_suffix in ('G', 'S') and body_suffix == 'M':
return "MIX"
elif top_suffix in ('M', 'R', 'MAT', 'RAW') and body_suffix in ('M', 'R', 'MAT'):
return "MAT"
elif top_suffix in ('M', 'R', 'MAT', 'RAW') and body_suffix in ('G', 'S'):
return "MIX"
elif top_suffix is None and body_suffix in ('M', 'R', 'MAT'):
return "MAT"
suffix_to_category = {
'G': 'gloss',
'S': 'satin',
'M': 'mat',
'R': 'mat',
'MAT': 'mat',
'RAW': 'mat',
}
top_category = suffix_to_category.get(top_suffix)
body_category = suffix_to_category.get(body_suffix)
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):
# This will catch if row_data is not a dict or keys are missing
return None
return None
def main():