jenkins-bot has submitted this change and it was merged.
Change subject: Add Site login status to cache key
......................................................................
Add Site login status to cache key
Some API responses differ depending on the privileges of the user,
which can range from logged out anon to sysop. The cache needs to
include the login status in the key, so the cached response is not
reused for queries from a different privilege level.
For example, when the cache is empty and the user has never logged
into pywikibot, the unit tests cache site.userinfo with the details
of the logged out user, causing tests which require a logged-in user
to prompt for a password on the console many times per test, and the
site.userinfo is never updated. With this change, only one password
prompt appears and the result is cached.
Change-Id: I5e480be86a0b7b6a5907cf71690ba9ebed0b04b6
---
M pywikibot/data/api.py
M pywikibot/site.py
M tests/dry_api_tests.py
3 files changed, 67 insertions(+), 2 deletions(-)
Approvals:
John Vandenberg: Looks good to me, but someone else must approve
Merlijn van Deen: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index 157a99b..f39380b 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -462,7 +462,19 @@
pass
def _uniquedescriptionstr(self):
- return (repr(self.site) + repr(sorted(self.iteritems())))
+ login_status = self.site._loginstatus
+
+ if login_status > pywikibot.site.LoginStatus.NOT_LOGGED_IN and \
+ hasattr(self.site, '_userinfo') and \
+ 'name' in self.site._userinfo:
+ user_key = pywikibot.page.User(self.site,
+ self.site._userinfo['name'])
+ else:
+ user_key = pywikibot.site.LoginStatus(
+ max(login_status, pywikibot.site.LoginStatus.NOT_LOGGED_IN))
+
+ return (repr(self.site) + repr(user_key) +
+ repr(sorted(self.iteritems())))
def _create_file_name(self):
self.http_params() # normalize self.iteritems()
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 9306d78..e9823fa 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -69,6 +69,12 @@
raise KeyError("Value %r could not be found in this enum"
% search_value)
+ def __init__(self, state):
+ self.state = state
+
+ def __repr__(self):
+ return 'LoginStatus(%s)' % (LoginStatus.name(self.state))
+
def Family(fam=None, fatal=True):
"""Import the named family.
diff --git a/tests/dry_api_tests.py b/tests/dry_api_tests.py
index 8e0cd69..7d30ccc 100644
--- a/tests/dry_api_tests.py
+++ b/tests/dry_api_tests.py
@@ -10,7 +10,7 @@
import datetime
import pywikibot
from pywikibot.data.api import CachedRequest, QueryGenerator
-from tests.utils import unittest
+from utils import unittest
class DryAPITests(unittest.TestCase):
@@ -44,6 +44,53 @@
self.assertNotEqual(self.req._cachefile_path(), self.diffreq._cachefile_path())
self.assertNotEqual(self.req._cachefile_path(), self.diffsite._cachefile_path())
+ def test_cachefile_path_different_users(self):
+ # Mock basesite object to test this.
+ class MockSite(pywikibot.site.APISite):
+
+ _namespaces = {2: 'User'}
+
+ def __init__(self):
+ self._user = 'anon'
+
+ def user(self):
+ return self._user
+
+ def encoding(self):
+ return 'utf-8'
+
+ def encodings(self):
+ return []
+
+ def _getsiteinfo(self):
+ self._siteinfo = {'case': 'first-letter'}
+ return {}
+
+ def __repr__(self):
+ return "MockSite()"
+
+ def __getattr__(self, attr):
+ raise Exception("Attribute %r not defined" % attr)
+
+ site = MockSite()
+ req = CachedRequest(expiry=1, site=site, action='query', meta='siteinfo')
+ anonpath = req._cachefile_path()
+
+ site._userinfo = {'name': u'user'}
+ site._loginstatus = 0
+ req = CachedRequest(expiry=1, site=site, action='query', meta='siteinfo')
+ userpath = req._cachefile_path()
+
+ self.assertNotEqual(anonpath, userpath)
+
+ site._userinfo = {'name': u'sysop'}
+ site._loginstatus = 1
+ req = CachedRequest(expiry=1, site=site, action='query', meta='siteinfo')
+ sysoppath = req._cachefile_path()
+
+ self.assertNotEqual(anonpath, sysoppath)
+ self.assertNotEqual(userpath, sysoppath)
+
def test_expired(self):
self.assertFalse(self.req._expired(datetime.datetime.now()))
self.assertTrue(self.req._expired(datetime.datetime.now() - datetime.timedelta(days=2)))
--
To view, visit https://gerrit.wikimedia.org/r/132807
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I5e480be86a0b7b6a5907cf71690ba9ebed0b04b6
Gerrit-PatchSet: 4
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: jenkins-bot <>
jenkins-bot has submitted this change and it was merged.
Change subject: Cleanup basic.py
......................................................................
Cleanup basic.py
- Fix pyflakes and pep257
- Remove global site
- revise incorrect docstring for BasicBot.load()
Change-Id: I4be20e6ed4a47ce21ff12a1f7a8f019fa5ebcd79
---
M scripts/basic.py
1 file changed, 15 insertions(+), 10 deletions(-)
Approvals:
John Vandenberg: Looks good to me, but someone else must approve
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/basic.py b/scripts/basic.py
index e51c8c2..6850dc7 100755
--- a/scripts/basic.py
+++ b/scripts/basic.py
@@ -1,6 +1,8 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
+An incomplete sample script.
+
This is not a complete bot; rather, it is a template from which simple
bots can be made. You can rename it to mybot.py, then edit it in
whatever way you want.
@@ -35,13 +37,18 @@
class BasicBot:
+
+ """An incomplete sample bot."""
+
# Edit summary message that should be used is placed on /i18n subdirectory.
# The file containing these messages should have the same name as the caller
# script (i.e. basic.py in this case)
def __init__(self, generator, dry):
"""
- Constructor. Parameters:
+ Constructor.
+
+ Parameters:
@param generator: The page generator that determines on which pages
to work.
@type generator: generator.
@@ -49,20 +56,20 @@
what would have been changed.
@type dry: boolean.
"""
- site = pywikibot.Site()
self.generator = generator
self.dry = dry
+
# Set the edit summary message
+ site = pywikibot.Site()
self.summary = i18n.twtranslate(site, 'basic-changing')
def run(self):
+ """ Process each page from the generator. """
for page in self.generator:
self.treat(page)
def treat(self, page):
- """
- Loads the given page, does some changes, and saves it.
- """
+ """ Load the given page, does some changes, and saves it. """
text = self.load(page)
if not text:
return
@@ -79,9 +86,7 @@
pywikibot.output(u'Page %s not saved.' % page.title(asLink=True))
def load(self, page):
- """
- Loads the given page, does some changes, and saves it.
- """
+ """ Load the text of the given page. """
try:
# Load the page
text = page.get()
@@ -97,6 +102,7 @@
def save(self, text, page, comment=None, minorEdit=True,
botflag=True):
+ """ Update the given page with new text. """
# only save if something was changed
if text != page.get():
# Show the title of the page we're working on.
@@ -133,8 +139,7 @@
def main():
- global site
-
+ """ Process command line arguments and invoke BasicBot. """
# Process global arguments to determine desired site
local_args = pywikibot.handleArgs()
site = pywikibot.Site()
--
To view, visit https://gerrit.wikimedia.org/r/141369
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I4be20e6ed4a47ce21ff12a1f7a8f019fa5ebcd79
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Alex S.H. Lin <alexsh(a)mail2000.com.tw>
Gerrit-Reviewer: Guoguo12 <Guoguo12(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Mdupont <jamesmikedupont(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Nullzero <nullzero.free(a)gmail.com>
Gerrit-Reviewer: Pyfisch <pyfisch(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>
jenkins-bot has submitted this change and it was merged.
Change subject: Remove config.google_key
......................................................................
Remove config.google_key
Follow up patch after Ieaa92eead313a7948879c7668c46fbf15eeb396d
config will emit a warning of the unknown variable 'google_key'.
Bug 65966
Change-Id: I9db2dd675441af248f1584db279e07ab031e7d22
---
M pywikibot/config2.py
1 file changed, 0 insertions(+), 6 deletions(-)
Approvals:
John Vandenberg: Looks good to me, but someone else must approve
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/config2.py b/pywikibot/config2.py
index b13b5aa..c79dd80 100644
--- a/pywikibot/config2.py
+++ b/pywikibot/config2.py
@@ -458,12 +458,6 @@
# ############# SEARCH ENGINE SETTINGS ##############
-# Some scripts allow querying Google via the Google Web API. To use this
-# feature, you must install the pyGoogle module from http://pygoogle.sourceforge.net
-# and have a Google Web API license key. Note that Google doesn't give out
-# license keys anymore.
-google_key = ''
-
# Some scripts allow using the Yahoo! Search Web Services. To use this feature,
# you must install the pYsearch module from http://pysearch.sourceforge.net
# and get a Yahoo AppID from https://developer.yahoo.com/
--
To view, visit https://gerrit.wikimedia.org/r/141945
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I9db2dd675441af248f1584db279e07ab031e7d22
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>
jenkins-bot has submitted this change and it was merged.
Change subject: Remove unused imports, methods and variables
......................................................................
Remove unused imports, methods and variables
The method load was copied from basic.py in the initial port from compat,
and is unused as the bot does page checking in treat().
Likewise import of config was copied from basic.py.
main() variable checkcurrent is set but was not used. Moved a block of code.
Change-Id: I97b83965bd08a8ce156634cea30841b0174a3ba7
---
M scripts/commonscat.py
1 file changed, 10 insertions(+), 24 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
Guoguo12: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/scripts/commonscat.py b/scripts/commonscat.py
index de28ab2..51c7dcc 100755
--- a/scripts/commonscat.py
+++ b/scripts/commonscat.py
@@ -67,7 +67,6 @@
import add_text
import pywikibot
-from pywikibot import config
from pywikibot import pagegenerators
docuReplacements = {
@@ -263,20 +262,6 @@
% page.title(asLink=True))
else:
self.addCommonscat(page)
-
- def load(self, page):
- """ Load the given page, do some changes, and save it. """
- try:
- text = page.get()
- except pywikibot.NoPage:
- pywikibot.output(u"Page %s does not exist; skipping."
- % page.title(asLink=True))
- except pywikibot.IsRedirectPage:
- pywikibot.output(u"Page %s is a redirect; skipping."
- % page.title(asLink=True))
- else:
- return text
- return None
def save(self, text, page, comment, minorEdit=True, botflag=True):
# only save if something was changed
@@ -572,20 +557,21 @@
summary = arg[9:]
elif arg.startswith('-checkcurrent'):
checkcurrent = True
- primaryCommonscat, commonscatAlternatives = \
- CommonscatBot.getCommonscatTemplate(
- pywikibot.Site().code)
- generator = pagegenerators.NamespaceFilterPageGenerator(
- pagegenerators.ReferringPageGenerator(
- pywikibot.Page(pywikibot.Site(),
- u'Template:' + primaryCommonscat),
- onlyTemplateInclusion=True), ns)
-
elif arg == '-always':
always = True
else:
genFactory.handleArg(arg)
+ if checkcurrent:
+ primaryCommonscat, commonscatAlternatives = \
+ CommonscatBot.getCommonscatTemplate(
+ pywikibot.Site().code)
+ generator = pagegenerators.NamespaceFilterPageGenerator(
+ pagegenerators.ReferringPageGenerator(
+ pywikibot.Page(pywikibot.Site(),
+ u'Template:' + primaryCommonscat),
+ onlyTemplateInclusion=True), ns)
+
if not generator:
generator = genFactory.getCombinedGenerator()
if not generator:
--
To view, visit https://gerrit.wikimedia.org/r/141368
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I97b83965bd08a8ce156634cea30841b0174a3ba7
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Guoguo12 <Guoguo12(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: devunt <devunt(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>