mirror of
https://git.mirrors.martin98.com/https://github.com/Ultimaker/Cura
synced 2025-07-02 05:55:10 +08:00
Refactor and enhance formula linter in printer-linter module
The update improves formula linting by refining the identification and correction processes of formula typos. It establishes a list of standard formula names and delimiters for better match and replacement operations. Additionally, it enhances error reporting, providing users with more specific and instructive feedback. The Cura settings list retrieval method has been optimized as well. Related Task: CURA-10901
This commit is contained in:
parent
db0e46b252
commit
dcd673a605
@ -1,7 +1,8 @@
|
|||||||
import difflib
|
import difflib
|
||||||
import json
|
import json
|
||||||
import re
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
from configparser import ConfigParser
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
@ -10,23 +11,29 @@ from UM.Settings.DefinitionContainer import DefinitionContainer
|
|||||||
from UM.VersionUpgradeManager import VersionUpgradeManager
|
from UM.VersionUpgradeManager import VersionUpgradeManager
|
||||||
from cura.CuraApplication import CuraApplication
|
from cura.CuraApplication import CuraApplication
|
||||||
from cura.Settings.CuraFormulaFunctions import CuraFormulaFunctions
|
from cura.Settings.CuraFormulaFunctions import CuraFormulaFunctions
|
||||||
|
|
||||||
from ..diagnostic import Diagnostic
|
from ..diagnostic import Diagnostic
|
||||||
from ..replacement import Replacement
|
from ..replacement import Replacement
|
||||||
from .linter import Linter
|
from .linter import Linter
|
||||||
from configparser import ConfigParser
|
|
||||||
|
FORMULA_NAMES = [
|
||||||
|
"extruderValue", "extruderValues", "anyExtruderWithMaterial", "anyExtruderNrWithOrDefault",
|
||||||
|
"resolveOrValue", "defaultExtruderPosition", "valueFromContainer", "extruderValueFromContainer"
|
||||||
|
]
|
||||||
|
|
||||||
|
DELIMITERS = [r'\+', '-', '=', '/', '\*', r'\(', r'\)', r'\[', r'\]', '{', '}', ' ', '^']
|
||||||
|
|
||||||
|
|
||||||
class Formulas(Linter):
|
class Formulas(Linter):
|
||||||
""" Finds issues in definition files, such as overriding default parameters """
|
"""Finds issues in definition files, such as overriding default parameters."""
|
||||||
|
|
||||||
def __init__(self, file: Path, settings: dict) -> None:
|
def __init__(self, file: Path, settings: dict) -> None:
|
||||||
super().__init__(file, settings)
|
super().__init__(file, settings)
|
||||||
self._cura_formula_functions = CuraFormulaFunctions(self)
|
self._cura_correction_strings = FORMULA_NAMES + list(self.getCuraSettingList())
|
||||||
formula_names = ["extruderValue", "extruderValues", "anyExtruderWithMaterial", "anyExtruderNrWithOrDefault"
|
|
||||||
, "resolveOrValue", "defaultExtruderPosition", "valueFromContainer", "extruderValueFromContainer"]
|
|
||||||
self._cura_settings_list = list(self.getCuraSettingsList()) + formula_names
|
|
||||||
self._definition = {}
|
self._definition = {}
|
||||||
|
|
||||||
def getCuraSettingsList(self) -> list:
|
def getCuraSettingList(self) -> list:
|
||||||
if VersionUpgradeManager._VersionUpgradeManager__instance ==None:
|
if VersionUpgradeManager._VersionUpgradeManager__instance is None:
|
||||||
VersionUpgradeManager._VersionUpgradeManager__instance = VersionUpgradeManager(MagicMock())
|
VersionUpgradeManager._VersionUpgradeManager__instance = VersionUpgradeManager(MagicMock())
|
||||||
CuraApplication._initializeSettingDefinitions()
|
CuraApplication._initializeSettingDefinitions()
|
||||||
definition_container = DefinitionContainer("whatever")
|
definition_container = DefinitionContainer("whatever")
|
||||||
@ -133,21 +140,16 @@ class Formulas(Linter):
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _correctTyposInFormula(self, input):
|
def _correctTyposInFormula(self, formula):
|
||||||
delimiters = [r'\+', '-', '=', '/', '\*', r'\(', r'\)', r'\[', r'\]', '{','}', ' ', '^']
|
pattern = '|'.join(DELIMITERS)
|
||||||
|
tokens = re.split(pattern, formula)
|
||||||
|
|
||||||
# Create pattern
|
output = formula
|
||||||
pattern = '|'.join(delimiters)
|
|
||||||
|
|
||||||
# Split string based on pattern
|
|
||||||
tokens = re.split(pattern, input)
|
|
||||||
output = input
|
|
||||||
for token in tokens:
|
for token in tokens:
|
||||||
# If the token does not contain a parenthesis, we treat it as a word
|
|
||||||
if '(' not in token and ')' not in token:
|
if '(' not in token and ')' not in token:
|
||||||
cleaned_token = re.sub(r'[^\w\s]', '', token)
|
cleaned_token = re.sub(r'[^\w\s]', '', token)
|
||||||
matches = difflib.get_close_matches(cleaned_token, self._cura_settings_list, n=1, cutoff=0.8)
|
possible_matches = difflib.get_close_matches(cleaned_token, self._cura_correction_strings, n=1, cutoff=0.8)
|
||||||
if matches:
|
if possible_matches:
|
||||||
output = output.replace(cleaned_token, matches[0])
|
output = output.replace(cleaned_token, possible_matches[0])
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user