userinterfaces package

User interfaces.

_interface_base module

Abstract base user interface module.

New in version 6.2.

class pywikibot.userinterfaces._interface_base.ABUIC[source]

Bases: abc.ABC

Abstract base user interface class.

Every user interface should derive from it to ensure that all required methods are implemented.

argvu()list[source]

Return copy of sys.argv.

Assigned to pywikibot.argvu in bot module

flush()[source]

Flush cached output.

May be passed to atexit.register() to flush any ui cache.

abstract init_handlers(*args, **kwargs)[source]

Initialize the handlers for user output.

Called in bot.init_handlers().

abstract input(*args, **kwargs)str[source]

Ask the user a question and return the answer.

Called by bot.input().

abstract input_choice(*args, **kwargs)Union[int, str][source]

Ask the user and returns a value from the options.

Called by bot.input_choice().

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

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

Called by bot.input_list_choice().

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

Output text to a stream.

terminal_interface module

Platform independent terminal interface module.

It imports the appropriate operating system specific implementation.

pywikibot.userinterfaces.terminal_interface.UI

alias of pywikibot.userinterfaces.terminal_interface_unix.UnixUI

terminal_interface_base module

Base for terminal user interfaces.

class pywikibot.userinterfaces.terminal_interface_base.MaxLevelFilter(level=None)[source]

Bases: logging.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 pywikibot.userinterfaces.terminal_interface_base.TerminalHandler(UI, stream=None, strm='[deprecated name of stream]')[source]

Bases: logging.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.

emit(record)[source]

Emit the record formatted to the output.

sharedlock = <unlocked _thread.RLock object owner=0 count=0>
class pywikibot.userinterfaces.terminal_interface_base.UI[source]

Bases: pywikibot.userinterfaces._interface_base.ABUIC

Base for terminal user interfaces.

New in version 6.2: subclassed from pywikibot.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.

argvu()[source]

Return copy of argv.

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: str, jumpIndex: Optional[int] = None, highlight: Optional[str] = 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 – the text to be edited

  • jumpIndex – position at which to put the caret

  • highlight – 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

str or None

encounter_color(color, target_stream)[source]

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.

input(question: str, password: bool = False, default: str = '', force: bool = False)str[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 – The question, without trailing whitespace.

  • password – if True, hides the user’s input (for password entry).

  • default – The default answer if none was entered. None to require an answer.

  • force – Automatically use the default

input_choice(question: str, options, default: Optional[str] = None, return_shortcut: bool = True, automatic_quit: bool = True, force: bool = 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 – The question, without trailing whitespace.

  • options (iterable containing sequences of length 2 or iterable containing Option instances or ChoiceException as well. Singletons of Option and its subclasses are also accepted.) – 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 – The default answer if no was entered. None to require an answer.

  • return_shortcut – Whether the shortcut or the index in the option should be returned.

  • automatic_quit – Adds the option ‘Quit’ (‘q’) if True and throws a QuitKeyboardInterrupt if selected.

  • force – 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

int (if not return_shortcut), lowercased str (otherwise)

input_list_choice(question: str, answers: collections.abc.Sequence[Any], default: Optional[Union[int, str]] = None, force: bool = False)Any[source]

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

Parameters
  • question – The question, without trailing whitespace.

  • answers – A sequence of options to be choosen.

  • default – The default answer if no was entered. None to require an answer.

  • force – Automatically use the default.

Returns

Return a single Sequence entry.

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.

split_col_pat = re.compile('(\\w+);?(\\w+)?')
support_color(target_stream)[source]

Return whether the target stream does support colors.

terminal_interface_unix module

User interface for Unix terminals.

class pywikibot.userinterfaces.terminal_interface_unix.UnixUI[source]

Bases: pywikibot.userinterfaces.terminal_interface_base.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.

encounter_color(color, target_stream)[source]

Write the Unix color directly to the stream.

make_unix_bg_color(color)[source]

Obtain background color from foreground color.

support_color(target_stream)[source]

Return that the target stream supports colors.

terminal_interface_win32 module

User interface for Win32 terminals.

class pywikibot.userinterfaces.terminal_interface_win32.Win32UI[source]

Bases: pywikibot.userinterfaces.terminal_interface_base.UI

User interface for Win32 terminals.

encounter_color(color, target_stream)[source]

Set the new color.

support_color(target_stream)[source]

Return whether the target stream supports actually color.

gui module

A window with a textfield where the user can edit.

Useful for editing the contents of an article.

New in version 6.1: Python 3.6 or highter is required.

class pywikibot.userinterfaces.gui.EditBoxWindow(parent=None, **kwargs)[source]

Bases: tkinter.Frame

Edit box window.

config_dialog(event=None)[source]

Show config dialog.

debug(event=None)[source]

Call quit() and return ‘break’.

edit(text: str, jumpIndex: Optional[int] = None, highlight: Optional[str] = None)[source]

Provide user with editor to modify text.

Parameters
  • text – the text to be edited

  • jumpIndex – position at which to put the caret

  • highlight – 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

str or None

find()[source]

Perform find operation.

find_all(target)[source]

Perform find all operation.

pressedOK()[source]

Perform OK operation.

Called when user pushes the OK button. Saves the buffer into a variable, and closes the window.

class pywikibot.userinterfaces.gui.ListBoxWindow(parent=None)[source]

Bases: object

List box window.

list(list)[source]

Put list of alternatives into listbox.

pressedOK()[source]

Perform OK operation.

Closes listbox.

class pywikibot.userinterfaces.gui.TextEditor(master=None, **kwargs)[source]

Bases: tkinter.scrolledtext.ScrolledText

A text widget with some editing enhancements.

A lot of code here is copied or adapted from the idlelib/EditorWindow.py file in the standard Python distribution.

Get default settings from user’s IDLE configuration.

add_bindings()[source]

Assign key and events bindings to methods.

copy(event)[source]

Perform copy operation.

cut(event)[source]

Perform cut operation.

del_word_left(event)[source]

Perform delete word (left) operation.

del_word_right(event=None)[source]

Perform delete word (right) operation.

do_highlight(start, end)[source]

Select and show the text from index start to index end.

find_again_event(event=None)[source]

Perform find again operation.

find_all(s)[source]

Highlight all occurrences of string s, and select the first one.

If the string has already been highlighted, jump to the next occurrence after the current selection. (You cannot go backwards using the button, but you can manually place the cursor anywhere in the document to start searching from that point.)

find_event(event=None)[source]

Perform find operation.

find_selection_event(event=None)[source]

Perform find selection operation.

goto_line_event(event)[source]

Perform goto line operation.

paste(event)[source]

Perform paste operation.

remove_selection(event=None)[source]

Perform remove operation.

replace_event(event=None)[source]

Perform replace operation.

select_all(event=None)[source]

Perform select all operation.

class pywikibot.userinterfaces.gui.Tkdialog(photo_description, photo, filename)[source]

Bases: object

The dialog window for image info.

get_image(photo, width, height)[source]

Take the BytesIO object and build an imageTK thumbnail.

ok_file()[source]

The user pressed the OK button.

show_dialog()tuple[source]

Activate the dialog.

Returns

new description, name, and if the image is skipped

skip_file()[source]

The user pressed the Skip button.

transliteration module

Module to transliterate text.

class pywikibot.userinterfaces.transliteration.transliterator(encoding: str)[source]

Bases: object

Class to transliterating text.

Initialize the transliteration mapping.

Parameters

encoding – the encoding available. Any transliterated character which can’t be mapped, will be removed from the mapping.

transliterate(char: str, default: str = '?', prev: str = '-', next: str = '-')str[source]

Transliterate the character.

Parameters
  • char – The character to transliterate.

  • default – The character used when there is no transliteration.

  • prev – The previous character

  • next – The next character

Returns

The transliterated character which may be an empty string

win32_unicode module

Stdout, stderr and argv support for unicode.

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

Bases: io.IOBase

Unicode terminal input class.

Initialize the input stream.

readline()[source]

Read one line from the input.

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

Bases: io.IOBase

Unicode terminal output class.

Initialize the output stream.

fileno()[source]

Return the fileno.

flush()[source]

Flush the stream.

write(text)[source]

Write the text to the output.

writelines(lines)[source]

Write a list of lines by using write.

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

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

pywikibot.userinterfaces.win32_unicode.get_unicode_console()[source]

Get Unicode console objects.

Returns

stdin, stdout, stderr, argv

Return type

tuple

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

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