Skip to content

Commit e0047f4

Browse files
committed
Use dedicated gdformatrc file for gdformat.
1 parent ab3fed7 commit e0047f4

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

Diff for: gdtoolkit/formatter/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Optional
22

3+
from types import MappingProxyType
34
from lark import Tree
45

56
from ..parser import parser
@@ -11,6 +12,12 @@
1112
LoosenTreeTransformer,
1213
)
1314

15+
DEFAULT_CONFIG = MappingProxyType(
16+
{
17+
"excluded_directories": {".git"},
18+
}
19+
)
20+
1421

1522
# pylint: disable-next=too-many-arguments
1623
def check_formatting_safety(

Diff for: gdtoolkit/formatter/__main__.py

+26-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
Usage:
88
gdformat <path>... [options]
9+
gdformat --dump-default-config
910
1011
Options:
1112
-c --check Don't write the files back,
@@ -19,6 +20,7 @@
1920
-s --use-spaces=<int> Use spaces for indent instead of tabs.
2021
-h --help Show this screen.
2122
--version Show version.
23+
--dump-default-config Dump default config to 'gdformatrc' file.
2224
2325
Examples:
2426
echo 'pass' | gdformat - # reads from STDIN
@@ -36,7 +38,7 @@
3638
import lark
3739
import yaml
3840

39-
from gdtoolkit.formatter import format_code, check_formatting_safety
41+
from gdtoolkit.formatter import format_code, check_formatting_safety, DEFAULT_CONFIG
4042
from gdtoolkit.formatter.exceptions import (
4143
TreeInvariantViolation,
4244
FormattingStabilityViolation,
@@ -48,9 +50,8 @@
4850
lark_unexpected_token_to_str,
4951
lark_unexpected_input_to_str,
5052
)
51-
from gdtoolkit.linter import DEFAULT_CONFIG
5253

53-
CONFIG_FILE_NAME = "gdlintrc"
54+
CONFIG_FILE_NAME = "gdformatrc"
5455

5556

5657
def main():
@@ -62,6 +63,9 @@ def main():
6263
),
6364
)
6465

66+
if arguments["--dump-default-config"]:
67+
_dump_default_config()
68+
6569
if arguments["--diff"]:
6670
arguments["--check"] = True
6771

@@ -76,6 +80,7 @@ def main():
7680
config_file_path = _find_config_file()
7781
config = _load_config_file_or_default(config_file_path)
7882
_log_config_entries(config)
83+
_update_config_with_missing_entries_inplace(config)
7984

8085
files: List[str] = find_gd_files_from_paths(
8186
arguments["<path>"], excluded_directories=set(config["excluded_directories"])
@@ -91,6 +96,14 @@ def main():
9196
_format_files(files, line_length, spaces_for_indent, safety_checks)
9297

9398

99+
def _dump_default_config() -> None:
100+
# TODO: error handling
101+
assert not os.path.isfile(CONFIG_FILE_NAME)
102+
with open(CONFIG_FILE_NAME, "w", encoding="utf-8") as handle:
103+
handle.write(yaml.dump(DEFAULT_CONFIG.copy()))
104+
sys.exit(0)
105+
106+
94107
def _find_config_file() -> Optional[str]:
95108
search_dir = pathlib.Path(os.getcwd())
96109
config_file_path = None
@@ -114,7 +127,7 @@ def _load_config_file_or_default(config_file_path: Optional[str]) -> MappingProx
114127
with open(config_file_path, "r", encoding="utf-8") as handle:
115128
return yaml.load(handle.read(), Loader=yaml.Loader)
116129

117-
logging.info("""No 'gdlintrc' nor '.gdlintrc' found. Using default config...""")
130+
logging.info("""No 'gdformatrc' nor '.gdformatrc' found. Using default config...""")
118131
return DEFAULT_CONFIG
119132

120133

@@ -124,6 +137,15 @@ def _log_config_entries(config: MappingProxyType) -> None:
124137
logging.info(entry)
125138

126139

140+
def _update_config_with_missing_entries_inplace(config: dict) -> None:
141+
for key in DEFAULT_CONFIG:
142+
if key not in config:
143+
logging.info(
144+
"Adding missing entry from defaults: %s", (key, DEFAULT_CONFIG[key])
145+
)
146+
config[key] = DEFAULT_CONFIG[key]
147+
148+
127149
def _format_stdin(
128150
line_length: int, spaces_for_indent: Optional[int], safety_checks: bool
129151
) -> None:

0 commit comments

Comments
 (0)