mirror of
https://git.mirrors.martin98.com/https://github.com/Ultimaker/Cura
synced 2025-06-30 10:35:10 +08:00
Add separate package with file formatters.
This commit is contained in:
parent
ad436f37f8
commit
b6ac85d251
4
printer-linter/src/printerlinter/formatters/__init__.py
Normal file
4
printer-linter/src/printerlinter/formatters/__init__.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from def_json_formatter import DefJsonFormatter
|
||||||
|
from inst_cfg_formatter import InstCfgFormatter
|
||||||
|
|
||||||
|
__all__ = ["DefJsonFormatter", "InstCfgFormatter"]
|
@ -0,0 +1,29 @@
|
|||||||
|
import json
|
||||||
|
import re
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from .formatter import FileFormatter
|
||||||
|
|
||||||
|
class DefJsonFormatter(FileFormatter):
|
||||||
|
def format(self, file: Path):
|
||||||
|
""" Format .def.json files according to the rules in settings """
|
||||||
|
definition = json.loads(file.read_text())
|
||||||
|
content = json.dumps(definition, indent=self._settings["format"].get("format-definition-indent", 4),
|
||||||
|
sort_keys=self._settings["format"].get("format-definition-sort-keys", True))
|
||||||
|
|
||||||
|
if self._settings["format"].get("format-definition-bracket-newline", True):
|
||||||
|
newline = re.compile(r"(\B\s+)(\"[\w\"]+)(\:\s\{)")
|
||||||
|
content = newline.sub(r"\1\2:\1{", content)
|
||||||
|
|
||||||
|
if self._settings["format"].get("format-definition-single-value-single-line", True):
|
||||||
|
single_value_dict = re.compile(r"(:)(\s*\n?.*\{\s+)(\".*)(\d*\s*\})(\s*)(,?)")
|
||||||
|
content = single_value_dict.sub(r"\1 { \3 }\6", content)
|
||||||
|
|
||||||
|
single_value_list = re.compile(r"(:)(\s*\n?.*\[\s+)(\".*)(\d*\s*\])(\s*)(,?)")
|
||||||
|
content = single_value_list.sub(r"\1 [ \3 ]\6", content)
|
||||||
|
|
||||||
|
if self._settings["format"].get("format-definition-paired-coordinate-array", True):
|
||||||
|
paired_coordinates = re.compile(r"(\[)\s+(-?\d*),\s*(-?\d*)\s*(\])")
|
||||||
|
content = paired_coordinates.sub(r"\1 \2, \3 \4", content)
|
||||||
|
|
||||||
|
file.write_text(content)
|
16
printer-linter/src/printerlinter/formatters/formatter.py
Normal file
16
printer-linter/src/printerlinter/formatters/formatter.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
class FileFormatter(ABC):
|
||||||
|
def __init__(self, settings: dict) -> None:
|
||||||
|
""" Yields Diagnostics for file, these are issues with the file such as bad text format or too large file size.
|
||||||
|
|
||||||
|
@param file: A file to generate diagnostics for
|
||||||
|
@param settings: A list of settings containing rules for creating diagnostics
|
||||||
|
"""
|
||||||
|
self._settings = settings
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def format(self, file: Path) -> None:
|
||||||
|
pass
|
@ -0,0 +1,21 @@
|
|||||||
|
import configparser
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
from collections import OrderedDict
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from .formatter import FileFormatter
|
||||||
|
|
||||||
|
class InstCfgFormatter(FileFormatter):
|
||||||
|
def format(self, file: Path):
|
||||||
|
""" Format .inst.cfg files according to the rules in settings """
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(file)
|
||||||
|
|
||||||
|
if self._settings["format"].get("format-profile-sort-keys", True):
|
||||||
|
for section in config._sections:
|
||||||
|
config._sections[section] = OrderedDict(sorted(config._sections[section].items(), key=lambda t: t[0]))
|
||||||
|
config._sections = OrderedDict(sorted(config._sections.items(), key=lambda t: t[0]))
|
||||||
|
|
||||||
|
with open(file, "w") as f:
|
||||||
|
config.write(f, space_around_delimiters=self._settings["format"].get("format-profile-space-around-delimiters", True))
|
Loading…
x
Reference in New Issue
Block a user