nodeslicer/infra/prusaslicer_profiles/fields.py

37 lines
1.3 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Any
import os
from functools import lru_cache
from ..json_handling import dict_from_json
from ..csv_handling import parse_csv_to_dict
# Robust path resolution relative to this file
BASE_DIR = os.path.dirname(__file__)
search_db_path: str = os.path.join(BASE_DIR, 'prusaslicer_fields.json')
search_db: dict[str, dict[str, Any]] = dict_from_json(search_db_path)
search_db_mod_path: str = os.path.join(BASE_DIR, 'prusaslicer_modifier_fields.csv')
search_db_mod: dict[str, list[str]] = parse_csv_to_dict(search_db_mod_path)
@lru_cache(maxsize=128)
def search_in_db(term: str) -> dict[str, dict[str, Any]]:
"""Searches the PrusaSlicer fields database for a given term."""
words = term.lower().split()
return {
k: v for k, v in search_db.items()
if all(word in k + " " + v.get('label', '') for word in words)
}
@lru_cache(maxsize=128)
def search_in_mod_db(term: str) -> dict[str, dict[str, Any]]:
"""Searches the PrusaSlicer modifier fields database for a given term."""
words = term.lower().split()
filtered_db = {k: v for k, v in search_db.items() if k in search_db_mod}
return {
k: v for k, v in filtered_db.items()
if all(word in k + " " + v.get('label', '') for word in words)
}