Skip to content

Commit a68c57c

Browse files
committed
Have gdformat read the gdlint config file for excluded folders.
1 parent cbd7a1d commit a68c57c

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

gdtoolkit/formatter/__main__.py

+47-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424
echo 'pass' | gdformat - # reads from STDIN
2525
"""
2626
import sys
27+
import os
28+
import logging
29+
import pathlib
2730
import difflib
2831
from typing import List, Tuple, Optional
32+
from types import MappingProxyType
2933
import pkg_resources
3034

3135
from docopt import docopt
3236
import lark
37+
import yaml
3338

3439
from gdtoolkit.formatter import format_code, check_formatting_safety
3540
from gdtoolkit.formatter.exceptions import (
@@ -43,6 +48,9 @@
4348
lark_unexpected_token_to_str,
4449
lark_unexpected_input_to_str,
4550
)
51+
from gdtoolkit.linter import DEFAULT_CONFIG
52+
53+
CONFIG_FILE_NAME = "gdlintrc"
4654

4755

4856
def main():
@@ -64,8 +72,13 @@ def main():
6472
else None
6573
)
6674
safety_checks = not arguments["--fast"]
75+
76+
config_file_path = _find_config_file()
77+
config = _load_config_file_or_default(config_file_path)
78+
_log_config_entries(config)
79+
6780
files: List[str] = find_gd_files_from_paths(
68-
arguments["<path>"], excluded_directories=set(".git")
81+
arguments["<path>"], excluded_directories=set(config["excluded_directories"])
6982
)
7083

7184
if files == ["-"]:
@@ -78,6 +91,39 @@ def main():
7891
_format_files(files, line_length, spaces_for_indent, safety_checks)
7992

8093

94+
def _find_config_file() -> Optional[str]:
95+
search_dir = pathlib.Path(os.getcwd())
96+
config_file_path = None
97+
while search_dir != pathlib.Path(os.path.abspath(os.sep)):
98+
file_path = os.path.join(search_dir, CONFIG_FILE_NAME)
99+
if os.path.isfile(file_path):
100+
config_file_path = file_path
101+
break
102+
file_path = os.path.join(search_dir, ".{}".format(CONFIG_FILE_NAME))
103+
if os.path.isfile(file_path):
104+
config_file_path = file_path
105+
break
106+
search_dir = search_dir.parent
107+
return config_file_path
108+
109+
110+
def _load_config_file_or_default(config_file_path: Optional[str]) -> MappingProxyType:
111+
# TODO: error handling
112+
if config_file_path is not None:
113+
logging.info("Config file found: '%s'", config_file_path)
114+
with open(config_file_path, "r", encoding="utf-8") as handle:
115+
return yaml.load(handle.read(), Loader=yaml.Loader)
116+
117+
logging.info("""No 'gdlintrc' nor '.gdlintrc' found. Using default config...""")
118+
return DEFAULT_CONFIG
119+
120+
121+
def _log_config_entries(config: MappingProxyType) -> None:
122+
logging.info("Loaded config:")
123+
for entry in config.items():
124+
logging.info(entry)
125+
126+
81127
def _format_stdin(
82128
line_length: int, spaces_for_indent: Optional[int], safety_checks: bool
83129
) -> None:

0 commit comments

Comments
 (0)