jenkins-bot has submitted this change and it was merged.
Change subject: [IMPROV] Use commons for claim-type commonsMedia ......................................................................
[IMPROV] Use commons for claim-type commonsMedia
The commonsMedia claim type only works for Wikimedia Commons so allowing in theory any image repository doesn't make sense. To make it more dynamic it's using a dictionary to exchange the conversion of the claim value depending on the claim type. Without that it wouldn't be possible to do the wikibase tests in dry mode because it would try to create a Site which isn't allowed usually.
Change-Id: I727edf1120b7dc75cea72e679e16f122ac4de3ad --- M pywikibot/page.py M tests/aspects.py 2 files changed, 20 insertions(+), 13 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index 2fcb3a5..918d5da 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -3610,6 +3610,16 @@ Claims are standard claims as well as references. """
+ TARGET_CONVERTER = { + 'wikibase-item': lambda value, site: + ItemPage(site, 'Q' + str(value['numeric-id'])), + 'commonsMedia': lambda value, site: + FilePage(pywikibot.Site('commons', 'commons'), value), + 'globe-coordinate': pywikibot.Coordinate.fromWikibase, + 'time': lambda value, site: pywikibot.WbTime.fromWikibase(value), + 'quantity': lambda value, site: pywikibot.WbQuantity.fromWikibase(value), + } + def __init__(self, site, pid, snak=None, hash=None, isReference=False, isQualifier=False, **kwargs): """ @@ -3661,19 +3671,9 @@ claim.snaktype = data['mainsnak']['snaktype'] if claim.getSnakType() == 'value': value = data['mainsnak']['datavalue']['value'] - if claim.type == 'wikibase-item': - claim.target = ItemPage(site, 'Q' + str(value['numeric-id'])) - elif claim.type == 'commonsMedia': - claim.target = FilePage(site.image_repository(), value) - elif claim.type == 'globe-coordinate': - claim.target = pywikibot.Coordinate.fromWikibase(value, site) - elif claim.type == 'time': - claim.target = pywikibot.WbTime.fromWikibase(value) - elif claim.type == 'quantity': - claim.target = pywikibot.WbQuantity.fromWikibase(value) - else: - # This covers string, url types - claim.target = value + # The default covers string, url types + claim.target = Claim.TARGET_CONVERTER.get( + claim.type, lambda value, site: value)(value, site) if 'rank' in data: # References/Qualifiers don't have ranks claim.rank = data['rank'] if 'references' in data: diff --git a/tests/aspects.py b/tests/aspects.py index 8e82b55..6915e39 100644 --- a/tests/aspects.py +++ b/tests/aspects.py @@ -346,6 +346,12 @@ config.site_interface = SiteNotPermitted
pywikibot.data.api.Request = tests.utils.DryRequest + from tests.utils import DrySite + self.old_convert = pywikibot.Claim.TARGET_CONVERTER['commonsMedia'] + pywikibot.Claim.TARGET_CONVERTER['commonsMedia'] = ( + lambda value, site: pywikibot.FilePage( + pywikibot.Site('commons', 'commons', interface=DrySite), + value))
super(DisconnectedSiteMixin, self).setUp()
@@ -355,6 +361,7 @@
config.site_interface = self.old_config_interface pywikibot.data.api.Request = _original_Request + pywikibot.Claim.TARGET_CONVERTER['commonsMedia'] = self.old_convert
class CacheInfoMixin(TestCaseBase):
pywikibot-commits@lists.wikimedia.org