jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/332384 )
Change subject: Make Wikibase Property provide labels data ......................................................................
Make Wikibase Property provide labels data
Change-Id: I139ef7e263343e965a9de12acc8c531b774ce63c --- M pywikibot/page.py M pywikibot/site.py M tests/wikibase_tests.py 3 files changed, 51 insertions(+), 4 deletions(-)
Approvals: jenkins-bot: Verified Sn1per: Looks good to me, approved
diff --git a/pywikibot/page.py b/pywikibot/page.py index f015e45..c83e1bd 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -12,7 +12,7 @@
""" # -# (C) Pywikibot team, 2008-2016 +# (C) Pywikibot team, 2008-2017 # # Distributed under the terms of the MIT license. # @@ -4275,6 +4275,17 @@ self._type = self.repo.getPropertyType(self) return self._type
+ @property + def labels(self): + """ + Return labels of this property. + + @rtype: dict + """ + if not hasattr(self, '_labels'): + self._labels = self.repo.get_property_labels(self) + return self._labels + @deprecated("Property.type") def getType(self): """ diff --git a/pywikibot/site.py b/pywikibot/site.py index f2724d9..f8b59e4 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -6,7 +6,7 @@ groups of wikis on the same topic in different languages. """ # -# (C) Pywikibot team, 2008-2016 +# (C) Pywikibot team, 2008-2017 # # Distributed under the terms of the MIT license. # @@ -7246,6 +7246,30 @@
return dtype
+ def get_property_labels(self, prop): + """ + Obtain labels of a property. + + Results are cached for 1 day. + """ + params = dict( + action='wbgetentities', + ids=prop.getID(), + props='labels', + ) + expiry = datetime.timedelta(days=1) + req = self._request(expiry=expiry, parameters=params) + data = req.submit() + + # the IDs returned from the API can be upper or lowercase, depending + # on the version. See bug T55894 for more information. + try: + dtype = data['entities'][prop.getID()]['labels'] + except KeyError: + dtype = data['entities'][prop.getID().lower()]['labels'] + + return dtype + @must_be(group='user') def editEntity(self, identification, data, bot=True, **kwargs): """ diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index 0aaad45..70aaa22 100644 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Tests for the Wikidata parts of the page module.""" # -# (C) Pywikibot team, 2008-2016 +# (C) Pywikibot team, 2008-2017 # # Distributed under the terms of the MIT license. # @@ -18,7 +18,7 @@ import pywikibot
from pywikibot import pagegenerators -from pywikibot.page import WikibasePage, ItemPage, PropertyPage +from pywikibot.page import WikibasePage, ItemPage, Property, PropertyPage from pywikibot.site import Namespace, NamespacesDict from pywikibot.tools import MediaWikiVersion
@@ -718,6 +718,18 @@ self.assertEqual(item.getRedirectTarget(), target)
+class TestProperty(WikidataTestCase): + + """Test Property.""" + + def test_labels(self): + """Test retrieving Property labels.""" + wikidata = self.get_repo() + property = Property(wikidata, 'P625') + expected = {'language': 'en', 'value': 'coordinate location'} + self.assertDictEqual(property.labels['en'], expected) + + class TestPropertyPage(WikidataTestCase):
"""Test PropertyPage."""
pywikibot-commits@lists.wikimedia.org