userinterfaces
— User interfaces#
User interfaces module.
The user interface object must define its own init_handlers()
method
which takes the root logger as its only argument, and which adds to that
logger whatever handlers and formatters are needed to process output and
display it to the user. The default
(terminal
)
interface sends level STDOUT
to sys.stdout
(as all interfaces
should) and sends all other levels to sys.stderr
; levels
WARNING
and above are labeled with the level name.
UserInterface objects must also define methods input()
,
input_choice()
, input_list_choice()
, output()
and editText()
,
all of which are documented in the abstract class
userinterfaces._interface_base.ABUIC
.
_interface_base
— Abstract User Interface#
Abstract base user interface module.
New in version 6.2.
- class userinterfaces._interface_base.ABUIC[source]#
Bases:
ABC
Abstract base user interface class.
Every user interface should derive from it to ensure that all required methods are implemented.
New in version 6.2.
- argvu()[source]#
Return copy of sys.argv.
Assigned to pywikibot.argvu in bot module
- Return type:
List[str]
- abstract flush()[source]#
Flush cached output.
May be passed to atexit.register() to flush any ui cache.
- Return type:
None
- abstract init_handlers(*args, **kwargs)[source]#
Initialize the handlers for user output.
Called in bot.init_handlers().
- Return type:
None
- abstract input(*args, **kwargs)[source]#
Ask the user a question and return the answer.
Called by bot.input().
- Return type:
str
- abstract input_choice(*args, **kwargs)[source]#
Ask the user and returns a value from the options.
Called by bot.input_choice().
- Return type:
Union[int, str]
terminal_interface
module — Terminal User Interface#
Platform independent terminal interface module.
It imports the appropriate operating system specific implementation.
- userinterfaces.terminal_interface.UI#
alias of
UnixUI
terminal_interface_base
— Base Terminal Interface#
Base for terminal user interfaces.
- class userinterfaces.terminal_interface_base.MaxLevelFilter(level=None)[source]#
Bases:
Filter
Filter that only passes records at or below a specific level.
(setting handler level only passes records at or above a specified level, so this provides the opposite functionality)
- class userinterfaces.terminal_interface_base.TerminalHandler(UI, stream=None)[source]#
Bases:
StreamHandler
A handler class that writes logging records to a terminal.
This class does not close the stream, as sys.stdout or sys.stderr may be (and usually will be) used.
Slightly modified version of the StreamHandler class that ships with logging module, plus code for colorization of output.
Initialize the handler.
If stream is not specified, sys.stderr is used.
- createLock()[source]#
Acquire a thread lock for serializing access to the underlying I/O.
Replace Handler’s instance-specific lock with the shared class lock to ensure that only one instance of this handler can write to the console at a time.
- Return type:
None
- class userinterfaces.terminal_interface_base.UI[source]#
Bases:
ABUIC
Base for terminal user interfaces.
Changed in version 6.2:: subclassed from
userinterfaces._interface_base.ABUIC
Initialize the UI.
This caches the std-streams locally so any attempts to monkey-patch the streams later will not work.
Changed in version 7.1: memorize original streams
- classmethod divide_color(color)[source]#
Split color label in a tuple.
Received color is a string like ‘fg_color;bg_color’ or ‘fg_color’. Returned values are (fg_color, bg_color) or (fg_color, None).
- static editText(text, jumpIndex=None, highlight=None)[source]#
Return the text as edited by the user.
Uses a Tkinter edit box because we don’t have a console editor
- Parameters:
text (str) – the text to be edited
jumpIndex (Optional[int]) – position at which to put the caret
highlight (Optional[str]) – each occurrence of this substring will be highlighted
- Returns:
the modified text, or None if the user didn’t save the text file in his text editor
- Return type:
Optional[str]
- encounter_color(color, target_stream)[source]#
Abstract method to handle the next color encountered.
- init_handlers(root_logger, default_stream='stderr')[source]#
Initialize the handlers for user output.
This method initializes handler(s) for output levels VERBOSE (if enabled by config.verbose_output), INFO, STDOUT, WARNING, ERROR, and CRITICAL. STDOUT writes its output to sys.stdout; all the others write theirs to sys.stderr.
- Parameters:
default_stream (str) –
- Return type:
None
- input(question, password=False, default='', force=False)[source]#
Ask the user a question and return the answer.
Works like raw_input(), but returns a unicode string instead of ASCII.
Unlike raw_input, this function automatically adds a colon and space after the question if they are not already present. Also recognises a trailing question mark.
- Parameters:
question (str) – The question, without trailing whitespace.
password (bool) – if True, hides the user’s input (for password entry).
default (Optional[str]) – The default answer if none was entered. None to require an answer.
force (bool) – Automatically use the default
- Return type:
str
- input_choice(question, options, default=None, return_shortcut=True, automatic_quit=True, force=False)[source]#
Ask the user and returns a value from the options.
Depending on the options setting return_shortcut to False may not be sensible when the option supports multiple values as it’ll return an ambiguous index.
- Parameters:
question (str) – The question, without trailing whitespace.
options (Union[Iterable[Union[Tuple[str, str], Option]], Option]) – Iterable of all available options. Each entry contains the full length answer and a shortcut of only one character. Alternatively they may be Option (or subclass) instances or ChoiceException instances which have a full option and shortcut and will be raised if selected.
default (Optional[str]) – The default answer if no was entered. None to require an answer.
return_shortcut (bool) – Whether the shortcut or the index in the option should be returned.
automatic_quit (bool) – Adds the option ‘Quit’ (‘q’) if True and throws a
QuitKeyboardInterrupt
if selected.force (bool) – Automatically use the default
- Returns:
If return_shortcut the shortcut of options or the value of default (if it’s not None). Otherwise the index of the answer in options. If default is not a shortcut, it’ll return -1.
- Return type:
Any
- input_list_choice(question, answers, default=None, force=False)[source]#
Ask the user to select one entry from a list of entries.
- Parameters:
question (str) – The question, without trailing whitespace.
answers (Sequence[Any]) – A sequence of options to be choosen.
default (Optional[Union[str, int]]) – The default answer if no was entered. None to require an answer.
force (bool) – Automatically use the default.
- Returns:
Return a single Sequence entry.
- Return type:
Any
- output(text, targetStream=None)[source]#
Forward text to cache and flush if output is not locked.
All input methods locks the output to a stream but collect them in cache. They will be printed with next unlocked output call or at termination time.
Changed in version 7.0: Forward text to cache and flush if output is not locked.
- Return type:
None
- split_col_pat = re.compile('(\\w+);?(\\w+)?')#
- stream_output(text, targetStream=None)[source]#
Output text to a stream.
If a character can’t be displayed in the encoding used by the user’s terminal, it will be replaced with a question mark or by a transliteration.
New in version 7.0:
UI.output()
was renamed toUI.stream_output()
- Return type:
None
- userinterfaces.terminal_interface_base.colors = ['default', 'black', 'blue', 'green', 'aqua', 'red', 'purple', 'yellow', 'lightgray', 'gray', 'lightblue', 'lightgreen', 'lightaqua', 'lightred', 'lightpurple', 'lightyellow', 'white']#
Colors supported by Pywikibot
terminal_interface_unix
— Unix Terminal Interface#
User interface for Unix terminals.
- class userinterfaces.terminal_interface_unix.UnixUI[source]#
Bases:
UI
User interface for Unix terminals.
Initialize the UI.
This caches the std-streams locally so any attempts to monkey-patch the streams later will not work.
Changed in version 7.1: memorize original streams
terminal_interface_win32
—- Windows Terminal Interface#
User interface for Win32 terminals.
buffer_interface
— Buffer Interface#
Non-interactive interface that stores output.
New in version 6.4.
- class userinterfaces.buffer_interface.UI[source]#
Bases:
ABUIC
Collects output into an unseen buffer.
New in version 6.4.
Initialize the UI.
- init_handlers(root_logger, *args, **kwargs)[source]#
Initialize the handlers for user output.
- Return type:
None
- input(question, password=False, default='', force=False)[source]#
Ask the user a question and return the answer.
- Parameters:
question (str) –
password (bool) –
default (str) –
force (bool) –
- Return type:
str
- input_choice(question, options, default=None, return_shortcut=True, automatic_quit=True, force=False)[source]#
Ask the user and returns a value from the options.
- Parameters:
question (str) –
default (Optional[str]) –
return_shortcut (bool) –
automatic_quit (bool) –
force (bool) –
- input_list_choice(question, answers, default=None, force=False)[source]#
Ask the user to select one entry from a list of entries.
- Parameters:
question (str) –
answers (Sequence[Any]) –
default (Optional[Union[str, int]]) –
force (bool) –
- Return type:
Any
gui
— Graphical User Interface#
transliteration
— Transliteration#
Module to transliterate text.
- userinterfaces.transliteration.NON_LATIN_DIGITS = {'bn': '০১২৩৪৫৬৭৮৯', 'ckb': '٠١٢٣٤٥٦٧٨٩', 'fa': '۰۱۲۳۴۵۶۷۸۹', 'gu': '૦૧૨૩૪૫૬૭૮૯', 'hi': '०१२३४५६७८९', 'km': '០១២៣៤៥៦៧៨៩', 'kn': '೦೧೨೩೪೫೬೭೮೯', 'lo': '໐໑໒໓໔໕໖໗໘໙', 'ml': '൦൧൨൩൪൫൬൭൮൯', 'or': '୦୧୨୩୪୫୬୭୮୯', 'pa': '੦੧੨੩੪੫੬੭੮੯', 'te': '౦౧౨౩౪౫౬౭౮౯'}#
Non latin digits used by the framework
- class userinterfaces.transliteration.transliterator(encoding)[source]#
Bases:
object
Class to transliterating text.
Initialize the transliteration mapping.
- Parameters:
encoding (str) – the encoding available. Any transliterated character which can’t be mapped, will be removed from the mapping.
- transliterate(char, default='?', prev='-', next='-')[source]#
Transliterate the character.
- Parameters:
char (str) – The character to transliterate.
default (str) – The character used when there is no transliteration.
prev (str) – The previous character
next (str) – The next character
- Returns:
The transliterated character which may be an empty string
- Return type:
str