@@ -11,28 +11,83 @@ from zulipterminal.config.keys import (
11
11
KEY_BINDINGS ,
12
12
display_keys_for_command ,
13
13
)
14
+ from zulipterminal .config .regexes import REGEX_READLINE_COMMANDS
14
15
15
16
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"
19
24
KEYS_FILE_NAME = KEYS_FILE .name
20
- OUTPUT_FILE = Path ( __file__ ). resolve (). parent . parent / "docs" / "hotkeys.md"
25
+ OUTPUT_FILE = ROOT_DIRECTORY / "docs" / "hotkeys.md"
21
26
OUTPUT_FILE_NAME = OUTPUT_FILE .name
22
27
SCRIPT_NAME = PurePath (__file__ ).name
23
28
HELP_TEXT_STYLE = re .compile (r"^[a-zA-Z /()',&@#:_-]*$" )
24
29
25
30
# Exclude keys from duplicate keys checking
26
31
KEYS_TO_EXCLUDE = ["q" , "e" , "m" , "r" ]
27
32
33
+ # Exclude files from being checked for external command usage
34
+ EXCLUDED_FILES = [
35
+ KEYS_FILE ,
36
+ ZULIPTERMINAL / "config" / "regexes.py" ,
37
+ ]
38
+
28
39
29
40
def main (fix : bool ) -> None :
41
+ lint_all_external_commands ()
30
42
if fix :
31
43
generate_hotkeys_file ()
32
44
else :
33
45
lint_hotkeys_file ()
34
46
35
47
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
+
36
91
def lint_hotkeys_file () -> None :
37
92
"""
38
93
Lint KEYS_FILE for key description, then compare if in sync with
0 commit comments