55and for performing full language audits.
66"""
77
8- import json
98import sys
109from pathlib import Path
11- from typing import TextIO
1210
1311from rich .panel import Panel
1412from rich .table import Table
1513
16- from .dataclasses import RuleInfo , ComparisonResult
17- from .parsers import parse_yaml_file , diff_rules
18- from .renderer import collect_issues , console , print_warnings
14+ from .dataclasses import ComparisonResult , RuleInfo
15+ from .parsers import diff_rules , parse_yaml_file
16+ from .renderer import console , print_warnings
1917
2018# Re-export console so existing `from .auditor import console` callers keep working.
2119__all__ = ["console" ]
2220
21+ GREEN_FILE_COUNT_THRESHOLD = 7
22+ YELLOW_FILE_COUNT_THRESHOLD = 4
23+
24+
25+ def file_count_color (file_count : int ) -> str :
26+ """Map number of translated YAML files to a display color."""
27+ if file_count >= GREEN_FILE_COUNT_THRESHOLD :
28+ return "green"
29+ if file_count >= YELLOW_FILE_COUNT_THRESHOLD :
30+ return "yellow"
31+ return "red"
32+
2333
2434def split_language_into_base_and_region (language : str ) -> tuple [str , str | None ]:
2535 """Split a language code into base and optional region."""
@@ -148,8 +158,6 @@ def merge_rules(base_rules: list[RuleInfo], region_rules: list[RuleInfo]) -> lis
148158def audit_language (
149159 language : str ,
150160 specific_file : str | None = None ,
151- output_format : str = "rich" ,
152- output_path : str | None = None ,
153161 rules_dir : str | None = None ,
154162 issue_filter : set [str ] | None = None ,
155163 verbose : bool = False ,
@@ -178,15 +186,10 @@ def audit_language(
178186 # Get list of files to audit
179187 files = [specific_file ] if specific_file else get_yaml_files (english_dir , english_region_dir )
180188
181- if output_format == "rich" :
182- # Print header
183- console .print (Panel (f"MathCAT Translation Audit: { language .upper ()} " , style = "bold cyan" ))
184- console .print (f"\n [dim]Comparing against English (en) reference files[/]" )
185- console .print (f" [dim]Files to check: { len (files )} [/]" )
186-
187- out_stream : TextIO = sys .stdout
188- if output_path :
189- out_stream = open (output_path , "w" , encoding = "utf-8" , newline = "" )
189+ # Print header
190+ console .print (Panel (f"MathCAT Translation Audit: { language .upper ()} " , style = "bold cyan" ))
191+ console .print ("\n [dim]Comparing against English (en) reference files[/]" )
192+ console .print (f" [dim]Files to check: { len (files )} [/]" )
190193
191194 total_issues = 0
192195 total_missing = 0
@@ -214,52 +217,39 @@ def audit_language(
214217 str (english_region_path ) if english_region_path and english_region_path .exists () else None ,
215218 )
216219
217- if output_format == "rich" :
218- if result .has_issues :
219- issues = print_warnings (result , file_name , verbose , language )
220- if issues > 0 :
221- files_with_issues += 1
222- total_issues += issues
223- else :
224- files_ok += 1
225- else :
226- issues_list = collect_issues (result , file_name , language )
227- for issue in issues_list :
228- out_stream .write (json .dumps (issue , ensure_ascii = False ) + "\n " )
229- if issues_list :
220+ if result .has_issues :
221+ issues = print_warnings (result , file_name , verbose , language )
222+ if issues > 0 :
230223 files_with_issues += 1
231- total_issues += len ( issues_list )
232- else :
233- files_ok += 1
224+ total_issues += issues
225+ else :
226+ files_ok += 1
234227
235228 total_missing += len (result .missing_rules )
236229 total_untranslated += sum (len (entries ) for _ , entries in result .untranslated_text )
237230 total_extra += len (result .extra_rules )
238231 total_differences += len (result .rule_differences )
239232
240- if output_format == "rich" :
241- # Summary
242- table = Table (title = "SUMMARY" , title_style = "bold" , box = None , show_header = False , padding = (0 , 2 ))
243- table .add_column (width = 30 )
244- table .add_column ()
245- for label , value , color in [
246- ("Files checked" , len (files ), None ),
247- ("Files with issues" , files_with_issues , "yellow" if files_with_issues else "green" ),
248- ("Files OK" , files_ok , "green" if files_ok else None ),
249- ("Missing rules" , total_missing , "red" if total_missing else "green" ),
250- ("Untranslated text" , total_untranslated , "yellow" if total_untranslated else "green" ),
251- ("Rule differences" , total_differences , "magenta" if total_differences else "green" ),
252- ("Extra rules" , total_extra , "blue" if total_extra else None ),
253- ]:
254- table .add_row (label , f"[{ color } ]{ value } [/]" if color else str (value ))
255- console .print (Panel (table , style = "cyan" ))
256-
257- if output_path :
258- out_stream .close ()
233+ # Summary
234+ table = Table (title = "SUMMARY" , title_style = "bold" , box = None , show_header = False , padding = (0 , 2 ))
235+ table .add_column (width = 30 )
236+ table .add_column ()
237+ for label , value , color in [
238+ ("Files checked" , len (files ), None ),
239+ ("Files with issues" , files_with_issues , "yellow" if files_with_issues else "green" ),
240+ ("Files OK" , files_ok , "green" if files_ok else None ),
241+ ("Missing rules" , total_missing , "red" if total_missing else "green" ),
242+ ("Untranslated text" , total_untranslated , "yellow" if total_untranslated else "green" ),
243+ ("Rule differences" , total_differences , "magenta" if total_differences else "green" ),
244+ ("Extra rules" , total_extra , "blue" if total_extra else None ),
245+ ]:
246+ table .add_row (label , f"[{ color } ]{ value } [/]" if color else str (value ))
247+ console .print (Panel (table , style = "cyan" ))
248+
259249 return total_issues
260250
261251
262- def list_languages (rules_dir : str | None = None ):
252+ def list_languages (rules_dir : str | None = None ) -> None :
263253 """List available languages for auditing"""
264254 console .print (Panel ("Available Languages" , style = "bold cyan" ))
265255
@@ -272,15 +262,16 @@ def list_languages(rules_dir: str | None = None):
272262 if not lang_dir .is_dir () or lang_dir .name == "en" :
273263 continue
274264 base_count = len (get_yaml_files (lang_dir ))
275- color = "green" if base_count >= 7 else "yellow" if base_count >= 4 else "red"
265+ color = file_count_color ( base_count )
276266 table .add_row (lang_dir .name , f"[{ color } ]{ base_count } [/] files" )
277267
278268 for region_dir in sorted (lang_dir .iterdir ()):
279- if region_dir .is_dir ():
280- code = f"{ lang_dir .name } -{ region_dir .name } "
281- count = len (get_yaml_files (lang_dir , region_dir ))
282- region_color = "green" if count >= 7 else "yellow" if count >= 4 else "red"
283- table .add_row (code , f"[{ region_color } ]{ count } [/] files" )
269+ if not region_dir .is_dir () or region_dir .name .lower () == "sharedrules" :
270+ continue
271+ code = f"{ lang_dir .name } -{ region_dir .name } "
272+ count = len (get_yaml_files (lang_dir , region_dir ))
273+ region_color = file_count_color (count )
274+ table .add_row (code , f"[{ region_color } ]{ count } [/] files" )
284275
285276 console .print (table )
286277 console .print ("\n [dim]Reference: en (English) - base translation[/]\n " )
0 commit comments