Skip to content

Commit 514db27

Browse files
committed
lint/regex: keys: Add linting for usage of external commands.
Identify external commands using the added suffix. Search for both the suffix variable and the actual suffix.
1 parent 84f5014 commit 514db27

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

tools/lint-hotkeys

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
1924
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"
2126
OUTPUT_FILE_NAME = OUTPUT_FILE.name
2227
SCRIPT_NAME = PurePath(__file__).name
2328
HELP_TEXT_STYLE = re.compile(r"^[a-zA-Z /()',&@#:_-]*$")
2429

2530
# Exclude keys from duplicate keys checking
2631
KEYS_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

2940
def 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+
3691
def lint_hotkeys_file() -> None:
3792
"""
3893
Lint KEYS_FILE for key description, then compare if in sync with

zulipterminal/config/regexes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88

99
# (*) Stream and topic regexes
10+
from zulipterminal.config.keys import READLINE_SUFFIX
11+
12+
1013
REGEX_STREAM_NAME = r"([^*>]+)"
1114
REGEX_TOPIC_NAME = r"([^*]*)"
1215

@@ -46,3 +49,7 @@
4649

4750
# Example: 6-test-stream
4851
REGEX_INTERNAL_LINK_STREAM_ID = r"^[0-9]+-"
52+
53+
54+
# Example: UNDO_LAST_ACTION_READLINE
55+
REGEX_READLINE_COMMANDS = rf"([A-Z_]+{READLINE_SUFFIX})"

0 commit comments

Comments
 (0)