@@ -11,28 +11,83 @@ from zulipterminal.config.keys import (
1111 KEY_BINDINGS ,
1212 display_keys_for_command ,
1313)
14+ from zulipterminal .config .regexes import REGEX_READLINE_COMMANDS
1415
1516
16- KEYS_FILE = (
17- Path (__file__ ).resolve ().parent .parent / "zulipterminal" / "config" / "keys.py"
18- )
17+ # absolute path to zulip-terminal
18+ ROOT_DIRECTORY = Path (__file__ ).resolve ().parent .parent
19+
20+ # absolute path to zulip-terminal/zulipterminal to be passed as parameter
21+ ZULIPTERMINAL = ROOT_DIRECTORY / "zulipterminal"
22+
23+ KEYS_FILE = ZULIPTERMINAL / "config" / "keys.py"
1924KEYS_FILE_NAME = KEYS_FILE .name
20- OUTPUT_FILE = Path ( __file__ ). resolve (). parent . parent / "docs" / "hotkeys.md"
25+ OUTPUT_FILE = ROOT_DIRECTORY / "docs" / "hotkeys.md"
2126OUTPUT_FILE_NAME = OUTPUT_FILE .name
2227SCRIPT_NAME = PurePath (__file__ ).name
2328HELP_TEXT_STYLE = re .compile (r"^[a-zA-Z /()',&@#:_-]*$" )
2429
2530# Exclude keys from duplicate keys checking
2631KEYS_TO_EXCLUDE = ["q" , "e" , "m" , "r" ]
2732
33+ # Exclude files from being checked for external command usage
34+ EXCLUDED_FILES = [
35+ KEYS_FILE ,
36+ ZULIPTERMINAL / "config" / "regexes.py" ,
37+ ]
38+
2839
2940def main (fix : bool ) -> None :
41+ lint_all_external_commands ()
3042 if fix :
3143 generate_hotkeys_file ()
3244 else :
3345 lint_hotkeys_file ()
3446
3547
48+ def lint_all_external_commands () -> None :
49+ lint_external_commands_by_type (
50+ regex_pattern = REGEX_READLINE_COMMANDS ,
51+ command_type = "Urwid Readline" ,
52+ suffix = "READLINE_SUFFIX" ,
53+ )
54+ print ("All external commands have been linted successfully." )
55+
56+
57+ def lint_external_commands_by_type (
58+ regex_pattern : str , command_type : str , suffix : str
59+ ) -> None :
60+ """
61+ Lint src directory for the usage of external commands
62+ in the codebase by checking for their regex
63+ """
64+ error_flag = 0
65+ for file_path in ZULIPTERMINAL .rglob ("*.py" ):
66+ if file_path in EXCLUDED_FILES :
67+ continue
68+ with file_path .open () as f :
69+ contents = f .read ()
70+ regex_matches = re .finditer (regex_pattern , contents )
71+ suffix_matches = re .finditer (suffix , contents )
72+ count_matches = sum (1 for _ in regex_matches ) + sum (
73+ 1 for _ in suffix_matches
74+ )
75+ if count_matches > 0 :
76+ print (
77+ f"{ file_path .name } contains { count_matches } mentions of { command_type } commands."
78+ )
79+ error_flag = 1
80+ if error_flag == 1 :
81+ print (
82+ f"{ command_type } commands are not intended for direct use or modification."
83+ )
84+ print (
85+ f"Please refer to { KEYS_FILE_NAME } for identifying the { command_type } commands."
86+ )
87+ print ("Rerun this command after removing the usage of external commands." )
88+ sys .exit (error_flag )
89+
90+
3691def lint_hotkeys_file () -> None :
3792 """
3893 Lint KEYS_FILE for key description, then compare if in sync with
0 commit comments