Update readme with instruction for developing.

Change DiagnosticGenerator -> Linter since this is much easier to understand.
This commit is contained in:
Joey de l'Arago 2022-11-23 17:52:33 +01:00
parent 8e027c4af9
commit e83ed933e2
10 changed files with 39 additions and 19 deletions

View file

@ -8,4 +8,26 @@ From the Cura root folder.
```python3 printer-linter/src/terminal.py "flashforge_dreamer_nx.def.json" "flashforge_base.def.json" --fix --format```
## Developing
### Printer Linter Rules
Inside ```.printer-linter``` you can find a list of rules. These are seperated into roughly three categories.
1. Checks
1. These rules are about checking if a file meets some requirements that can't be fixed by replacing its content.
2. An example of a check is ```diagnostic-mesh-file-extension``` this checks if a mesh file extension is acceptable.
2. Format
1. These rules are purely about how a file is structured, not content.
2. An example of a format rule is ```format-definition-bracket-newline``` This rule says that when assigning a dict value the bracket should go on a new line.
3. Fixes
1. These are about the content of the file.
2. An example of a fix is ```diagnostic-definition-redundant-override``` This removes settings that have already been defined by a parent definition
### Linters
Linters find issues within a file. There are separate linters for each type of file. The linter that is used is decided by the create function in factory.py. All linters implement the abstract class Linter.
A Linter class returns an iterator of Diagnostics, each diagnostic is an issue with the file. The diagnostics can also contain suggested fixes.
### Formatters
Formatters load a file reformat it and write it to disk. There are separate formatters for each file type. All formatters implement the abstract class Formatter.
Formatters should format based on the Format rules in .printer-linter

View file

@ -1,4 +1,4 @@
from .diagnostic import Diagnostic
from .factory import create
from .factory import getLinter
__all__ = ["Diagnostic", "create"]
__all__ = ["Diagnostic", "getLinter"]

View file

@ -3,12 +3,12 @@ from typing import Optional
from .linters.profile import Profile
from .linters.defintion import Definition
from .linters.diagnostic_generator import DiagnosticGenerator
from .linters.linter import Linter
from .linters.meshes import Meshes
def create(file: Path, settings: dict) -> Optional[DiagnosticGenerator]:
""" Returns a DiagnosticGenerator depending on the file format """
def getLinter(file: Path, settings: dict) -> Optional[Linter]:
""" Returns a Linter depending on the file format """
if not file.exists():
return None
elif ".inst" in file.suffixes and ".cfg" in file.suffixes:

View file

@ -1,6 +1,6 @@
from .profile import Profile
from .meshes import Meshes
from .diagnostic_generator import DiagnosticGenerator
from .linter import Linter
from .defintion import Definition
__all__ = ["Profile", "Meshes", "DiagnosticGenerator", "Definition"]
__all__ = ["Profile", "Meshes", "Linter", "Definition"]

View file

@ -4,11 +4,11 @@ from pathlib import Path
from typing import Iterator
from ..diagnostic import Diagnostic
from .diagnostic_generator import DiagnosticGenerator
from .linter import Linter
from ..replacement import Replacement
class Definition(DiagnosticGenerator):
class Definition(Linter):
""" Finds issues in definition files, such as overriding default parameters """
def __init__(self, file: Path, settings: dict) -> None:
super().__init__(file, settings)

View file

@ -5,7 +5,7 @@ from typing import Iterator
from ..diagnostic import Diagnostic
class DiagnosticGenerator(ABC):
class Linter(ABC):
def __init__(self, file: Path, settings: dict) -> None:
""" Yields Diagnostics for file, these are issues with the file such as bad text format or too large file size.

View file

@ -2,10 +2,10 @@ from pathlib import Path
from typing import Iterator
from ..diagnostic import Diagnostic
from .diagnostic_generator import DiagnosticGenerator
from .linter import Linter
class Meshes(DiagnosticGenerator):
class Meshes(Linter):
def __init__(self, file: Path, settings: dict) -> None:
""" Finds issues in model files, such as incorrect file format or too large size """
super().__init__(file, settings)

View file

@ -1,9 +1,9 @@
from typing import Iterator
from ..diagnostic import Diagnostic
from .diagnostic_generator import DiagnosticGenerator
from .linter import Linter
class Profile(DiagnosticGenerator):
class Profile(Linter):
def check(self) -> Iterator[Diagnostic]:
yield

View file

@ -1,6 +1,5 @@
from pathlib import Path
class Replacement:
def __init__(self, file: Path, offset: int, length: int, replacement_text: str):
""" Replacement text for file between offset and offset+length.

View file

@ -65,13 +65,12 @@ def main() -> None:
def diagnoseIssuesWithFile(file: Path, settings: dict) -> List[Diagnostic]:
""" For file, runs all diagnostic checks in settings and returns a list of diagnostics """
# Find correct diagnostic generator for file
diagnostic_generator = factory.create(file, settings)
linter = factory.getLinter(file, settings)
if not diagnostic_generator:
if not linter:
return []
return list(filter(lambda d: d is not None, diagnostic_generator.check()))
return list(filter(lambda d: d is not None, linter.check()))
def applyFixesToFile(file, settings, full_body_check) -> None: