11import os
2- from collections .abc import Generator
2+ from collections .abc import Iterable
3+ from typing import Iterator
34
45import click
56import fsspec
@@ -39,7 +40,7 @@ def __init__(
3940 rule_specs : tuple [str , ...] = (),
4041 output_format : str = DEFAULT_OUTPUT_FORMAT ,
4142 output_path : str | None = None ,
42- color_enabled : bool = True ,
43+ styled : bool = True ,
4344 files : tuple [str , ...] = (),
4445 ):
4546 self .no_default_config = no_default_config
@@ -48,7 +49,7 @@ def __init__(
4849 self .rule_specs = rule_specs
4950 self .output_format = output_format
5051 self .output_path = output_path
51- self .color_enabled = color_enabled
52+ self .styled = styled
5253 self .files = files
5354
5455 def load_config (self ) -> ConfigList :
@@ -74,6 +75,7 @@ def load_config(self) -> ConfigList:
7475 for config_path in DEFAULT_CONFIG_FILES :
7576 try :
7677 config_list = read_config_list (config_path )
78+ break
7779 except FileNotFoundError :
7880 pass
7981 except ConfigError as e :
@@ -92,16 +94,15 @@ def load_config(self) -> ConfigList:
9294
9395 return ConfigList .from_value (configs )
9496
95- def verify_datasets (self , config_list : ConfigList ) -> list [Result ]:
97+ def verify_datasets (self , config_list : ConfigList ) -> Iterator [Result ]:
9698 global_filter = config_list .get_global_filter (default = DEFAULT_GLOBAL_FILTER )
97- results : list [ Result ] = []
99+ linter = Linter ()
98100 for file_path , is_dir in get_files (self .files , global_filter ):
99101 config = config_list .compute_config (file_path )
100102 if config is not None :
101- linter = Linter (config = config )
102- result = linter .verify_dataset (file_path )
103+ yield linter .verify_dataset (file_path , config = config )
103104 else :
104- result = Result .new (
105+ yield Result .new (
105106 config = config ,
106107 file_path = file_path ,
107108 messages = [
@@ -111,11 +112,8 @@ def verify_datasets(self, config_list: ConfigList) -> list[Result]:
111112 )
112113 ],
113114 )
114- results .append (result )
115115
116- return results
117-
118- def format_results (self , results : list [Result ]) -> str :
116+ def format_results (self , results : Iterable [Result ]) -> str :
119117 output_format = (
120118 self .output_format if self .output_format else DEFAULT_OUTPUT_FORMAT
121119 )
@@ -130,19 +128,23 @@ def format_results(self, results: list[Result]) -> str:
130128 # TODO: pass and validate format-specific args/kwargs
131129 # against formatter.meta.schema
132130 if output_format == "simple" :
133- formatter_kwargs = {"color_enabled" : self .color_enabled }
131+ formatter_kwargs = {
132+ "styled" : self .styled and self .output_path is None ,
133+ "output" : self .output_path is None ,
134+ }
134135 else :
135136 formatter_kwargs = {}
136137 # noinspection PyArgumentList
137138 formatter_op = formatter .op_class (** formatter_kwargs )
138139 return formatter_op .format (FormatterContext (False ), results )
139140
140141 def write_report (self , report : str ):
141- if not self .output_path :
142- print (report )
143- else :
142+ if self .output_path :
144143 with fsspec .open (self .output_path , mode = "w" ) as f :
145144 f .write (report )
145+ elif self .output_format != "simple" :
146+ # The simple formatters outputs incrementally to console
147+ print (report )
146148
147149 @classmethod
148150 def init_config_file (cls ):
@@ -156,7 +158,7 @@ def init_config_file(cls):
156158
157159def get_files (
158160 file_paths : tuple [str , ...], global_filter : FileFilter
159- ) -> Generator [tuple [str , bool | None ]]:
161+ ) -> Iterator [tuple [str , bool | None ]]:
160162 for file_path in file_paths :
161163 _fs , root = fsspec .url_to_fs (file_path )
162164 fs : fsspec .AbstractFileSystem = _fs
0 commit comments