]> jfr.im git - irc/weechat/weechat.git/commitdiff
python: Include constant values in python stub
authorTrygve Aaberge <redacted>
Sun, 2 Oct 2022 17:16:00 +0000 (19:16 +0200)
committerSébastien Helleu <redacted>
Sun, 2 Oct 2022 18:25:17 +0000 (20:25 +0200)
This is useful for two reasons:

1. When running unit tests for a script weechat needs to be mocked. By
   having the constant values available in the stub file, they can be
   loaded from that, instead of having to define the constants manually
   for the mock.

2. If you log a constant value you have to look up what it means. This
   makes it easier, in the same vein as PR #1824.

doc/python_stub.py
src/plugins/python/weechat.pyi

index 7778d975335f220c6f3b56768936af818f78704e..ba5f730c4878e53610fa9de65de6f0462299245a 100755 (executable)
@@ -28,6 +28,7 @@ from pathlib import Path
 import re
 
 DOC_DIR = Path(__file__).resolve().parent / "en"
+SRC_DIR = Path(__file__).resolve().parent.parent / "src"
 
 STUB_HEADER = """\
 #
@@ -56,11 +57,19 @@ def print_stub_constants() -> None:
         "string": "str",
     }
     constant_pattern = re.compile(CONSTANT_RE)
-    with open(DOC_DIR / "weechat_scripting.en.adoc",
-              encoding="utf-8") as scripting_file:
+    with open(
+        DOC_DIR / "weechat_scripting.en.adoc", encoding="utf-8"
+    ) as scripting_file, open(
+        SRC_DIR / "plugins" / "weechat-plugin.h", encoding="utf-8"
+    ) as plugin_header_file:
         scripting = scripting_file.read()
+        plugin_header = plugin_header_file.read()
         for match in constant_pattern.finditer(scripting):
-            print(f'{match["constant"]}: {types[match["type"]]}')
+            value_re = rf'^#define {match["constant"]} +(?P<value>[\w"-]+)$'
+            value_match = re.search(value_re, plugin_header, re.MULTILINE)
+            value = f' = {value_match["value"]}' if value_match else ""
+
+            print(f'{match["constant"]}: {types[match["type"]]}{value}')
 
 
 def print_stub_functions() -> None:
index de277d54854a41e02d1c9daccb1217d1321ead38..8c20260c95064ce947e276acdf7a48f027100b6c 100644 (file)
@@ -5,46 +5,46 @@
 
 from typing import Dict
 
-WEECHAT_RC_OK: int
-WEECHAT_RC_OK_EAT: int
-WEECHAT_RC_ERROR: int
-WEECHAT_CONFIG_READ_OK: int
-WEECHAT_CONFIG_READ_MEMORY_ERROR: int
-WEECHAT_CONFIG_READ_FILE_NOT_FOUND: int
-WEECHAT_CONFIG_WRITE_OK: int
-WEECHAT_CONFIG_WRITE_ERROR: int
-WEECHAT_CONFIG_WRITE_MEMORY_ERROR: int
-WEECHAT_CONFIG_OPTION_SET_OK_CHANGED: int
-WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE: int
-WEECHAT_CONFIG_OPTION_SET_ERROR: int
-WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND: int
-WEECHAT_CONFIG_OPTION_UNSET_OK_NO_RESET: int
-WEECHAT_CONFIG_OPTION_UNSET_OK_RESET: int
-WEECHAT_CONFIG_OPTION_UNSET_OK_REMOVED: int
-WEECHAT_CONFIG_OPTION_UNSET_ERROR: int
-WEECHAT_LIST_POS_SORT: str
-WEECHAT_LIST_POS_BEGINNING: str
-WEECHAT_LIST_POS_END: str
-WEECHAT_HOTLIST_LOW: str
-WEECHAT_HOTLIST_MESSAGE: str
-WEECHAT_HOTLIST_PRIVATE: str
-WEECHAT_HOTLIST_HIGHLIGHT: str
-WEECHAT_HOOK_PROCESS_RUNNING: int
-WEECHAT_HOOK_PROCESS_ERROR: int
-WEECHAT_HOOK_CONNECT_OK: int
-WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND: int
-WEECHAT_HOOK_CONNECT_IP_ADDRESS_NOT_FOUND: int
-WEECHAT_HOOK_CONNECT_CONNECTION_REFUSED: int
-WEECHAT_HOOK_CONNECT_PROXY_ERROR: int
-WEECHAT_HOOK_CONNECT_LOCAL_HOSTNAME_ERROR: int
-WEECHAT_HOOK_CONNECT_GNUTLS_INIT_ERROR: int
-WEECHAT_HOOK_CONNECT_GNUTLS_HANDSHAKE_ERROR: int
-WEECHAT_HOOK_CONNECT_MEMORY_ERROR: int
-WEECHAT_HOOK_CONNECT_TIMEOUT: int
-WEECHAT_HOOK_CONNECT_SOCKET_ERROR: int
-WEECHAT_HOOK_SIGNAL_STRING: str
-WEECHAT_HOOK_SIGNAL_INT: str
-WEECHAT_HOOK_SIGNAL_POINTER: str
+WEECHAT_RC_OK: int = 0
+WEECHAT_RC_OK_EAT: int = 1
+WEECHAT_RC_ERROR: int = -1
+WEECHAT_CONFIG_READ_OK: int = 0
+WEECHAT_CONFIG_READ_MEMORY_ERROR: int = -1
+WEECHAT_CONFIG_READ_FILE_NOT_FOUND: int = -2
+WEECHAT_CONFIG_WRITE_OK: int = 0
+WEECHAT_CONFIG_WRITE_ERROR: int = -1
+WEECHAT_CONFIG_WRITE_MEMORY_ERROR: int = -2
+WEECHAT_CONFIG_OPTION_SET_OK_CHANGED: int = 2
+WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE: int = 1
+WEECHAT_CONFIG_OPTION_SET_ERROR: int = 0
+WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND: int = -1
+WEECHAT_CONFIG_OPTION_UNSET_OK_NO_RESET: int = 0
+WEECHAT_CONFIG_OPTION_UNSET_OK_RESET: int = 1
+WEECHAT_CONFIG_OPTION_UNSET_OK_REMOVED: int = 2
+WEECHAT_CONFIG_OPTION_UNSET_ERROR: int = -1
+WEECHAT_LIST_POS_SORT: str = "sort"
+WEECHAT_LIST_POS_BEGINNING: str = "beginning"
+WEECHAT_LIST_POS_END: str = "end"
+WEECHAT_HOTLIST_LOW: str = "0"
+WEECHAT_HOTLIST_MESSAGE: str = "1"
+WEECHAT_HOTLIST_PRIVATE: str = "2"
+WEECHAT_HOTLIST_HIGHLIGHT: str = "3"
+WEECHAT_HOOK_PROCESS_RUNNING: int = -1
+WEECHAT_HOOK_PROCESS_ERROR: int = -2
+WEECHAT_HOOK_CONNECT_OK: int = 0
+WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND: int = 1
+WEECHAT_HOOK_CONNECT_IP_ADDRESS_NOT_FOUND: int = 2
+WEECHAT_HOOK_CONNECT_CONNECTION_REFUSED: int = 3
+WEECHAT_HOOK_CONNECT_PROXY_ERROR: int = 4
+WEECHAT_HOOK_CONNECT_LOCAL_HOSTNAME_ERROR: int = 5
+WEECHAT_HOOK_CONNECT_GNUTLS_INIT_ERROR: int = 6
+WEECHAT_HOOK_CONNECT_GNUTLS_HANDSHAKE_ERROR: int = 7
+WEECHAT_HOOK_CONNECT_MEMORY_ERROR: int = 8
+WEECHAT_HOOK_CONNECT_TIMEOUT: int = 9
+WEECHAT_HOOK_CONNECT_SOCKET_ERROR: int = 10
+WEECHAT_HOOK_SIGNAL_STRING: str = "string"
+WEECHAT_HOOK_SIGNAL_INT: str = "int"
+WEECHAT_HOOK_SIGNAL_POINTER: str = "pointer"
 
 
 def register(name: str, author: str, version: str, license: str, description: str, shutdown_function: str, charset: str) -> int: