jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/729231 )
Change subject: [IMPR] provide a initialize_handlers wrapper for ui functions ......................................................................
[IMPR] provide a initialize_handlers wrapper for ui functions
provide a initialize_handlers wrapper for ui functions and decorate ui functions with initialize_handlers wrapper
Patch detached from Ia1c7f31
Change-Id: Iad9cae7e08f2a7da0be8434adfa9ebf65370367c --- M pywikibot/bot.py 1 file changed, 21 insertions(+), 17 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py index d477031..d439154 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -98,8 +98,10 @@ import time import warnings import webbrowser + from collections.abc import Generator from contextlib import closing +from functools import wraps from importlib import import_module from pathlib import Path from textwrap import fill @@ -320,11 +322,11 @@ def init_handlers() -> None: """Initialize logging system for terminal-based bots.
- This function must be called before using pywikibot.output(); and must - be called again if the destination stream is changed. + This function must be called before using any input/output methods; + and must be called again if ui handler is changed..
- Note: this function is called by handle_args(), so it should normally - not need to be called explicitly + Note: this function is called by any user input and output function, + so it should normally not need to be called explicitly.
All user output is routed through the logging module. Each type of output is handled by an appropriate handler object. @@ -532,7 +534,19 @@
# User input functions
+def initialize_handlers(function): + """Make sure logging system has been initialized.
+ .. versionadded:: 7.0 + """ + @wraps(function) + def wrapper(*args, **kwargs): + init_handlers() + return function(*args, **kwargs) + return wrapper + + +@initialize_handlers def input(question: str, password: bool = False, default: Optional[str] = '', @@ -546,15 +560,11 @@ an answer. :param force: Automatically use the default """ - # make sure logging system has been initialized - if not _handlers_initialized: - init_handlers() - assert ui is not None - data = ui.input(question, password=password, default=default, force=force) - return data + return ui.input(question, password=password, default=default, force=force)
+@initialize_handlers def input_choice(question: str, answers: ANSWER_TYPE, default: Optional[str] = None, @@ -579,10 +589,6 @@ selected, it does not return the shortcut and the default is not a valid shortcut. """ - # make sure logging system has been initialized - if not _handlers_initialized: - init_handlers() - assert ui is not None return ui.input_choice(question, answers, default, return_shortcut, automatic_quit=automatic_quit, force=force) @@ -619,6 +625,7 @@ automatic_quit=automatic_quit, force=force) == 'y'
+@initialize_handlers def input_list_choice(question: str, answers: ANSWER_TYPE, default: Union[int, str, None] = None, @@ -633,9 +640,6 @@ :param force: Automatically use the default :return: The selected answer. """ - if not _handlers_initialized: - init_handlers() - assert ui is not None return ui.input_list_choice(question, answers, default=default, force=force)
pywikibot-commits@lists.wikimedia.org