jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/620371 )
Change subject: [4.0] Remove Python 2 code in several scripts and make other improvements ......................................................................
[4.0] Remove Python 2 code in several scripts and make other improvements
Change-Id: I1f8a22195be4806a6bd23d56e06c534726476400 --- M pywikibot/fixes.py M pywikibot/throttle.py M pywikibot/titletranslate.py M pywikibot/tools/djvu.py M pywikibot/xmlreader.py 5 files changed, 36 insertions(+), 67 deletions(-)
Approvals: Framawiki: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/fixes.py b/pywikibot/fixes.py index 4db13d0..ecfd4c8 100644 --- a/pywikibot/fixes.py +++ b/pywikibot/fixes.py @@ -5,8 +5,6 @@ # # Distributed under the terms of the MIT license. # -from __future__ import absolute_import, division, unicode_literals - import os.path
from pywikibot import config @@ -37,7 +35,7 @@ syntax. """
-__doc__ = __doc__ + parameter_help +__doc__ += parameter_help
fixes = { # These replacements will convert HTML to wiki syntax where possible, and @@ -684,13 +682,10 @@ with open(filename, 'rb') as f: exec(compile(f.read(), filename, 'exec'), globals()) return True - else: - return False + + return False
# Load the user fixes file. filename = config.datafilepath('user-fixes.py') -if _load_file(filename): - user_fixes_loaded = True -else: - user_fixes_loaded = False +user_fixes_loaded = _load_file(filename) diff --git a/pywikibot/throttle.py b/pywikibot/throttle.py index 91b3318..6b5ce1f 100644 --- a/pywikibot/throttle.py +++ b/pywikibot/throttle.py @@ -5,18 +5,20 @@ # # Distributed under the terms of the MIT license. # -from __future__ import absolute_import, division, unicode_literals - import math import threading import time
+from contextlib import suppress + import pywikibot from pywikibot import config from pywikibot.tools import deprecated
_logger = 'wiki.throttle'
+FORMAT_LINE = '{pid} {time} {site}\n' + # global process identifier # # When the first Throttle is instantiated, it will set this variable to a @@ -25,7 +27,7 @@ pid = False
-class Throttle(object): +class Throttle:
"""Control rate of access to wiki server.
@@ -76,7 +78,7 @@ self.setDelays()
@property - @deprecated(since='20180423') + @deprecated(since='20180423', future_warning=True) def lastwait(self): """DEPRECATED property.""" return 0.0 @@ -85,7 +87,7 @@ """Count running processes for site and set process_multiplicity.""" global pid mysite = self.mysite - pywikibot.debug('Checking multiplicity: pid = %(pid)s' % globals(), + pywikibot.debug('Checking multiplicity: pid = {pid}'.format(pid=pid), _logger) with self.lock: processes = [] @@ -130,18 +132,12 @@ 'time': self.checktime, 'site': mysite}) processes.sort(key=lambda p: (p['pid'], p['site'])) - try: - f = open(self.ctrlfilename, 'w') + with suppress(IOError), open(self.ctrlfilename, 'w') as f: for p in processes: - f.write('%(pid)s %(time)s %(site)s\n' % p) - except IOError: - pass - else: - f.close() + f.write(FORMAT_LINE.format_map(p)) self.process_multiplicity = count - pywikibot.log( - 'Found {0} {1} processes running, including this one.'.format( - count, mysite)) + pywikibot.log('Found {} {} processes running, including this one.' + .format(count, mysite))
def setDelays(self, delay=None, writedelay=None, absolute=False): """Set the nominal delays in seconds. Defaults to config values.""" @@ -189,15 +185,8 @@ # delay this time thisdelay = self.getDelay(write=write) now = time.time() - if write: - ago = now - self.last_write - else: - ago = now - self.last_read - if ago < thisdelay: - delta = thisdelay - ago - return delta - else: - return 0.0 + ago = now - (self.last_write if write else self.last_read) + return max(0.0, thisdelay - ago)
def drop(self): """Remove me from the list of running bot processes.""" @@ -227,27 +216,23 @@ 'site': this_site})
processes.sort(key=lambda p: p['pid']) - try: - with open(self.ctrlfilename, 'w') as f: - for p in processes: - f.write('%(pid)s %(time)s %(site)s\n' % p) - except IOError: - return + with suppress(IOError), open(self.ctrlfilename, 'w') as f: + for p in processes: + f.write(FORMAT_LINE.format_map(p))
def wait(self, seconds): """Wait for seconds seconds.
Announce the delay if it exceeds a preset limit. - """ if seconds <= 0: return
- message = ('Sleeping for %(seconds).1f seconds, %(now)s' % { - 'seconds': seconds, - 'now': time.strftime('%Y-%m-%d %H:%M:%S', - time.localtime()) - }) + message = 'Sleeping for {seconds:.1f} seconds, {now}' \ + .format_map({ + 'seconds': seconds, + 'now': time.strftime('%Y-%m-%d %H:%M:%S', + time.localtime())}) if seconds > config.noisysleep: pywikibot.output(message) else: diff --git a/pywikibot/titletranslate.py b/pywikibot/titletranslate.py index 3753608..5b02c2d 100644 --- a/pywikibot/titletranslate.py +++ b/pywikibot/titletranslate.py @@ -1,12 +1,10 @@ # -*- coding: utf-8 -*- """Title translate module.""" # -# (C) Pywikibot team, 2003-2018 +# (C) Pywikibot team, 2003-2020 # # Distributed under the terms of the MIT license. # -from __future__ import absolute_import, division, unicode_literals - import pywikibot from pywikibot import date
@@ -16,7 +14,7 @@
@deprecated_args(family=None) def translate(page=None, hints=(), auto=True, removebrackets=False, - site=None): + site=None) -> list: """ Return a list of links to pages on other sites based on hints.
@@ -31,8 +29,7 @@
assert page or site
- if site is None: - site = page.site + site = site or page.site
for h in hints: # argument may be given as -hint:xy where xy is a language code @@ -74,8 +71,8 @@ dict_name, value = date.getAutoFormat(sitelang, page.title()) if dict_name: pywikibot.output( - 'TitleTranslate: %s was recognized as %s with value %d' - % (page.title(), dict_name, value)) + 'TitleTranslate: {} was recognized as {} with value {}' + .format(page.title(), dict_name, value)) for entry_lang, entry in date.formats[dict_name].items(): if entry_lang not in site.languages(): continue diff --git a/pywikibot/tools/djvu.py b/pywikibot/tools/djvu.py index cf9f254..f13436c 100644 --- a/pywikibot/tools/djvu.py +++ b/pywikibot/tools/djvu.py @@ -6,31 +6,26 @@ # # Distributed under the terms of the MIT license. # -from __future__ import absolute_import, division, unicode_literals - -from collections import Counter import os import re import subprocess
+from collections import Counter + import pywikibot
from pywikibot.tools import deprecated, deprecated_args
-def _call_cmd(args, lib='djvulibre'): +def _call_cmd(args, lib='djvulibre') -> tuple: """ Tiny wrapper around subprocess.Popen().
@param args: same as Popen() @type args: str or typing.Sequence[string]
- @param library: library to be logged in logging messages - @type library: str - - @param log: log process output; errors are always logged. - @type library: bool - + @param lib: library to be logged in logging messages + @type lib: str
@return: returns a tuple (res, stdoutdata), where res is True if dp.returncode != 0 else False diff --git a/pywikibot/xmlreader.py b/pywikibot/xmlreader.py index 9c1f883..c4f8424 100644 --- a/pywikibot/xmlreader.py +++ b/pywikibot/xmlreader.py @@ -13,15 +13,12 @@ # # Distributed under the terms of the MIT license. # -from __future__ import absolute_import, division, unicode_literals - import re import threading +import xml.sax
from xml.etree.ElementTree import iterparse
-import xml.sax - from pywikibot.tools import open_archive
pywikibot-commits@lists.wikimedia.org