userinterfaces package#

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 module#

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]

abstract input_list_choice(*args, **kwargs)[source]#

Ask the user to select one entry from a list of entries.

Called by bot.input_list_choice().

Return type:

Any

abstract output(*args, **kwargs)[source]#

Output text to a stream.

Return type:

None

terminal_interface module#

Platform independent terminal interface module.

It imports the appropriate operating system specific implementation.

userinterfaces.terminal_interface.UI#

alias of UnixUI

terminal_interface_base module#

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)

filter(record)[source]#

Return true if the level is below or equal to the set level.

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

emit(record)[source]#

Emit the record formatted to the output.

Return type:

None

sharedlock = <unlocked _thread.RLock object owner=0 count=0>#
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

cache_output(*args, **kwargs)[source]#

Put text to cache.

New in version 7.0.

Return type:

None

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).

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.

flush()[source]#

Output cached text.

New in version 7.0.

Return type:

None

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[int, str]]) – 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 to UI.stream_output()

Return type:

None

support_color(target_stream)[source]#

Return whether the target stream does support colors.

Return type:

bool

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 module#

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

encounter_color(color, target_stream)[source]#

Write the Unix color directly to the stream.

Return type:

None

static make_unix_bg_color(color)[source]#

Obtain background color from foreground color.

support_color(target_stream)[source]#

Return that the target stream supports colors.

Return type:

bool

terminal_interface_win32 module#

User interface for Win32 terminals.

class userinterfaces.terminal_interface_win32.Win32UI[source]#

Bases: UI

User interface for Win32 terminals.

encounter_color(color, target_stream)[source]#

Set the new color.

Return type:

None

support_color(target_stream)[source]#

Return whether the target stream supports actually color.

buffer_interface module#

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.

clear()[source]#

Removes any buffered output.

Return type:

None

flush()[source]#

Flush cached output.

Return type:

None

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[int, str]]) –

  • force (bool) –

Return type:

Any

output(text, *args, **kwargs)[source]#

Output text that would usually go to a stream.

Return type:

None

pop_output()[source]#

Provide and clear any buffered output.

gui module#

transliteration module#

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

win32_unicode module#

Unicode support for stdout, stderr and argv with Python 3.5.

Deprecated since version 7.1: will be removed with Pywikibot 8 when Python 3.5 support is dropped.

class userinterfaces.win32_unicode.UnicodeInput(hConsole, name, bufsize=1024)[source]#

Bases: IOBase

Unicode terminal input class.

Initialize the input stream.

Parameters:

bufsize (int) –

readline()[source]#

Read one line from the input.

class userinterfaces.win32_unicode.UnicodeOutput(hConsole, stream, fileno, name)[source]#

Bases: IOBase

Unicode terminal output class.

Initialize the output stream.

fileno()[source]#

Return the fileno.

flush()[source]#

Flush the stream.

isatty()[source]#

Return True if the stream is interactive.

write(text)[source]#

Write the text to the output.

writelines(lines)[source]#

Write a list of lines by using write.

userinterfaces.win32_unicode.force_truetype_console(h_stdout)[source]#

Force the console to use a TrueType font (Vista+).

Return type:

None

userinterfaces.win32_unicode.get_unicode_console()[source]#

Get Unicode console objects.

Returns:

stdin, stdout, stderr, argv

Return type:

Tuple[IO, IO, IO, List[str]]

userinterfaces.win32_unicode.old_fileno(std_name)[source]#

Return the fileno or None if that doesn’t work.