From 023173a5148678c3a3988496d2029d5ce67a0f6b Mon Sep 17 00:00:00 2001 From: Christen Lofland Date: Thu, 7 Mar 2024 20:20:00 -0600 Subject: [PATCH] Use dedicated gdformatrc file for gdformat. --- gdtoolkit/formatter/__init__.py | 7 +++++++ gdtoolkit/formatter/__main__.py | 30 ++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/gdtoolkit/formatter/__init__.py b/gdtoolkit/formatter/__init__.py index 3de8a221..d40bb5ac 100644 --- a/gdtoolkit/formatter/__init__.py +++ b/gdtoolkit/formatter/__init__.py @@ -1,6 +1,7 @@ from typing import Optional from lark import Tree +from types import MappingProxyType from ..parser import parser from .formatter import format_code # noqa: F401 @@ -11,6 +12,12 @@ LoosenTreeTransformer, ) +DEFAULT_CONFIG = MappingProxyType( + { + "excluded_directories": {".git"}, + } +) + # pylint: disable-next=too-many-arguments def check_formatting_safety( diff --git a/gdtoolkit/formatter/__main__.py b/gdtoolkit/formatter/__main__.py index 19ed942e..6e5aca7b 100644 --- a/gdtoolkit/formatter/__main__.py +++ b/gdtoolkit/formatter/__main__.py @@ -6,6 +6,7 @@ Usage: gdformat ... [options] + gdformat --dump-default-config Options: -c --check Don't write the files back, @@ -19,6 +20,7 @@ -s --use-spaces= Use spaces for indent instead of tabs. -h --help Show this screen. --version Show version. + --dump-default-config Dump default config to 'gdformatrc' file. Examples: echo 'pass' | gdformat - # reads from STDIN @@ -36,7 +38,7 @@ import lark import yaml -from gdtoolkit.formatter import format_code, check_formatting_safety +from gdtoolkit.formatter import format_code, check_formatting_safety, DEFAULT_CONFIG from gdtoolkit.formatter.exceptions import ( TreeInvariantViolation, FormattingStabilityViolation, @@ -48,9 +50,8 @@ lark_unexpected_token_to_str, lark_unexpected_input_to_str, ) -from gdtoolkit.linter import DEFAULT_CONFIG -CONFIG_FILE_NAME = "gdlintrc" +CONFIG_FILE_NAME = "gdformatrc" def main(): @@ -62,6 +63,9 @@ def main(): ), ) + if arguments["--dump-default-config"]: + _dump_default_config() + if arguments["--diff"]: arguments["--check"] = True @@ -76,6 +80,7 @@ def main(): config_file_path = _find_config_file() config = _load_config_file_or_default(config_file_path) _log_config_entries(config) + _update_config_with_missing_entries_inplace(config) files: List[str] = find_gd_files_from_paths( arguments[""], excluded_directories=set(config["excluded_directories"]) @@ -91,6 +96,14 @@ def main(): _format_files(files, line_length, spaces_for_indent, safety_checks) +def _dump_default_config() -> None: + # TODO: error handling + assert not os.path.isfile(CONFIG_FILE_NAME) + with open(CONFIG_FILE_NAME, "w", encoding="utf-8") as handle: + handle.write(yaml.dump(DEFAULT_CONFIG.copy())) + sys.exit(0) + + def _find_config_file() -> Optional[str]: search_dir = pathlib.Path(os.getcwd()) config_file_path = None @@ -114,7 +127,7 @@ def _load_config_file_or_default(config_file_path: Optional[str]) -> MappingProx with open(config_file_path, "r", encoding="utf-8") as handle: return yaml.load(handle.read(), Loader=yaml.Loader) - logging.info("""No 'gdlintrc' nor '.gdlintrc' found. Using default config...""") + logging.info("""No 'gdformatrc' nor '.gdformatrc' found. Using default config...""") return DEFAULT_CONFIG @@ -124,6 +137,15 @@ def _log_config_entries(config: MappingProxyType) -> None: logging.info(entry) +def _update_config_with_missing_entries_inplace(config: dict) -> None: + for key in DEFAULT_CONFIG: + if key not in config: + logging.info( + "Adding missing entry from defaults: %s", (key, DEFAULT_CONFIG[key]) + ) + config[key] = DEFAULT_CONFIG[key] + + def _format_stdin( line_length: int, spaces_for_indent: Optional[int], safety_checks: bool ) -> None: