10
10
from collections import OrderedDict
11
11
from typing import Any , Dict , List , Optional , TextIO , Tuple , Union
12
12
13
- # Import hardcoded version information from version.py
14
- root_directory = os . path . join ( os . path . dirname ( os . path . abspath ( __file__ )), "../../" )
15
- sys . path . append ( root_directory ) # Include the root directory
16
- import version # noqa: E402
13
+ sys . path . insert ( 0 , root_directory := os . path . join ( os . path . dirname ( os . path . abspath ( __file__ )), "../../" ))
14
+
15
+ import version
16
+ from methods import Ansi , toggle_color
17
17
18
18
# $DOCS_URL/path/to/page.html(#fragment-tag)
19
19
GODOT_DOCS_PATTERN = re .compile (r"^\$DOCS_URL/(.*)\.html(#.*)?$" )
90
90
]
91
91
strings_l10n : Dict [str , str ] = {}
92
92
93
- STYLES : Dict [str , str ] = {}
94
-
95
93
CLASS_GROUPS : Dict [str , str ] = {
96
94
"global" : "Globals" ,
97
95
"node" : "Nodes" ,
@@ -699,31 +697,7 @@ def main() -> None:
699
697
)
700
698
args = parser .parse_args ()
701
699
702
- should_color = bool (args .color or sys .stdout .isatty () or os .environ .get ("CI" ))
703
-
704
- # Enable ANSI escape code support on Windows 10 and later (for colored console output).
705
- # <https://github.com/python/cpython/issues/73245>
706
- if should_color and sys .stdout .isatty () and sys .platform == "win32" :
707
- try :
708
- from ctypes import WinError , byref , windll # type: ignore
709
- from ctypes .wintypes import DWORD # type: ignore
710
-
711
- stdout_handle = windll .kernel32 .GetStdHandle (DWORD (- 11 ))
712
- mode = DWORD (0 )
713
- if not windll .kernel32 .GetConsoleMode (stdout_handle , byref (mode )):
714
- raise WinError ()
715
- mode = DWORD (mode .value | 4 )
716
- if not windll .kernel32 .SetConsoleMode (stdout_handle , mode ):
717
- raise WinError ()
718
- except Exception :
719
- should_color = False
720
-
721
- STYLES ["red" ] = "\x1b [91m" if should_color else ""
722
- STYLES ["green" ] = "\x1b [92m" if should_color else ""
723
- STYLES ["yellow" ] = "\x1b [93m" if should_color else ""
724
- STYLES ["bold" ] = "\x1b [1m" if should_color else ""
725
- STYLES ["regular" ] = "\x1b [22m" if should_color else ""
726
- STYLES ["reset" ] = "\x1b [0m" if should_color else ""
700
+ toggle_color (args .color )
727
701
728
702
# Retrieve heading translations for the given language.
729
703
if not args .dry_run and args .lang != "en" :
@@ -834,16 +808,16 @@ def main() -> None:
834
808
if state .script_language_parity_check .hit_count > 0 :
835
809
if not args .verbose :
836
810
print (
837
- f' { STYLES [ "yellow" ] } { state .script_language_parity_check .hit_count } code samples failed parity check. Use --verbose to get more information.{ STYLES [ "reset" ] } '
811
+ f" { Ansi . YELLOW } { state .script_language_parity_check .hit_count } code samples failed parity check. Use --verbose to get more information.{ Ansi . RESET } "
838
812
)
839
813
else :
840
814
print (
841
- f' { STYLES [ "yellow" ] } { state .script_language_parity_check .hit_count } code samples failed parity check:{ STYLES [ "reset" ] } '
815
+ f" { Ansi . YELLOW } { state .script_language_parity_check .hit_count } code samples failed parity check:{ Ansi . RESET } "
842
816
)
843
817
844
818
for class_name in state .script_language_parity_check .hit_map .keys ():
845
819
class_hits = state .script_language_parity_check .hit_map [class_name ]
846
- print (f'{ STYLES [ "yellow" ] } - { len (class_hits )} hits in class "{ class_name } "{ STYLES [ "reset" ] } ' )
820
+ print (f'{ Ansi . YELLOW } - { len (class_hits )} hits in class "{ class_name } "{ Ansi . RESET } ' )
847
821
848
822
for context , error in class_hits :
849
823
print (f" - { error } in { format_context_name (context )} " )
@@ -853,24 +827,22 @@ def main() -> None:
853
827
854
828
if state .num_warnings >= 2 :
855
829
print (
856
- f' { STYLES [ "yellow" ] } { state .num_warnings } warnings were found in the class reference XML. Please check the messages above.{ STYLES [ "reset" ] } '
830
+ f" { Ansi . YELLOW } { state .num_warnings } warnings were found in the class reference XML. Please check the messages above.{ Ansi . RESET } "
857
831
)
858
832
elif state .num_warnings == 1 :
859
833
print (
860
- f' { STYLES [ "yellow" ] } 1 warning was found in the class reference XML. Please check the messages above.{ STYLES [ "reset" ] } '
834
+ f" { Ansi . YELLOW } 1 warning was found in the class reference XML. Please check the messages above.{ Ansi . RESET } "
861
835
)
862
836
863
837
if state .num_errors >= 2 :
864
838
print (
865
- f' { STYLES [ "red" ] } { state .num_errors } errors were found in the class reference XML. Please check the messages above.{ STYLES [ "reset" ] } '
839
+ f" { Ansi . RED } { state .num_errors } errors were found in the class reference XML. Please check the messages above.{ Ansi . RESET } "
866
840
)
867
841
elif state .num_errors == 1 :
868
- print (
869
- f'{ STYLES ["red" ]} 1 error was found in the class reference XML. Please check the messages above.{ STYLES ["reset" ]} '
870
- )
842
+ print (f"{ Ansi .RED } 1 error was found in the class reference XML. Please check the messages above.{ Ansi .RESET } " )
871
843
872
844
if state .num_warnings == 0 and state .num_errors == 0 :
873
- print (f' { STYLES [ "green" ] } No warnings or errors found in the class reference XML.{ STYLES [ "reset" ] } ' )
845
+ print (f" { Ansi . GREEN } No warnings or errors found in the class reference XML.{ Ansi . RESET } " )
874
846
if not args .dry_run :
875
847
print (f"Wrote reStructuredText files for each class to: { args .output } " )
876
848
else :
@@ -881,12 +853,12 @@ def main() -> None:
881
853
882
854
883
855
def print_error (error : str , state : State ) -> None :
884
- print (f' { STYLES [ "red" ] } { STYLES [ "bold" ] } ERROR:{ STYLES [ "regular" ] } { error } { STYLES [ "reset" ] } ' )
856
+ print (f" { Ansi . RED } { Ansi . BOLD } ERROR:{ Ansi . REGULAR } { error } { Ansi . RESET } " )
885
857
state .num_errors += 1
886
858
887
859
888
860
def print_warning (warning : str , state : State ) -> None :
889
- print (f' { STYLES [ "yellow" ] } { STYLES [ "bold" ] } WARNING:{ STYLES [ "regular" ] } { warning } { STYLES [ "reset" ] } ' )
861
+ print (f" { Ansi . YELLOW } { Ansi . BOLD } WARNING:{ Ansi . REGULAR } { warning } { Ansi . RESET } " )
890
862
state .num_warnings += 1
891
863
892
864
0 commit comments