jenkins-bot merged this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[cleanup] clean scripts/patrol.py

- remove preleading "u" from strings
- use single quotes for string literals and double quotes
**only** if they consist of single quotes within them
- use str.format(...) instead of modulo for type specifier
arguments
- use "+" to concatenate strings in some cases

Change-Id: I73fa2def2842bf2005f0b751557fcaafc370721a
---
M scripts/patrol.py
1 file changed, 52 insertions(+), 52 deletions(-)

diff --git a/scripts/patrol.py b/scripts/patrol.py
index 3c0e8a0..cbbcec5 100755
--- a/scripts/patrol.py
+++ b/scripts/patrol.py
@@ -80,7 +80,7 @@

# Localised name of the whitelist page
whitelist_subpage_name = {
- 'en': u'patrol_whitelist',
+ 'en': 'patrol_whitelist',
}

def __init__(self, site=True, **kwargs):
@@ -108,10 +108,9 @@
else:
local_whitelist_subpage_name = pywikibot.translate(
self.site, self.whitelist_subpage_name, fallback=True)
- self.whitelist_pagename = u'%s:%s/%s' % (
- self.site.namespace(2),
- self.site.username(),
- local_whitelist_subpage_name)
+ self.whitelist_pagename = '{0}:{1}/{2}'.format(
+ self.site.namespace(2), self.site.username(),
+ local_whitelist_subpage_name)
self.whitelist = None
self.whitelist_ts = 0
self.whitelist_load_ts = 0
@@ -136,14 +135,14 @@
if (self.whitelist_load_ts and (time.time() - self.whitelist_load_ts <
self.getOption('versionchecktime'))):
if pywikibot.config.verbose_output:
- pywikibot.output(u'Whitelist not stale yet')
+ pywikibot.output('Whitelist not stale yet')
return

whitelist_page = pywikibot.Page(self.site,
self.whitelist_pagename)

if not self.whitelist:
- pywikibot.output(u'Loading %s' % self.whitelist_pagename)
+ pywikibot.output('Loading ' + self.whitelist_pagename)

try:
if self.whitelist_ts:
@@ -155,11 +154,11 @@
# it has been effectively reloaded 'now'
self.whitelist_load_ts = time.time()
if pywikibot.config.verbose_output:
- pywikibot.output(u'Whitelist not modified')
+ pywikibot.output('Whitelist not modified')
return

if self.whitelist:
- pywikibot.output(u'Reloading whitelist')
+ pywikibot.output('Reloading whitelist')

# Fetch whitelist
wikitext = whitelist_page.get()
@@ -172,12 +171,12 @@
# cascade if there isnt a whitelist to fallback on
if not self.whitelist:
raise
- pywikibot.error(u'%s' % e)
+ pywikibot.error('{0}'.format(e))

def in_list(self, pagelist, title):
"""Check if title present in pagelist."""
if pywikibot.config.verbose_output:
- pywikibot.output(u'Checking whitelist for: %s' % title)
+ pywikibot.output('Checking whitelist for: ' + title)

# quick check for exact match
if title in pagelist:
@@ -186,16 +185,16 @@
# quick check for wildcard
if '' in pagelist:
if pywikibot.config.verbose_output:
- pywikibot.output(u'wildcarded')
+ pywikibot.output('wildcarded')
return '.*'

for item in pagelist:
if pywikibot.config.verbose_output:
- pywikibot.output('checking against whitelist item = %s' % item)
+ pywikibot.output('checking against whitelist item = ' + item)

if isinstance(item, LinkedPagesRule):
if pywikibot.config.verbose_output:
- pywikibot.output(u'invoking programmed rule')
+ pywikibot.output('invoking programmed rule')
if item.match(title):
return item

@@ -203,7 +202,7 @@
return item

if pywikibot.config.verbose_output:
- pywikibot.output(u'not found')
+ pywikibot.output('not found')

def parse_page_tuples(self, wikitext, user=None):
"""Parse page details apart from 'user:' for use."""
@@ -234,13 +233,13 @@
if name.lower() in self._prefixindex_aliases:
if not prefix:
if pywikibot.config.verbose_output:
- pywikibot.output(u'Whitelist everything')
+ pywikibot.output('Whitelist everything')
page = ''
else:
page = prefix
if pywikibot.config.verbose_output:
- pywikibot.output(u'Whitelist prefixindex hack '
- u'for: %s' % page)
+ pywikibot.output('Whitelist prefixindex hack '
+ 'for: ' + page)
# p = pywikibot.Page(self.site, obj.target[20:])
# obj.namespace = p.namespace
# obj.target = p.title()
@@ -251,7 +250,7 @@
# the user will be the target of subsequent rules
current_user = obj.title
if pywikibot.config.verbose_output:
- pywikibot.output(u'Whitelist user: %s' % current_user)
+ pywikibot.output('Whitelist user: ' + current_user)
continue
else:
page = obj.canonical_title()
@@ -260,20 +259,20 @@
if not user or current_user == user:
if self.is_wikisource_author_page(page):
if pywikibot.config.verbose_output:
- pywikibot.output('Whitelist author: %s' % page)
+ pywikibot.output('Whitelist author: ' + page)
page = LinkedPagesRule(page)
else:
if pywikibot.config.verbose_output:
- pywikibot.output(u'Whitelist page: %s' % page)
+ pywikibot.output('Whitelist page: ' + page)
if pywikibot.config.verbose_output:
pywikibot.output('Adding {0}:{1}'
.format(current_user, page))
whitelist[current_user].append(page)
elif pywikibot.config.verbose_output:
- pywikibot.output(u'Discarding whitelist page for '
- u'another user: %s' % page)
+ pywikibot.output('Discarding whitelist page for '
+ 'another user: ' + page)
else:
- raise Exception(u'No user set for page %s' % page)
+ raise Exception('No user set for page ' + page)

return dict(whitelist)

@@ -289,12 +288,12 @@
pass
if author_ns:
author_ns_prefix = self.site.namespace(author_ns)
- pywikibot.debug(u'Author ns: %d; name: %s'
- % (author_ns, author_ns_prefix), _logger)
+ pywikibot.debug('Author ns: {0}; name: {1}'
+ .format(author_ns, author_ns_prefix), _logger)
if title.find(author_ns_prefix + ':') == 0:
if pywikibot.config.verbose_output:
author_page_name = title[len(author_ns_prefix) + 1:]
- pywikibot.output(u'Found author %s' % author_page_name)
+ pywikibot.output('Found author ' + author_page_name)
return True

def run(self, feed=None):
@@ -332,23 +331,23 @@
self.repeat_start_ts = time.time()

if pywikibot.config.verbose_output or self.getOption('ask'):
- pywikibot.output(u'User %s has created or modified page %s'
- % (username, title))
+ pywikibot.output('User {0} has created or modified page {1}'
+ .format(username, title))

if self.getOption('autopatroluserns') and (page['ns'] == 2 or
page['ns'] == 3):
# simple rule to whitelist any user editing their own userspace
if title.partition(':')[2].split('/')[0].startswith(username):
if pywikibot.config.verbose_output:
- pywikibot.output(u'%s is whitelisted to modify %s'
- % (username, title))
+ pywikibot.output('{0} is whitelisted to modify {1}'
+ .format(username, title))
choice = True

if not choice and username in self.whitelist:
if self.in_list(self.whitelist[username], title):
if pywikibot.config.verbose_output:
- pywikibot.output(u'%s is whitelisted to modify %s'
- % (username, title))
+ pywikibot.output('{0} is whitelisted to modify {1}'
+ .format(username, title))
choice = True

if self.getOption('ask'):
@@ -360,11 +359,11 @@
# list() iterates over patrol() which returns a generator
list(self.site.patrol(rcid))
self.patrol_counter = self.patrol_counter + 1
- pywikibot.output(u'Patrolled %s (rcid %d) by user %s'
- % (title, rcid, username))
+ pywikibot.output('Patrolled {0} (rcid {1}) by user {2}'
+ .format(title, rcid, username))
else:
if pywikibot.config.verbose_output:
- pywikibot.output(u'Skipped')
+ pywikibot.output('Skipped')

if rcid > self.highest_rcid:
self.highest_rcid = rcid
@@ -372,20 +371,20 @@
self.rc_item_counter = self.rc_item_counter + 1

except pywikibot.NoPage:
- pywikibot.output(u'Page %s does not exist; skipping.'
- % title(as_link=True))
+ pywikibot.output('Page {0} does not exist; skipping.'
+ .format(title(as_link=True)))
except pywikibot.IsRedirectPage:
- pywikibot.output(u'Page %s is a redirect; skipping.'
- % title(as_link=True))
+ pywikibot.output('Page {0} is a redirect; skipping.'
+ .format(title(as_link=True)))


def title_match(prefix, title):
"""Match title substring with given prefix."""
if pywikibot.config.verbose_output:
- pywikibot.output(u'Matching %s to prefix %s' % (title, prefix))
+ pywikibot.output('Matching {0} to prefix {1}'.format(title, prefix))
if title.startswith(prefix):
if pywikibot.config.verbose_output:
- pywikibot.output(u'substr match')
+ pywikibot.output('substr match')
return True
return

@@ -418,7 +417,7 @@

if not self.linkedpages:
if pywikibot.config.verbose_output:
- pywikibot.output(u'loading page links on %s' % self.page_title)
+ pywikibot.output('loading page links on ' + self.page_title)
p = pywikibot.Page(self.site, self.page_title)
linkedpages = []
for linkedpage in p.linkedPages():
@@ -426,14 +425,15 @@

self.linkedpages = linkedpages
if pywikibot.config.verbose_output:
- pywikibot.output(u'Loaded %d page links' % len(linkedpages))
+ pywikibot.output('Loaded {0} page links'
+ .format(len(linkedpages)))

for p in self.linkedpages:
if pywikibot.config.verbose_output:
- pywikibot.output(u"Checking against '%s'" % p)
+ pywikibot.output("Checking against '{0}'".format(p))
if title_match(p, page_title):
if pywikibot.config.verbose_output:
- pywikibot.output(u'Matched.')
+ pywikibot.output('Matched.')
return p


@@ -452,7 +452,7 @@
else:
yield page[1]
if repeat:
- pywikibot.output(u'Sleeping for %d seconds' % delay)
+ pywikibot.output('Sleeping for {0} seconds'.format(delay))
pywikibot.sleep(delay)
else:
break
@@ -488,7 +488,7 @@
elif arg.startswith('-versionchecktime:'):
versionchecktime = arg[len('-versionchecktime:'):]
options['versionchecktime'] = int(versionchecktime)
- elif arg.startswith("-whitelist:"):
+ elif arg.startswith('-whitelist:'):
options['whitelist'] = arg[len('-whitelist:'):]
else:
generator = gen_factory.handleArg(arg)
@@ -518,7 +518,7 @@
bot = PatrolBot(**options)

if newpages or usercontribs:
- pywikibot.output(u'Newpages:')
+ pywikibot.output('Newpages:')
gen = site.newpages
feed = api_feed_repeater(gen, delay=60, repeat=repeat,
user=usercontribs,
@@ -527,15 +527,15 @@
bot.run(feed)

if recentchanges or usercontribs:
- pywikibot.output(u'Recentchanges:')
+ pywikibot.output('Recentchanges:')
gen = site.recentchanges
feed = api_feed_repeater(gen, delay=60, repeat=repeat,
namespaces=gen_factory.namespaces,
user=usercontribs)
bot.run(feed)

- pywikibot.output(u'%d/%d patrolled'
- % (bot.patrol_counter, bot.rc_item_counter))
+ pywikibot.output('{0}/{1} patrolled'
+ .format(bot.patrol_counter, bot.rc_item_counter))


if __name__ == '__main__':

To view, visit change 462446. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I73fa2def2842bf2005f0b751557fcaafc370721a
Gerrit-Change-Number: 462446
Gerrit-PatchSet: 3
Gerrit-Owner: D3r1ck01 <alangiderick@gmail.com>
Gerrit-Reviewer: D3r1ck01 <alangiderick@gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot (75)