24
24
echo 'pass' | gdformat - # reads from STDIN
25
25
"""
26
26
import sys
27
+ import os
28
+ import logging
29
+ import pathlib
27
30
import difflib
28
31
from typing import List , Tuple , Optional
32
+ from types import MappingProxyType
29
33
import pkg_resources
30
34
31
35
from docopt import docopt
32
36
import lark
37
+ import yaml
33
38
34
39
from gdtoolkit .formatter import format_code , check_formatting_safety
35
40
from gdtoolkit .formatter .exceptions import (
43
48
lark_unexpected_token_to_str ,
44
49
lark_unexpected_input_to_str ,
45
50
)
51
+ from gdtoolkit .linter import DEFAULT_CONFIG
52
+
53
+ CONFIG_FILE_NAME = "gdlintrc"
46
54
47
55
48
56
def main ():
@@ -64,8 +72,13 @@ def main():
64
72
else None
65
73
)
66
74
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
+
67
80
files : List [str ] = find_gd_files_from_paths (
68
- arguments ["<path>" ], excluded_directories = set (".git" )
81
+ arguments ["<path>" ], excluded_directories = set (config [ "excluded_directories" ] )
69
82
)
70
83
71
84
if files == ["-" ]:
@@ -78,6 +91,39 @@ def main():
78
91
_format_files (files , line_length , spaces_for_indent , safety_checks )
79
92
80
93
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
+
81
127
def _format_stdin (
82
128
line_length : int , spaces_for_indent : Optional [int ], safety_checks : bool
83
129
) -> None :
0 commit comments