Skip to content

Commit 0bb9b8d

Browse files
committed
Add warnings hook_process_url and hook_process_hashtable_url
1 parent 6df21af commit 0bb9b8d

File tree

8 files changed

+94
-8
lines changed

8 files changed

+94
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Remove warnings on deprecated infos `irc_nick_color` and `irc_nick_color_name` (used again with WeeChat >= 4.1.0)
88

9+
### Added
10+
11+
- Add warnings `hook_process_url` and `hook_process_hashtable_url`
12+
913
### Changed
1014

1115
- Improve regex to detect e-mails

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ which may not be UTF-8 valid. This is a problem in some languages like Python.
116116

117117
**How to fix**: use the signal `irc_out1_xxx`.
118118

119+
### Warning: hook_process_url
120+
121+
**Issue**: the function `hook_process` used with `url:` should be
122+
replaced by the new function `hook_url` added in WeeChat 4.1.0, which uses
123+
a thread instead of a new process, making it more lightweight and thus
124+
recommended for this usage.
125+
126+
**How to fix**: call the function `hook_url` and make necessary changes
127+
in code, as the function is different from `hook_process`.
128+
129+
### Warning: hook_process_hashtable_url
130+
131+
**Issue**: the function `hook_process_hashtable` used with `url:` should be
132+
replaced by the new function `hook_url` added in WeeChat 4.1.0, which uses
133+
a thread instead of a new process, making it more lightweight and thus
134+
recommended for this usage.
135+
136+
**How to fix**: call the function `hook_url` and make necessary changes
137+
in code, as the function is different from `hook_process_hashtable`.
138+
119139
### Info: unneeded_shebang
120140

121141
**Issue**: the shebang is not needed, except if the script can be called

tests/scripts/script_all_errors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222
weechat.hook_modifier("irc_in_privmsg", "callback", "")
2323
weechat.hook_signal("*,irc_out_privmsg", "callback", "")
2424
weechat.hook_signal("*,irc_outtags_privmsg", "callback", "")
25+
weechat.hook_process("url:http://localhost:1234", 1000, "callback", "")
26+
weechat.hook_process_hashtable("url:http://localhost:1234", {}, 1000, "callback", "")
2527
sys.exit(1)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# Author: Sébastien Helleu <[email protected]>
3+
#
4+
5+
"""A WeeChat script."""
6+
7+
import weechat
8+
9+
if __name__ == "__main__":
10+
if weechat.register("script", "author", "0.1", "GPL3", "desc", "", ""):
11+
weechat.hook_process("url:http://localhost:1234", 1000, "callback", "")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# Author: Sébastien Helleu <[email protected]>
3+
#
4+
5+
"""A WeeChat script."""
6+
7+
import weechat
8+
9+
if __name__ == "__main__":
10+
if weechat.register("script", "author", "0.1", "GPL3", "desc", "", ""):
11+
weechat.hook_process_hashtable("url:http://localhost:1234", {}, 1000, "callback", "")

tests/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def test_main_dir() -> None:
104104
with mock.patch.object(sys, "argv", args):
105105
weechat_script_lint.main()
106106
assert exc.type == SystemExit
107-
assert exc.value.code == 22
107+
assert exc.value.code == 26
108108

109109
# check directory with scripts, display scripts by score
110110
args = [

tests/test_script.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
("error", 17, "missing_infolist_free"),
3434
("error", 18, "python2_bin"),
3535
("error", 1, "mixed_tabs_spaces"),
36-
("warning", 25, "sys_exit"),
36+
("warning", 27, "sys_exit"),
3737
("warning", 19, "deprecated_hook_completion_get_string"),
3838
("warning", 20, "deprecated_hook_completion_list_add"),
3939
("warning", 22, "modifier_irc_in"),
4040
("warning", 23, "signal_irc_out"),
4141
("warning", 24, "signal_irc_outtags"),
42+
("warning", 25, "hook_process_url"),
43+
("warning", 26, "hook_process_hashtable_url"),
4244
("info", 1, "unneeded_shebang"),
4345
("info", 13, "url_weechat"),
4446
]
@@ -79,12 +81,12 @@ def test_script_all_errors() -> None:
7981
assert script.script
8082
script.check()
8183
assert str(script)
82-
assert len(str(script).split("\n")) == 12
84+
assert len(str(script).split("\n")) == len(ALL_ERRORS)
8385
print(script.count)
84-
assert script.count == {"error": 4, "warning": 6, "info": 2}
86+
assert script.count == {"error": 4, "warning": 8, "info": 2}
8587
errors = [(msg.level, msg.line, msg.msg_name) for msg in script.messages]
8688
assert errors == ALL_ERRORS
87-
assert len(script.get_report(False).split("\n")) == 12
89+
assert len(script.get_report(False).split("\n")) == len(ALL_ERRORS)
8890
assert script.get_report(True) == "script_all_errors.py"
8991

9092
# ignore 2 messages: "missing_email" and "sys_exit"
@@ -99,9 +101,9 @@ def test_script_all_errors() -> None:
99101
assert script.script
100102
script.check()
101103
assert str(script)
102-
assert len(str(script).split("\n")) == 10
103-
assert script.count == {"error": 3, "warning": 5, "info": 2}
104-
assert len(script.get_report(False).split("\n")) == 10
104+
assert len(str(script).split("\n")) == len(ALL_ERRORS) - 2
105+
assert script.count == {"error": 3, "warning": 7, "info": 2}
106+
assert len(script.get_report(False).split("\n")) == len(ALL_ERRORS) - 2
105107
assert script.get_report(True) == "script_all_errors.py"
106108

107109

weechat_script_lint/script.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@
9090
"signal irc_outtags_{message} should be replaced by "
9191
"irc_out1_{message} which sends only valid UTF-8 data",
9292
),
93+
"hook_process_url": (
94+
-5,
95+
"function hook_process with \"url:\" should be replaced "
96+
"by hook_url (WeeChat ≥ 4.1.0)",
97+
),
98+
"hook_process_hashtable_url": (
99+
-5,
100+
"function hook_process_hashtable with \"url:\" should be replaced "
101+
"by hook_url (WeeChat ≥ 4.1.0)",
102+
),
93103
},
94104
"info": {
95105
"unneeded_shebang": (
@@ -347,6 +357,32 @@ def _check_signals_irc_out(self) -> None:
347357
message=m.group(1),
348358
)
349359

360+
def _check_hook_process_url(self) -> None:
361+
"""Check if hook_process(_hashtable) with "url:" is used."""
362+
func_url = self.search_regex("hook_url")
363+
if func_url:
364+
return
365+
func_process = self.search_func(
366+
"hook_process", r"[\"']url:"
367+
)
368+
func_process_hashtable = self.search_func(
369+
"hook_process_hashtable", r"[\"']url:"
370+
)
371+
if func_process:
372+
for line_no, m in func_process:
373+
self.message(
374+
"warning",
375+
"hook_process_url",
376+
line=line_no,
377+
)
378+
if func_process_hashtable:
379+
for line_no, m in func_process_hashtable:
380+
self.message(
381+
"warning",
382+
"hook_process_hashtable_url",
383+
line=line_no,
384+
)
385+
350386
# === info ===
351387

352388
def _check_shebang(self) -> None:

0 commit comments

Comments
 (0)