jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] Add abstract base user interface module to the framework

bot module calls the user interface to be used.
in compat release we had additional interfacec (cgi, tkinter, wxpython)
but all of them failed due to missing interface methods. Therefore add
a abstract base user interface class to ensure that each interface
method is provided. This probably would allow us to implement an
interface for tests and enable assertLogs/assertNoLogs unittest methods

Change-Id: Ic425fe52264b57c44fe97a0a89b9e6260944a8c7
---
M docs/api_ref/pywikibot.userinterfaces.rst
M pywikibot/CONTENT.rst
A pywikibot/userinterfaces/_interface_base.py
M pywikibot/userinterfaces/terminal_interface_base.py
4 files changed, 87 insertions(+), 4 deletions(-)

diff --git a/docs/api_ref/pywikibot.userinterfaces.rst b/docs/api_ref/pywikibot.userinterfaces.rst
index 486ae00..6d0b506 100644
--- a/docs/api_ref/pywikibot.userinterfaces.rst
+++ b/docs/api_ref/pywikibot.userinterfaces.rst
@@ -3,10 +3,10 @@

.. automodule:: pywikibot.userinterfaces

-gui module
-----------
+\_interface\_base module
+------------------------

-.. automodule:: pywikibot.userinterfaces.gui
+.. automodule:: pywikibot.userinterfaces._interface_base

terminal\_interface module
--------------------------
@@ -28,6 +28,11 @@

.. automodule:: pywikibot.userinterfaces.terminal_interface_win32

+gui module
+----------
+
+.. automodule:: pywikibot.userinterfaces.gui
+
transliteration module
----------------------

diff --git a/pywikibot/CONTENT.rst b/pywikibot/CONTENT.rst
index 5929d91..7f977f5 100644
--- a/pywikibot/CONTENT.rst
+++ b/pywikibot/CONTENT.rst
@@ -172,6 +172,8 @@
+-----------------------------------------------------------------------------------+
| User Interface |
+============================+======================================================+
+ | _interface_base.py | Abstract base user interface module |
+ +----------------------------+------------------------------------------------------+
| gui.py | GUI with a unicode textfield where the user can edit |
+----------------------------+------------------------------------------------------+
| terminal_interface.py | Platform independent terminal interface module |
diff --git a/pywikibot/userinterfaces/_interface_base.py b/pywikibot/userinterfaces/_interface_base.py
new file mode 100644
index 0000000..856a2a3
--- /dev/null
+++ b/pywikibot/userinterfaces/_interface_base.py
@@ -0,0 +1,75 @@
+"""Abstract base user interface module.
+
+*New in version 6.2.*
+"""
+#
+# (C) Pywikibot team, 2021
+#
+# Distributed under the terms of the MIT license.
+#
+import sys
+
+from abc import ABC, abstractmethod
+from typing import Any, Union
+
+from pywikibot.backports import List
+
+
+class ABUIC(ABC):
+
+ """Abstract base user interface class.
+
+ Every user interface should derive from it to ensure that all
+ required methods are implemented.
+ """
+
+ def argvu(self) -> List[str]:
+ """Return copy of sys.argv.
+
+ Assigned to pywikibot.argvu in bot module
+ """
+ return list(sys.argv)
+
+ @abstractmethod
+ def init_handlers(self, *args, **kwargs):
+ """Initialize the handlers for user output.
+
+ Called in bot.init_handlers().
+ """
+
+ @abstractmethod
+ def input(self, *args, **kwargs) -> str:
+ """Ask the user a question and return the answer.
+
+ Called by bot.input().
+ """
+ if args:
+ return input(args[0])
+ return input()
+
+ @abstractmethod
+ def input_choice(self, *args, **kwargs) -> Union[int, str]:
+ """Ask the user and returns a value from the options.
+
+ Called by bot.input_choice().
+ """
+ return self.input()
+
+ @abstractmethod
+ def input_list_choice(self, *args, **kwargs) -> Any:
+ """Ask the user to select one entry from a list of entries.
+
+ Called by bot.input_list_choice().
+ """
+ return self.input()
+
+ @abstractmethod
+ def output(self, *args, **kwargs) -> None:
+ """Output text to a stream."""
+ print(*args, **kwargs) # noqa: T001
+
+ def flush(self):
+ """Flush cached output.
+
+ May be passed to atexit.register() to flush any ui cache.
+ """
diff --git a/pywikibot/userinterfaces/terminal_interface_base.py b/pywikibot/userinterfaces/terminal_interface_base.py
index dfbb0ac..0c13fec 100755
--- a/pywikibot/userinterfaces/terminal_interface_base.py
+++ b/pywikibot/userinterfaces/terminal_interface_base.py
@@ -22,6 +22,7 @@
StandardOption,
)
from pywikibot.logging import INFO, INPUT, STDOUT, VERBOSE, WARNING
+from pywikibot.userinterfaces._interface_base import ABUIC
from pywikibot.userinterfaces import transliteration


@@ -51,7 +52,7 @@
colorTagR = re.compile('\03{((:?%s);?(:?%s)?)}' % (_color_pat, _color_pat))


-class UI:
+class UI(ABUIC):

"""Base for terminal user interfaces."""


To view, visit change 687057. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ic425fe52264b57c44fe97a0a89b9e6260944a8c7
Gerrit-Change-Number: 687057
Gerrit-PatchSet: 7
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged