jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/617103 )
Change subject: [4.0] Remove Python 2 code parts in few scripts ......................................................................
[4.0] Remove Python 2 code parts in few scripts
Change-Id: Ib6ae321b3a34d774a1b641cb7d53daad6779ecbc --- M pywikibot/__metadata__.py M pywikibot/bot_choice.py M pywikibot/config2.py M pywikibot/daemonize.py 4 files changed, 38 insertions(+), 58 deletions(-)
Approvals: Matěj Suchánek: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/__metadata__.py b/pywikibot/__metadata__.py index 8eaf1f2..90c4121 100644 --- a/pywikibot/__metadata__.py +++ b/pywikibot/__metadata__.py @@ -5,8 +5,6 @@ # # Distributed under the terms of the MIT license. # -from __future__ import unicode_literals - __name__ = 'pywikibot' __version__ = '4.0.0.dev0' __description__ = 'Python MediaWiki Bot Framework' diff --git a/pywikibot/bot_choice.py b/pywikibot/bot_choice.py index a512f69..2242c94 100755 --- a/pywikibot/bot_choice.py +++ b/pywikibot/bot_choice.py @@ -1,19 +1,17 @@ # -*- coding: utf-8 -*- """Choices for input_choice.""" # -# (C) Pywikibot team, 2015-2019 +# (C) Pywikibot team, 2015-2020 # # Distributed under the terms of the MIT license. # -from __future__ import absolute_import, division, unicode_literals - import re from textwrap import fill
import pywikibot
-class Option(object): +class Option:
""" A basic option for input_choice. @@ -23,30 +21,28 @@ * result(value) * test(value)
- The methods C{test} and C{handled} are in such a relationship that when - C{handled} returns itself that C{test} must return True for that value. So - if C{test} returns False C{handled} may not return itself but it may return - not None. + The methods C{test} and C{handled} are in such a relationship that + when C{handled} returns itself that C{test} must return True for + that value. So if C{test} returns False C{handled} may not return + itself but it may return not None.
Also C{result} only returns a sensible value when C{test} returns True for the same value. """
- def __init__(self, stop=True): + def __init__(self, stop=True) -> None: """Initializer.""" self._stop = stop
@staticmethod - def formatted(text, options, default=None): + def formatted(text: str, options, default=None) -> str: """ Create a text with the options formatted into it.
@param text: Text into which options are to be formatted - @type text: str @param options: Option instances to be formatted @type options: Iterable @return: Text with the options formatted into it - @rtype: str """ formatted_options = [] for option in options: @@ -58,7 +54,7 @@ return fill(re.sub(pattern, '{}', text), width=77).format(*highlights)
@property - def stop(self): + def stop(self) -> bool: """Return whether this option stops asking.""" return self._stop
@@ -110,20 +106,19 @@
"""An option with a description and shortcut and returning the shortcut."""
- def __init__(self, option, shortcut, **kwargs): + def __init__(self, option: str, shortcut, **kwargs): """ Initializer.
@param option: option string - @type option: str @param shortcut: Shortcut of the option @type shortcut: str """ - super(StandardOption, self).__init__(**kwargs) + super().__init__(**kwargs) self.option = option self.shortcut = shortcut.lower()
- def format(self, default=None): + def format(self, default=None) -> str: """Return a formatted string for that option.""" index = self.option.lower().find(self.shortcut) shortcut = self.shortcut @@ -140,7 +135,7 @@ """Return the lowercased shortcut.""" return self.shortcut
- def test(self, value): + def test(self, value) -> bool: """Return True whether this option applies.""" return (self.shortcut.lower() == value.lower() or self.option.lower() == value.lower()) @@ -152,7 +147,7 @@
def __init__(self, option, shortcut, output, **kwargs): """Create a new option for the given sequence.""" - super(OutputProxyOption, self).__init__(option, shortcut, **kwargs) + super().__init__(option, shortcut, **kwargs) self._outputter = output
def output(self): @@ -171,14 +166,14 @@
def __init__(self, option, shortcut, description, options): """Initializer.""" - super(NestedOption, self).__init__(option, shortcut, stop=False) + super().__init__(option, shortcut, stop=False) self.description = description self.options = options
def format(self, default=None): """Return a formatted string for that option.""" self._output = Option.formatted(self.description, self.options) - return super(NestedOption, self).format(default=default) + return super().format(default=default)
def handled(self, value): """Return itself if it applies or the applying sub option.""" @@ -187,7 +182,7 @@ if handled is not None: return handled else: - return super(NestedOption, self).handled(value) + return super().handled(value)
def output(self): """Output the suboptions.""" @@ -202,7 +197,7 @@ self, option, shortcut, text, context, delta=100, start=0, end=0 ): """Initializer.""" - super(ContextOption, self).__init__(option, shortcut, stop=False) + super().__init__(option, shortcut, stop=False) self.text = text self.context = context self.delta = delta @@ -212,7 +207,7 @@ def result(self, value): """Add the delta to the context and output it.""" self.context += self.delta - super(ContextOption, self).result(value) + super().result(value)
def output(self): """Output the context.""" @@ -231,7 +226,7 @@
def __init__(self, option, shortcut, replacer): """Initializer.""" - super(Choice, self).__init__(option, shortcut) + super().__init__(option, shortcut) self._replacer = replacer
@property @@ -254,7 +249,7 @@
def __init__(self, option, shortcut, result): """Create instance with replacer set to None.""" - super(StaticChoice, self).__init__(option, shortcut, None) + super().__init__(option, shortcut, None) self._result = result
def handle(self): @@ -269,7 +264,7 @@ def __init__(self, option, shortcut, replacer, replace_section, replace_label): """Initializer.""" - super(LinkChoice, self).__init__(option, shortcut, replacer) + super().__init__(option, shortcut, replacer) self._section = replace_section self._label = replace_label
@@ -306,7 +301,7 @@
def __init__(self, replacer, option='always', shortcut='a'): """Initializer.""" - super(AlwaysChoice, self).__init__(option, shortcut, replacer) + super().__init__(option, shortcut, replacer) self.always = False
def handle(self): @@ -330,7 +325,7 @@
def __init__(self, minimum=1, maximum=None, prefix='', **kwargs): """Initializer.""" - super(IntegerOption, self).__init__(**kwargs) + super().__init__(**kwargs) if not ((minimum is None or isinstance(minimum, int)) and (maximum is None or isinstance(maximum, int))): raise ValueError( @@ -341,7 +336,7 @@ self._max = maximum self.prefix = prefix
- def test(self, value): + def test(self, value) -> bool: """Return whether the value is an int and in the specified range.""" try: value = self.parse(value) @@ -361,7 +356,7 @@ """Return the upper bound of the range of allowed values.""" return self._max
- def format(self, default=None): + def format(self, default=None) -> str: """Return a formatted string showing the range.""" if default is not None and self.test(default): value = self.parse(default) @@ -389,7 +384,7 @@ rng = 'any' + default return '{0}<number> [{1}]'.format(self.prefix, rng)
- def parse(self, value): + def parse(self, value) -> int: """Return integer from value with prefix removed.""" if value.lower().startswith(self.prefix.lower()): return int(value[len(self.prefix):]) @@ -409,7 +404,7 @@ """Initializer.""" self._list = sequence try: - super(ListOption, self).__init__(1, self.maximum, prefix, **kwargs) + super().__init__(1, self.maximum, prefix, **kwargs) except ValueError: raise ValueError('The sequence is empty.') del self._max @@ -419,10 +414,10 @@ if not self._list: raise ValueError('The sequence is empty.') else: - return super(ListOption, self).format(default=default) + return super().format(default=default)
@property - def maximum(self): + def maximum(self) -> int: """Return the maximum value.""" return len(self._list)
@@ -445,7 +440,7 @@ @param post: Additional comment printed after the list. @type post: str """ - super(ShowingListOption, self).__init__(sequence, prefix, **kwargs) + super().__init__(sequence, prefix, **kwargs) self.pre = pre self.post = post
@@ -469,7 +464,7 @@
"""An option to select multiple items from a list."""
- def test(self, value): + def test(self, value) -> bool: """Return whether the values are int and in the specified range.""" try: values = [self.parse(val) for val in value.split(',')] @@ -526,4 +521,4 @@
def __init__(self): """Constructor using the 'quit' ('q') in input_choice.""" - super(QuitKeyboardInterrupt, self).__init__('quit', 'q') + super().__init__('quit', 'q') diff --git a/pywikibot/config2.py b/pywikibot/config2.py index fe01116..49482d5 100644 --- a/pywikibot/config2.py +++ b/pywikibot/config2.py @@ -36,8 +36,6 @@ # # Distributed under the terms of the MIT license. # -from __future__ import absolute_import, division, unicode_literals - import collections import copy import os @@ -56,15 +54,12 @@
from pywikibot import __version__ as pwb_version from pywikibot.logging import error, output, warning -from pywikibot.tools import PY2, issue_deprecation_warning +from pywikibot.tools import issue_deprecation_warning
OSWIN32 = (sys.platform == 'win32')
if OSWIN32: - if not PY2: - import winreg - else: - import _winreg as winreg + import winreg
# Normalize old PYWIKIBOT2 environment variables and issue a deprecation warn. @@ -417,16 +412,10 @@ # be 'cp850' ('cp437' for older versions). Linux users might try 'iso-8859-1' # or 'utf-8'. # This default code should work fine, so you don't have to think about it. +# When using pywikibot inside a daemonized twisted application, we get +# "StdioOnnaStick instance has no attribute 'encoding'"; assign None instead. # TODO: consider getting rid of this config variable. -try: - if not PY2 or not sys.stdout.encoding: - console_encoding = sys.stdout.encoding - else: - console_encoding = sys.stdout.encoding.decode('ascii') -except AttributeError: - # When using pywikibot inside a daemonized twisted application, - # we get "StdioOnnaStick instance has no attribute 'encoding'" - console_encoding = None +console_encoding = getattr(sys.stdout, 'encoding', None)
# The encoding the user would like to see text transliterated to. This can be # set to a charset (e.g. 'ascii', 'iso-8859-1' or 'cp850'), and we will output diff --git a/pywikibot/daemonize.py b/pywikibot/daemonize.py index 677481a..41fb678 100644 --- a/pywikibot/daemonize.py +++ b/pywikibot/daemonize.py @@ -5,8 +5,6 @@ # # Distributed under the terms of the MIT license. # -from __future__ import absolute_import, division, unicode_literals - import codecs import os import stat