mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-13 01:37:51 -06:00
Flatten main logic.
Parse files from directories to remove checking if a file is a directory in many places.
This commit is contained in:
parent
ad7e719146
commit
f74a40bed0
1 changed files with 22 additions and 21 deletions
|
@ -5,6 +5,7 @@ from argparse import ArgumentParser
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from os import getcwd
|
from os import getcwd
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import List
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
@ -74,6 +75,18 @@ def formatFile(file: Path, settings) -> None:
|
||||||
config.write(f, space_around_delimiters=settings["format"].get("format-profile-space-around-delimiters", True))
|
config.write(f, space_around_delimiters=settings["format"].get("format-profile-space-around-delimiters", True))
|
||||||
|
|
||||||
|
|
||||||
|
def extract_file_paths(paths: List[Path]) -> List[Path]:
|
||||||
|
""" Takes list of files and directories, returns the files as well as all files within directories as a List """
|
||||||
|
file_paths = []
|
||||||
|
for path in paths:
|
||||||
|
if path.is_dir():
|
||||||
|
file_paths.extend(path.rglob("**/*"))
|
||||||
|
else:
|
||||||
|
file_paths.append(path)
|
||||||
|
|
||||||
|
return file_paths
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
parser = ArgumentParser(
|
parser = ArgumentParser(
|
||||||
description="UltiMaker Cura printer linting, static analysis and formatting of Cura printer definitions and other resources")
|
description="UltiMaker Cura printer linting, static analysis and formatting of Cura printer definitions and other resources")
|
||||||
|
@ -85,7 +98,7 @@ def main() -> None:
|
||||||
parser.add_argument("Files", metavar="F", type=Path, nargs="+", help="Files or directories to format")
|
parser.add_argument("Files", metavar="F", type=Path, nargs="+", help="Files or directories to format")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
files = args.Files
|
files = extract_file_paths(args.Files)
|
||||||
setting_path = args.setting
|
setting_path = args.setting
|
||||||
to_format = args.format
|
to_format = args.format
|
||||||
to_fix = args.fix
|
to_fix = args.fix
|
||||||
|
@ -102,14 +115,11 @@ def main() -> None:
|
||||||
with open(setting_path, "r") as f:
|
with open(setting_path, "r") as f:
|
||||||
settings = yaml.load(f, yaml.FullLoader)
|
settings = yaml.load(f, yaml.FullLoader)
|
||||||
|
|
||||||
|
full_body_check = {"Diagnostics": []}
|
||||||
|
|
||||||
if to_fix or to_diagnose:
|
if to_fix or to_diagnose:
|
||||||
full_body_check = {"Diagnostics": []}
|
|
||||||
for file in files:
|
for file in files:
|
||||||
if file.is_dir():
|
examineFile(file, settings, full_body_check)
|
||||||
for fp in file.rglob("**/*"):
|
|
||||||
examineFile(fp, settings, full_body_check)
|
|
||||||
else:
|
|
||||||
examineFile(file, settings, full_body_check)
|
|
||||||
|
|
||||||
results = yaml.dump(full_body_check, default_flow_style=False, indent=4, width=240)
|
results = yaml.dump(full_body_check, default_flow_style=False, indent=4, width=240)
|
||||||
if report:
|
if report:
|
||||||
|
@ -117,23 +127,14 @@ def main() -> None:
|
||||||
else:
|
else:
|
||||||
print(results)
|
print(results)
|
||||||
|
|
||||||
if to_fix:
|
if to_fix:
|
||||||
for file in files:
|
for file in files:
|
||||||
if file.is_dir():
|
if f"{file.as_posix()}" in full_body_check:
|
||||||
for fp in file.rglob("**/*"):
|
fixFile(file, settings, full_body_check)
|
||||||
if f"{file.as_posix()}" in full_body_check:
|
|
||||||
fixFile(fp, settings, full_body_check)
|
|
||||||
else:
|
|
||||||
if f"{file.as_posix()}" in full_body_check:
|
|
||||||
fixFile(file, settings, full_body_check)
|
|
||||||
|
|
||||||
if to_format:
|
if to_format:
|
||||||
for file in files:
|
for file in files:
|
||||||
if file.is_dir():
|
formatFile(file, settings)
|
||||||
for fp in file.rglob("**/*"):
|
|
||||||
formatFile(fp, settings)
|
|
||||||
else:
|
|
||||||
formatFile(file, settings)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue