jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] reduce code complexity in pagegenerators.py

Change-Id: Ife0b5a13191d89f88426e8aec4f6573de986a322
---
M pywikibot/pagegenerators.py
M tests/pagegenerators_tests.py
2 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py
index 22ca97b..4937e1f 100644
--- a/pywikibot/pagegenerators.py
+++ b/pywikibot/pagegenerators.py
@@ -1646,8 +1646,13 @@

"""Item claim filter."""

+ page_classes = {
+ True: pywikibot.PropertyPage,
+ False: pywikibot.ItemPage,
+ }
+
@classmethod
- def __filter_match(cls, page, prop, claim, qualifiers=None):
+ def __filter_match(cls, page, prop, claim, qualifiers):
"""
Return true if the page contains the claim given.

@@ -1655,34 +1660,27 @@
@return: true if page contains the claim, false otherwise
@rtype: bool
"""
- if not isinstance(page, pywikibot.page.WikibasePage):
- if isinstance(page.site, pywikibot.site.DataSite): # T175151
- on_repo = page.namespace() in (
- page.site.item_namespace, page.site.property_namespace)
- else:
- on_repo = False
- if on_repo:
- if page.namespace() == page.site.property_namespace:
- page_cls = pywikibot.PropertyPage
- else:
- page_cls = pywikibot.ItemPage
+ if not isinstance(page, pywikibot.page.WikibasePage): # T175151
+ try:
+ assert page.site.property_namespace
+ assert page.site.item_namespace
+ key = page.namespace() == page.site.property_namespace
+ page_cls = cls.page_classes[key]
page = page_cls(page.site, page.title(with_ns=False))
- else:
+ except (AttributeError, AssertionError):
try:
page = pywikibot.ItemPage.fromPage(page)
except pywikibot.NoPage:
return False

- for page_claim in page.get()['claims'].get(prop, []):
- if page_claim.target_equals(claim):
- if not qualifiers:
- return True
+ def match_qualifiers(page_claim, qualifiers):
+ return all(page_claim.has_qualifier(prop, val)
+ for prop, val in qualifiers.items())

- for prop, val in qualifiers.items():
- if not page_claim.has_qualifier(prop, val):
- return False
- return True
- return False
+ page_claims = page.get()['claims'].get(prop, [])
+ return any(
+ p_cl.target_equals(claim) and match_qualifiers(p_cl, qualifiers)
+ for p_cl in page_claims)

@classmethod
def filter(cls, generator, prop: str, claim,
@@ -1699,6 +1697,7 @@
@param negate: true if pages that do *not* contain specified claim
should be yielded, false otherwise
"""
+ qualifiers = qualifiers or {}
for page in generator:
if cls.__filter_match(page, prop, claim, qualifiers) is not negate:
yield page
diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py
index 3524234..9fec7fc 100755
--- a/tests/pagegenerators_tests.py
+++ b/tests/pagegenerators_tests.py
@@ -822,6 +822,15 @@
self._simple_claim_test('P463', 'Q37470', None, False, True)
self._simple_claim_test('P463', 'Q37471', None, True, True)

+ def test_item_from_page(self):
+ """Test ItemPage can be obtained form Page."""
+ site = pywikibot.Site('en', 'wikipedia')
+ page = pywikibot.Page(site, 'India')
+ gen = pagegenerators.ItemClaimFilterPageGenerator(
+ [page], 'P463', self._get_council_page())
+ pages = set(gen)
+ self.assertEqual(pages.pop(), page)
+

class TestFactoryGenerator(DefaultSiteTestCase):


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

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ife0b5a13191d89f88426e8aec4f6573de986a322
Gerrit-Change-Number: 640565
Gerrit-PatchSet: 4
Gerrit-Owner: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged