XZise has uploaded a new change for review.
https://gerrit.wikimedia.org/r/232255
Change subject: [IMPROV] Support generic Page upcasting
......................................................................
[IMPROV] Support generic Page upcasting
This adds a classmethod to upcast the `Page` instance depending on the
namespace as it has been done by `PageGenerator`. This also implements that
`LogEntry` returns the suitable class instance in the `page()` method.
Change-Id: If6b56cce3dd6990b999ab679bff0c53c0731cf7d
---
M pywikibot/data/api.py
M pywikibot/logentries.py
M pywikibot/page.py
M tests/logentry_tests.py
4 files changed, 26 insertions(+), 8 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/55/232255/1
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index a7b7f6d..7d67880 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -218,6 +218,7 @@
# submodules, because that would require to fetch all modules when only
# the names of them were requested
assert '+' not in name
+ print('_add_submodules')
modules = frozenset(modules)
if name == 'main':
# The main module behaves differently as it has no prefix
@@ -2799,13 +2800,8 @@
of object.
"""
- p = pywikibot.Page(self.site, pagedata['title'], pagedata['ns'])
- ns = pagedata['ns']
- # Upcast to proper Page subclass.
- if ns == 6:
- p = pywikibot.FilePage(p)
- elif ns == 14:
- p = pywikibot.Category(p)
+ p = pywikibot.Page.from_namespace(self.site, pagedata['title'],
+ pagedata['ns'])
update_page(p, pagedata, self.props)
return p
diff --git a/pywikibot/logentries.py b/pywikibot/logentries.py
index 11f1d73..2bb94b0 100644
--- a/pywikibot/logentries.py
+++ b/pywikibot/logentries.py
@@ -101,11 +101,15 @@
"""
Page on which action was performed.
+ This will automatically choose the proper subclass depending on the
+ namespace.
+
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
"""
if not hasattr(self, '_page'):
- self._page = pywikibot.Page(self.site, self.data['title'])
+ self._page = pywikibot.Page.from_namespace(
+ self.site, self.data['title'], self.ns())
return self._page
def type(self):
diff --git a/pywikibot/page.py b/pywikibot/page.py
index 5c0ee75..424c7de 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -2006,6 +2006,19 @@
'if source is a Site.')
super(Page, self).__init__(source, title, ns)
+ @classmethod
+ def from_namespace(cls, source, title, ns):
+ """Instantiate the proper page class depending on namespace."""
+ # Upcast to proper Page subclass.
+ if ns == 2:
+ return pywikibot.User(source, title)
+ elif ns == 6:
+ return pywikibot.FilePage(source, title)
+ elif ns == 14:
+ return pywikibot.Category(source, title)
+ else:
+ return pywikibot.Page(source, title, ns)
+
@deprecate_arg("get_redirect", None)
def templatesWithParams(self):
"""Iterate templates used on this Page.
diff --git a/tests/logentry_tests.py b/tests/logentry_tests.py
index 82447e8..e8e8b6d 100644
--- a/tests/logentry_tests.py
+++ b/tests/logentry_tests.py
@@ -152,6 +152,9 @@
# only 'block' entries can be tested
for logentry in self.site.logevents(logtype='block', total=5):
if logentry.action() == 'block':
+ if 'title' in logentry.data:
+ self.assertIsInstance(logentry.page(), (pywikibot.User,
+ int))
self.assertIsInstance(logentry.flags(), list)
# Check that there are no empty strings
self.assertTrue(all(logentry.flags()))
@@ -167,6 +170,8 @@
def test_RightsEntry(self, key):
"""Test RightsEntry methods."""
logentry = self._get_logentry('rights')
+ if 'title' in logentry.data:
+ self.assertIsInstance(logentry.page(), pywikibot.User)
self.assertIsInstance(logentry.oldgroups, list)
self.assertIsInstance(logentry.newgroups, list)
--
To view, visit https://gerrit.wikimedia.org/r/232255
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: If6b56cce3dd6990b999ab679bff0c53c0731cf7d
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits