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 <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/612572 )
Change subject: [IMPR] use defaultdict in wikistats.py
......................................................................
[IMPR] use defaultdict in wikistats.py
Change-Id: I84253c44d24f414b370074d481e3c5fc8735bebf
---
M pywikibot/data/wikistats.py
1 file changed, 9 insertions(+), 16 deletions(-)
Approvals:
D3r1ck01: Looks good to me, but someone else must approve
Matěj Suchánek: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/wikistats.py b/pywikibot/data/wikistats.py
index 19d5c7c..5c2f0f4 100644
--- a/pywikibot/data/wikistats.py
+++ b/pywikibot/data/wikistats.py
@@ -6,6 +6,7 @@
# Distributed under the terms of the MIT license.
import csv
+from collections import defaultdict
from io import BytesIO, StringIO
import pywikibot
@@ -74,8 +75,8 @@
def __init__(self, url='https://wikistats.wmflabs.org/') -> None:
"""Initializer."""
self.url = url
- self._raw = {}
- self._data = {}
+ self._raw = defaultdict(dict)
+ self._data = defaultdict(dict)
def fetch(self, table: str, format='xml'):
"""
@@ -110,13 +111,10 @@
@type format: 'xml' or 'csv'.
@rtype: bytes
"""
- if format not in self._raw:
- self._raw[format] = {}
if table in self._raw[format]:
return self._raw[format][table]
data = self.fetch(table, format)
-
self._raw[format][table] = data
return data
@@ -127,13 +125,11 @@
@param table: table of data to fetch
@rtype: list
"""
- if table in self._data.setdefault('csv', {}):
+ if table in self._data['csv']:
return self._data['csv'][table]
- data = self.raw_cached(table, 'csv')
-
- f = StringIO(data.decode('utf8'))
-
+ raw = self.raw_cached(table, 'csv')
+ f = StringIO(raw.decode('utf8'))
reader = csv.DictReader(f)
data = list(reader)
self._data['csv'][table] = data
@@ -147,18 +143,16 @@
@param table: table of data to fetch
@rtype: list
"""
- if table in self._data.setdefault('xml', {}):
+ if table in self._data['xml']:
return self._data['xml'][table]
from xml.etree import ElementTree
- data = self.raw_cached(table, 'xml')
-
- f = BytesIO(data)
+ raw = self.raw_cached(table, 'xml')
+ f = BytesIO(raw)
tree = ElementTree.parse(f)
data = []
-
for row in tree.findall('row'):
site = {}
@@ -169,7 +163,6 @@
data.append(site)
self._data['xml'][table] = data
-
return data
def get(self, table: str, format='csv'):
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/612572
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: I84253c44d24f414b370074d481e3c5fc8735bebf
Gerrit-Change-Number: 612572
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Dvorapa <dvorapa(a)seznam.cz>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/618780 )
Change subject: [tests] use en-wiki instead of test2 for RepeatingGenerator tests
......................................................................
[tests] use en-wiki instead of test2 for RepeatingGenerator tests
This solves T259810 and also the failing EventStreamsPageGeneratorTestCase
test due to low recent changes during 5 minutes timeout time
Bug: T259810
Change-Id: I6cd5ca2edc4ffbda693d1682c9c130ba1e111725
---
M tests/aspects.py
1 file changed, 1 insertion(+), 1 deletion(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/aspects.py b/tests/aspects.py
index b8e5470..24fb72c 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -1427,7 +1427,7 @@
super().setUpClass()
- if cls.get_site().code == 'test':
+ if cls.get_site().code in ('test', 'test2'):
cls.override_default_site(pywikibot.Site('en', 'wikipedia'))
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/618780
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: I6cd5ca2edc4ffbda693d1682c9c130ba1e111725
Gerrit-Change-Number: 618780
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/612604 )
Change subject: [4.0] remove Python 2 code parts of win32_unicode.py
......................................................................
[4.0] remove Python 2 code parts of win32_unicode.py
Change-Id: I71b98a5ec6d49f72badd469b8007ed64205bf6eb
---
M pywikibot/userinterfaces/win32_unicode.py
1 file changed, 7 insertions(+), 57 deletions(-)
Approvals:
Huji: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/userinterfaces/win32_unicode.py b/pywikibot/userinterfaces/win32_unicode.py
index bdada00..e03d861 100755
--- a/pywikibot/userinterfaces/win32_unicode.py
+++ b/pywikibot/userinterfaces/win32_unicode.py
@@ -22,18 +22,14 @@
# Licensed under both CC-BY-SA and the MIT license.
#
################################################
-from __future__ import (absolute_import, division,
- print_function, unicode_literals)
-
import codecs
import sys
-from ctypes import Structure, byref, c_int, create_unicode_buffer, sizeof
+from contextlib import suppress
+from ctypes import Structure, byref, create_unicode_buffer, sizeof
from ctypes import c_void_p as LPVOID
from io import IOBase, UnsupportedOperation
-from pywikibot.tools import PY2, UnicodeType
-
OSWIN32 = (sys.platform == 'win32')
stdin = sys.stdin
@@ -45,7 +41,7 @@
if OSWIN32:
from ctypes import WINFUNCTYPE, windll, POINTER, WinError
- from ctypes.wintypes import (BOOL, DWORD, HANDLE, LPCWSTR, LPWSTR,
+ from ctypes.wintypes import (BOOL, DWORD, HANDLE, LPWSTR,
SHORT, ULONG, UINT, WCHAR)
try:
@@ -77,11 +73,7 @@
byref(numrecv), None)
if not result:
raise Exception('stdin failure')
- data = self.buffer.value[:numrecv.value]
- if PY2:
- return data.encode(self.encoding)
- else:
- return data
+ return self.buffer.value[:numrecv.value]
class UnicodeOutput(IOBase):
@@ -117,11 +109,9 @@
"""Write the text to the output."""
try:
if self._hConsole is None:
- if PY2 and isinstance(text, UnicodeType):
- text = text.encode('utf-8')
self._stream.write(text)
else:
- if not isinstance(text, UnicodeType):
+ if not isinstance(text, str):
text = bytes(text).decode('utf-8')
remaining = len(text)
while remaining > 0:
@@ -159,10 +149,9 @@
# handle those like std streams which don't have fileno at all
std = getattr(sys, 'std{0}'.format(std_name))
if hasattr(std, 'fileno'):
- try:
+ with suppress(UnsupportedOperation):
return std.fileno()
- except UnsupportedOperation:
- pass
+ return None
# If any exception occurs in this code, try to print it on stderr,
@@ -332,45 +321,6 @@
_complain('exception {!r} while fixing up sys.stdout and sys.stderr'
.format(e))
- if not PY2:
- # no need to work around issue2128 since it's a Python 2 only issue
- return stdin, stdout, stderr, argv
-
- # While we're at it, let's unmangle the command-line arguments:
-
- # This works around <http://bugs.python.org/issue2128>.
- GetCommandLineW = WINFUNCTYPE(LPWSTR)(('GetCommandLineW', windll.kernel32))
- CommandLineToArgvW = (WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))
- (('CommandLineToArgvW', windll.shell32)))
-
- argc = c_int(0)
- argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc))
-
- argv = [argv_unicode[i].encode('utf-8') for i in range(0, argc.value)]
-
- if not hasattr(sys, 'frozen'):
- # If this is an executable produced by py2exe or bbfreeze, then it will
- # have been invoked directly. Otherwise, unicode_argv[0] is the Python
- # interpreter, so skip that.
- argv = argv[1:]
-
- # Also skip option arguments to the Python interpreter.
- while len(argv) > 0:
- arg = argv[0]
- if not arg.startswith(b'-') or arg == '-':
- break
- argv = argv[1:]
- if arg == '-m':
- # sys.argv[0] should really be the absolute path of the module
- # source, but never mind
- break
- if arg == '-c':
- argv[0] = '-c'
- break
-
- if argv == []:
- argv = ['']
-
return stdin, stdout, stderr, argv
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/612604
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: I71b98a5ec6d49f72badd469b8007ed64205bf6eb
Gerrit-Change-Number: 612604
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Huji <huji.huji(a)gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged