jenkins-bot merged this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] LogDict: Raise HiddenKeyError when a key is hidden

When part of a log event (key) is hidden 'actionhidden', 'commenthidden',
and/or 'userhidden' keys are included in the API response. If the desired
key is missing and the corresponsing hidden key is present, then raise
HiddenKeyError.

Bug: T187635
Change-Id: Iba46f5b17057ce825d6d1a94d9ccacb2a6a051dc
---
M pywikibot/exceptions.py
M pywikibot/logentries.py
M tests/logentry_tests.py
3 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/pywikibot/exceptions.py b/pywikibot/exceptions.py
index 929c1a4..07d47b7 100644
--- a/pywikibot/exceptions.py
+++ b/pywikibot/exceptions.py
@@ -542,6 +542,13 @@
pass


+class HiddenKeyError(UserRightsError, KeyError):
+
+ """Insufficient user rights to view the hidden key."""
+
+ pass
+
+
class NotEmailableError(PageRelatedError):

"""This user is not emailable."""
diff --git a/pywikibot/logentries.py b/pywikibot/logentries.py
index c12ff73..01f2246 100644
--- a/pywikibot/logentries.py
+++ b/pywikibot/logentries.py
@@ -10,7 +10,7 @@
import sys

import pywikibot
-from pywikibot.exceptions import Error
+from pywikibot.exceptions import Error, HiddenKeyError
from pywikibot.tools import deprecated, classproperty

if sys.version_info[0] > 2:
@@ -22,7 +22,10 @@
class LogDict(dict):

"""
- Simple custom dict that raises a custom KeyError when a key is missing.
+ Simple custom dict that raises custom Errors when a key is missing.
+
+ HiddenKeyError is raised when the user does not have permission.
+ KeyError is raised otherwise.

It also logs debugging information when a key is missing.
"""
@@ -31,6 +34,13 @@
"""Debug when the key is missing."""
pywikibot.debug(u"API log entry received:\n" + repr(self),
_logger)
+ if ((key in ('ns', 'title', 'pageid', 'logpage', 'params', 'action')
+ and 'actionhidden' in self)
+ or (key == 'comment' and 'commenthidden' in self)
+ or (key == 'user' and 'userhidden' in self)):
+ raise HiddenKeyError(
+ "Log entry ({0}) has a hidden '{1}' key and you don't have "
+ 'permission to view it.'.format(self._type, key))
raise KeyError("Log entry (%s) has no '%s' key" % (self._type, key))


@@ -112,12 +122,8 @@
"""
Page on which action was performed.

- @note: title may be missing in data dict e.g. by oversight action to
- hide the title. In that case a KeyError exception will raise
-
@return: page on action was performed
@rtype: pywikibot.Page
- @raise: KeyError: title was missing from log entry
"""
if not hasattr(self, '_page'):
self._page = pywikibot.Page(self.site, self.data['title'])
@@ -146,7 +152,6 @@
def comment(self):
"""Return the logentry's comment.

- @raise KeyError: The log entry has no 'comment' key.
@rtype: str
"""
return self.data['comment']
@@ -300,11 +305,7 @@
"""
Return FilePage on which action was performed.

- Note: title may be missing in data dict e.g. by oversight action to
- hide the title. In that case a KeyError exception will raise
-
@rtype: pywikibot.FilePage
- @raise: KeyError: title was missing from log entry
"""
if not hasattr(self, '_page'):
self._page = pywikibot.FilePage(self.site, self.data['title'])
diff --git a/tests/logentry_tests.py b/tests/logentry_tests.py
index 50f900a..05c734f 100644
--- a/tests/logentry_tests.py
+++ b/tests/logentry_tests.py
@@ -11,6 +11,7 @@

import pywikibot

+from pywikibot.exceptions import HiddenKeyError
from pywikibot.logentries import LogEntryFactory, UserTargetLogEntry
from pywikibot.tools import (
MediaWikiVersion,
@@ -77,6 +78,11 @@
self.assertIsInstance(logentry.action(), unicode)
try:
self.assertIsInstance(logentry.comment(), unicode)
+ except HiddenKeyError as e:
+ self.assertRegex(
+ str(e),
+ "Log entry ([^)]+) has a hidden 'comment' key, and you don't "
+ 'have permission to view it.')
except KeyError as e:
self.assertRegex(str(e), "Log entry ([^)]+) has no 'comment' key")
self.assertIsInstance(logentry.logid(), int)

To view, visit change 421805. To unsubscribe, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Iba46f5b17057ce825d6d1a94d9ccacb2a6a051dc
Gerrit-Change-Number: 421805
Gerrit-PatchSet: 11
Gerrit-Owner: JJMC89 <JJMC89.Wikimedia@gmail.com>
Gerrit-Reviewer: Framawiki <framawiki@tools.wmflabs.org>
Gerrit-Reviewer: JJMC89 <JJMC89.Wikimedia@gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1999@gmail.com>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444@gmail.com>
Gerrit-Reviewer: jenkins-bot <>