6
6
7
7
Usage:
8
8
gdformat <path>... [options]
9
+ gdformat --dump-default-config
9
10
10
11
Options:
11
12
-c --check Don't write the files back,
19
20
-s --use-spaces=<int> Use spaces for indent instead of tabs.
20
21
-h --help Show this screen.
21
22
--version Show version.
23
+ --dump-default-config Dump default config to 'gdformatrc' file.
22
24
23
25
Examples:
24
26
echo 'pass' | gdformat - # reads from STDIN
36
38
import lark
37
39
import yaml
38
40
39
- from gdtoolkit .formatter import format_code , check_formatting_safety
41
+ from gdtoolkit .formatter import format_code , check_formatting_safety , DEFAULT_CONFIG
40
42
from gdtoolkit .formatter .exceptions import (
41
43
TreeInvariantViolation ,
42
44
FormattingStabilityViolation ,
48
50
lark_unexpected_token_to_str ,
49
51
lark_unexpected_input_to_str ,
50
52
)
51
- from gdtoolkit .linter import DEFAULT_CONFIG
52
53
53
- CONFIG_FILE_NAME = "gdlintrc "
54
+ CONFIG_FILE_NAME = "gdformatrc "
54
55
55
56
56
57
def main ():
@@ -62,6 +63,9 @@ def main():
62
63
),
63
64
)
64
65
66
+ if arguments ["--dump-default-config" ]:
67
+ _dump_default_config ()
68
+
65
69
if arguments ["--diff" ]:
66
70
arguments ["--check" ] = True
67
71
@@ -76,6 +80,7 @@ def main():
76
80
config_file_path = _find_config_file ()
77
81
config = _load_config_file_or_default (config_file_path )
78
82
_log_config_entries (config )
83
+ _update_config_with_missing_entries_inplace (config )
79
84
80
85
files : List [str ] = find_gd_files_from_paths (
81
86
arguments ["<path>" ], excluded_directories = set (config ["excluded_directories" ])
@@ -91,6 +96,14 @@ def main():
91
96
_format_files (files , line_length , spaces_for_indent , safety_checks )
92
97
93
98
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
+
94
107
def _find_config_file () -> Optional [str ]:
95
108
search_dir = pathlib .Path (os .getcwd ())
96
109
config_file_path = None
@@ -114,7 +127,7 @@ def _load_config_file_or_default(config_file_path: Optional[str]) -> MappingProx
114
127
with open (config_file_path , "r" , encoding = "utf-8" ) as handle :
115
128
return yaml .load (handle .read (), Loader = yaml .Loader )
116
129
117
- logging .info ("""No 'gdlintrc ' nor '.gdlintrc ' found. Using default config...""" )
130
+ logging .info ("""No 'gdformatrc ' nor '.gdformatrc ' found. Using default config...""" )
118
131
return DEFAULT_CONFIG
119
132
120
133
@@ -124,6 +137,15 @@ def _log_config_entries(config: MappingProxyType) -> None:
124
137
logging .info (entry )
125
138
126
139
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
+
127
149
def _format_stdin (
128
150
line_length : int , spaces_for_indent : Optional [int ], safety_checks : bool
129
151
) -> None :
0 commit comments