jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/615735 )
Change subject: [4.0] Remove Python 2 related code from tools.formatter.py
......................................................................
[4.0] Remove Python 2 related code from tools.formatter.py
- remove Python 2 code
- keep doc strings beneath 72 chars
- remove _vformat method because Python 3.5+ gives the right result
and a string as the first tuple component and it is not necessary
to override this internal method
- remove _convert_bytes which is not used anymore
- remove type checking in vformat which is done by _string.formatter_parser
- remove Python 2 code from reflinks.py which imported PY2 from
tools.formatter instead from tools
Change-Id: Ifb6c98ea77983a03d1840619e8bf8c705b7bdfe8
---
M pywikibot/tools/formatter.py
M scripts/reflinks.py
2 files changed, 38 insertions(+), 89 deletions(-)
Approvals:
Matěj Suchánek: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/tools/formatter.py b/pywikibot/tools/formatter.py
index ec90afa..7ee318b 100644
--- a/pywikibot/tools/formatter.py
+++ b/pywikibot/tools/formatter.py
@@ -1,36 +1,35 @@
# -*- coding: utf-8 -*-
"""Module containing various formatting related utilities."""
#
-# (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 math
from string import Formatter
+from typing import Sequence
from pywikibot.logging import output
-from pywikibot.tools import PY2, UnicodeType
from pywikibot.userinterfaces.terminal_interface_base import colors
-class SequenceOutputter(object):
+class SequenceOutputter:
- """
- A class formatting a list of items.
+ """A class formatting a list of items.
- It is possible to customize the appearance by changing C{format_string}
- which is used by C{str.format} with C{index}, C{width} and C{item}. Each
- line is joined by the separator and the complete text is surrounded by the
- prefix and the suffix. All three are by default a new line. The index
- starts at 1 and for the width it's using the width of the sequence's length
- written as a decimal number. So a length of 100 will result in a with of 3
- and a length of 99 in a width of 2.
+ It is possible to customize the appearance by changing
+ C{format_string} which is used by C{str.format} with C{index},
+ C{width} and C{item}. Each line is joined by the separator and the
+ complete text is surrounded by the prefix and the suffix. All three
+ are by default a new line. The index starts at 1 and for the width
+ it's using the width of the sequence's length written as a decimal
+ number. So a length of 100 will result in a with of 3 and a length
+ of 99 in a width of 2.
- It is iterating over C{self.sequence} to generate the text. That sequence
- can be any iterator but the result is better when it has an order.
+ It is iterating over C{self.sequence} to generate the text. That
+ sequence can be any iterator but the result is better when it has
+ an order.
"""
format_string = ' {index:>{width}} - {item}'
@@ -40,7 +39,7 @@
def __init__(self, sequence):
"""Create a new instance with a reference to the sequence."""
- super(SequenceOutputter, self).__init__()
+ super().__init__()
self.sequence = sequence
def format_list(self):
@@ -74,13 +73,12 @@
if key == 'color' and kwargs.get('color') in self.colors:
return '\03{{{0}}}'.format(kwargs[key])
else:
- return super(_ColorFormatter, self).get_value(key, args, kwargs)
+ return super().get_value(key, args, kwargs)
- def parse(self, format_string):
+ def parse(self, format_string: str):
"""Yield results similar to parse but skip colors."""
previous_literal = ''
- for literal, field, spec, conv in super(_ColorFormatter, self).parse(
- format_string):
+ for literal, field, spec, conv in super().parse(format_string):
if field in self.colors:
if spec:
raise ValueError(
@@ -106,73 +104,32 @@
if previous_literal:
yield previous_literal, None, None, None
- def _vformat(self, *args, **kwargs):
- """
- Override original `_vformat` to prevent that it changes into `bytes`.
-
- The original `_vformat` is returning `bytes` under certain
- circumstances. It happens when the `format_string` is empty, when there
- is no literal text around it or when the field value is not a `unicode`
- already.
-
- @rtype: str
- """
- result = super(_ColorFormatter, self)._vformat(*args, **kwargs)
- if isinstance(result, tuple):
- additional_params = result[1:]
- result = result[0]
- else:
- additional_params = ()
- result = self._convert_bytes(result)
- if additional_params:
- result = (result, ) + additional_params
- return result
-
- def _convert_bytes(self, result):
- """Convert everything into unicode."""
- if PY2 and isinstance(result, str):
- assert result == b''
- result = '' # This is changing it into a unicode
- elif not isinstance(result, UnicodeType):
- result = UnicodeType(result)
- return result
-
- def vformat(self, format_string, args, kwargs):
- """
- Return the normal format result but verify no colors are keywords.
+ def vformat(self, format_string: str, args: Sequence, kwargs: dict) -> str:
+ """Return the format result but verify no colors are keywords.
@param format_string: The format template string
- @type format_string: str
@param args: The positional field values
- @type args: typing.Sequence
@param kwargs: The named field values
- @type kwargs: dict
@return: The formatted string
- @rtype: str
"""
if self.colors.intersection(kwargs): # kwargs use colors
raise ValueError('Keyword argument(s) use valid color(s): '
+ '", "'.join(self.colors.intersection(kwargs)))
- if not isinstance(format_string, UnicodeType):
- raise TypeError('expected {0}, got {1}'
- .format(type(''), type(format_string)))
- return super(_ColorFormatter, self).vformat(format_string, args,
- kwargs)
+ return super().vformat(format_string, args, kwargs)
-def color_format(text, *args, **kwargs):
+def color_format(text: str, *args, **kwargs) -> str:
r"""
Do C{str.format} without having to worry about colors.
It is automatically adding \03 in front of color fields so it's
- unnecessary to add them manually. Any other \03 in the text is disallowed.
+ unnecessary to add them manually. Any other \03 in the text is
+ disallowed.
You may use a variant {color} by assigning a valid color to a named
parameter color.
@param text: The format template string
- @type text: str
@return: The formatted string
- @rtype: str
"""
return _ColorFormatter().format(text, *args, **kwargs)
diff --git a/scripts/reflinks.py b/scripts/reflinks.py
index 78d6913..76ae2ff 100755
--- a/scripts/reflinks.py
+++ b/scripts/reflinks.py
@@ -42,9 +42,8 @@
#
# Distributed under the terms of the MIT license.
#
-from __future__ import absolute_import, division, unicode_literals
-
import codecs
+import http.client as httplib
import os
import re
import socket
@@ -52,6 +51,9 @@
import tempfile
from functools import partial
+from urllib.error import URLError
+
+from requests import codes
import pywikibot
@@ -60,18 +62,10 @@
from pywikibot.pagegenerators import (
XMLDumpPageGenerator as _XMLDumpPageGenerator,
)
-from pywikibot.tools.formatter import color_format, PY2
-
-from requests import codes
+from pywikibot.tools.formatter import color_format
from scripts import noreferences
-if not PY2:
- import http.client as httplib
- from urllib.error import URLError
-else:
- import httplib
- from urllib2 import URLError
docuReplacements = {
'¶ms;': pagegenerators.parameterHelp
@@ -196,7 +190,7 @@
_XMLDumpPageGenerator, text_predicate=linksInRef.search)
-class RefLink(object):
+class RefLink:
"""Container to handle a single bare reference."""
@@ -238,7 +232,7 @@
self.title = re.sub(r'-+', '-', self.title)
# remove formatting, i.e long useless strings
self.title = re.sub(r'[\.+\-=]{4,}', ' ', self.title)
- # remove \n and \r and Unicode spaces from titles
+ # remove \n and \r and unicode spaces from titles
self.title = re.sub(r'(?u)\s', ' ', self.title)
self.title = re.sub(r'[\n\r\t]', ' ', self.title)
# remove extra whitespaces
@@ -272,11 +266,11 @@
nb_letter += 1
if letter.isdigit():
return
- if nb_upper / (nb_letter + 1) > .70:
+ if nb_upper / (nb_letter + 1) > 0.7:
self.title = self.title.title()
-class DuplicateReferences(object):
+class DuplicateReferences:
"""Helper to de-duplicate references in text.
@@ -407,7 +401,7 @@
'summary': None,
})
- super(ReferencesRobot, self).__init__(**kwargs)
+ super().__init__(**kwargs)
self.generator = generator
self.site = pywikibot.Site()
self._use_fake_user_agent = config.fake_user_agent_default.get(
@@ -760,7 +754,7 @@
actual_rev = self.stop_page.latest_revision_id
if actual_rev != self.stop_page_rev_id:
pywikibot.output(
- '{0} has been edited : Someone wants us to stop.'
+ '{} has been edited: Someone wants us to stop.'
.format(self.stop_page.title(as_link=True)))
return
@@ -786,10 +780,8 @@
for arg in local_args:
if arg.startswith('-summary:'):
options['summary'] = arg[9:]
- elif arg == '-always':
- options['always'] = True
- elif arg == '-ignorepdf':
- options['ignorepdf'] = True
+ elif arg in ('-always', '-ignorepdf'):
+ options[arg[1:]] = True
elif arg.startswith('-limit:'):
options['limit'] = int(arg[7:])
elif arg.startswith('-xmlstart'):
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/615735
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ifb6c98ea77983a03d1840619e8bf8c705b7bdfe8
Gerrit-Change-Number: 615735
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: D3r1ck01 <[email protected]>
Gerrit-Reviewer: Matěj Suchánek <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits