jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/334912 )
Change subject: Set Coordinate globe via item
......................................................................
Set Coordinate globe via item
Allow globe to be set via a PageItem (in addition to its entity
uri). Also renames this parameter to clarify its use.
Also add get_globe_item() for retrieving the ItemPage corresponding
to the globe, independently of how the globe was set.
Change-Id: Idbf79a42720aca30be9a61aaf3b497f53ed87490
---
M pywikibot/__init__.py
M tests/wikibase_edit_tests.py
M tests/wikibase_tests.py
3 files changed, 141 insertions(+), 16 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 273e538..bc23405 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -235,8 +235,9 @@
_items = ('lat', 'lon', 'globe')
+ @deprecate_arg('entity', 'globe_item')
def __init__(self, lat, lon, alt=None, precision=None, globe='earth',
- typ="", name="", dim=None, site=None,
entity=''):
+ typ='', name='', dim=None, site=None, globe_item=None):
"""
Represent a geo coordinate.
@@ -257,8 +258,9 @@
@type dim: int
@param site: The Wikibase site
@type site: pywikibot.site.DataSite
- @param entity: The URL entity of a Wikibase item for the globe
- @type entity: str
+ @param globe_item: The Wikibase item for the globe, or the entity URI
+ of this Wikibase item.
+ @type globe_item: pywikibot.ItemPage or str
"""
self.lat = lat
self.lon = lon
@@ -267,20 +269,22 @@
if globe:
globe = globe.lower()
self.globe = globe
- self._entity = entity
+ self._entity = globe_item
self.type = typ
self.name = name
self._dim = dim
- if not site:
- self.site = Site().data_repository()
- else:
- self.site = site
+ self.site = site or Site().data_repository()
@property
def entity(self):
- if self._entity:
- return self._entity
- return self.site.globes()[self.globe]
+ """Return the entity uri of the globe."""
+ if not self._entity:
+ return self.site.globes()[self.globe]
+
+ if isinstance(self._entity, ItemPage):
+ return self._entity.concept_uri()
+
+ return self._entity
def toWikibase(self):
"""
@@ -326,7 +330,7 @@
return cls(data['latitude'], data['longitude'],
data['altitude'], data['precision'],
- globe, site=site, entity=data['globe'])
+ globe, site=site, globe_item=data['globe'])
@property
def precision(self):
@@ -396,6 +400,29 @@
)
return self._dim
+ def get_globe_item(self, repo=None, lazy_load=False):
+ """
+ Return the ItemPage corresponding to the globe.
+
+ Note that the globe need not be in the same data repository as the
+ Coordinate itself.
+
+ A successful lookup is stored as an internal value to avoid the need
+ for repeated lookups.
+
+ @param repo: the Wikibase site for the globe, if different from that
+ provided with the Coordinate.
+ @type repo: pywikibot.site.DataSite
+ @param lazy_load: Do not raise NoPage if ItemPage does not exist.
+ @type lazy_load: bool
+ @return: pywikibot.ItemPage
+ """
+ if isinstance(self._entity, ItemPage):
+ return self._entity
+
+ repo = repo or self.site
+ return ItemPage.from_entity_uri(repo, self.entity, lazy_load)
+
class WbTime(_WbRepresentation):
diff --git a/tests/wikibase_edit_tests.py b/tests/wikibase_edit_tests.py
index 670eab9..6a54d5a 100644
--- a/tests/wikibase_edit_tests.py
+++ b/tests/wikibase_edit_tests.py
@@ -239,6 +239,30 @@
claim = item.claims['P271'][0]
self.assertEqual(claim.getTarget(), target)
+ def test_Coordinate_edit(self):
+ """Attempt adding a Coordinate with globe set via
item."""
+ testsite = self.get_repo()
+ item = self._clean_item(testsite, 'P20480')
+
+ # Make sure the wiki supports wikibase-conceptbaseuri
+ version = testsite.version()
+ if MediaWikiVersion(version) < MediaWikiVersion('1.29.0-wmf.2'):
+ raise unittest.SkipTest('Wiki version must be 1.29.0-wmf.2 or '
+ 'newer to support unbound uncertainties.')
+
+ # set new claim
+ claim = pywikibot.page.Claim(testsite, 'P20480',
+ datatype='globe-coordinate')
+ target = pywikibot.Coordinate(site=testsite, lat=12.0, lon=13.0,
+ globe_item=item)
+ claim.setTarget(target)
+ item.addClaim(claim)
+
+ # confirm new claim
+ item.get(force=True)
+ claim = item.claims['P20480'][0]
+ self.assertEqual(claim.getTarget(), target)
+
def test_WbQuantity_edit_unbound(self):
"""Attempt adding a quantity with unbound
errors."""
# Clean the slate in preparation for test.
@@ -246,7 +270,8 @@
item = self._clean_item(testsite, 'P69')
# Make sure the wiki supports unbound uncertainties
- if MediaWikiVersion(testsite.version()) <
MediaWikiVersion('1.29.0-wmf.2'):
+ version = testsite.version()
+ if MediaWikiVersion(version) < MediaWikiVersion('1.29.0-wmf.2'):
raise unittest.SkipTest('Wiki version must be 1.29.0-wmf.2 or '
'newer to support unbound uncertainties.')
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 79a70d8..c80afff 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -158,18 +158,91 @@
with self.assertRaises(ValueError):
z.precisionToDim()
- def test_Coordinate_entity_globe(self):
- """Test setting Coordinate globe from entity."""
+ def test_Coordinate_plain_globe(self):
+ """Test setting Coordinate globe from a plain-text
value."""
repo = self.get_repo()
coord = pywikibot.Coordinate(
site=repo, lat=12.0, lon=13.0, precision=0,
-
entity='http://www.wikidata.org/entity/Q123')
+ globe='moon')
+ self.assertEqual(coord.toWikibase(),
+ {'latitude': 12.0, 'longitude': 13.0,
+ 'altitude': None, 'precision': 0,
+ 'globe':
'http://www.wikidata.org/entity/Q405'})
+
+ def test_Coordinate_entity_uri_globe(self):
+ """Test setting Coordinate globe from an entity
uri."""
+ repo = self.get_repo()
+ coord = pywikibot.Coordinate(
+ site=repo, lat=12.0, lon=13.0, precision=0,
+
globe_item='http://www.wikidata.org/entity/Q123')
self.assertEqual(coord.toWikibase(),
{'latitude': 12.0, 'longitude': 13.0,
'altitude': None, 'precision': 0,
'globe':
'http://www.wikidata.org/entity/Q123'})
+class TestWikibaseCoordinateNonDry(WikidataTestCase):
+
+ """
+ Test Wikibase Coordinate data type (non-dry).
+
+ These can be moved to TestWikibaseCoordinate once DrySite has been bumped
+ to the appropriate version.
+ """
+
+ def test_Coordinate_item_globe(self):
+ """Test setting Coordinate globe from an
ItemPage."""
+ repo = self.get_repo()
+ coord = pywikibot.Coordinate(
+ site=repo, lat=12.0, lon=13.0, precision=0,
+ globe_item=ItemPage(repo, 'Q123'))
+ self.assertEqual(coord.toWikibase(),
+ {'latitude': 12.0, 'longitude': 13.0,
+ 'altitude': None, 'precision': 0,
+ 'globe':
'http://www.wikidata.org/entity/Q123'})
+
+ def test_Coordinate_get_globe_item_from_uri(self):
+ """Test getting globe item from Coordinate with entity uri
globe."""
+ repo = self.get_repo()
+ q = pywikibot.Coordinate(
+ site=repo, lat=12.0, lon=13.0, precision=0,
+
globe_item='http://www.wikidata.org/entity/Q123')
+ self.assertEqual(q.get_globe_item(), ItemPage(repo, 'Q123'))
+
+ def test_Coordinate_get_globe_item_from_itempage(self):
+ """Test getting globe item from Coordinate with ItemPage
globe."""
+ repo = self.get_repo()
+ globe = ItemPage(repo, 'Q123')
+ q = pywikibot.Coordinate(
+ site=repo, lat=12.0, lon=13.0, precision=0, globe_item=globe)
+ self.assertEqual(q.get_globe_item(), ItemPage(repo, 'Q123'))
+
+ def test_Coordinate_get_globe_item_from_plain_globe(self):
+ """Test getting globe item from Coordinate with plain text
globe."""
+ repo = self.get_repo()
+ q = pywikibot.Coordinate(
+ site=repo, lat=12.0, lon=13.0, precision=0, globe='moon')
+ self.assertEqual(q.get_globe_item(), ItemPage(repo, 'Q405'))
+
+ def test_Coordinate_get_globe_item_provide_repo(self):
+ """Test getting globe item from Coordinate, providing
repo."""
+ repo = self.get_repo()
+ q = pywikibot.Coordinate(
+ site=repo, lat=12.0, lon=13.0, precision=0,
+
globe_item='http://www.wikidata.org/entity/Q123')
+ self.assertEqual(q.get_globe_item(repo), ItemPage(repo, 'Q123'))
+
+ def test_Coordinate_get_globe_item_different_repo(self):
+ """Test getting globe item in different repo from
Coordinate."""
+ repo = self.get_repo()
+ test_repo = pywikibot.Site('test', 'wikidata')
+ q = pywikibot.Coordinate(
+ site=repo, lat=12.0, lon=13.0, precision=0,
+
globe_item='http://test.wikidata.org/entity/Q123')
+ self.assertEqual(q.get_globe_item(test_repo),
+ ItemPage(test_repo, 'Q123'))
+
+
class TestWbTime(WikidataTestCase):
"""Test Wikibase WbTime data type."""
--
To view, visit
https://gerrit.wikimedia.org/r/334912
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Idbf79a42720aca30be9a61aaf3b497f53ed87490
Gerrit-PatchSet: 7
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Lokal Profil <lokal.profil(a)gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Lokal Profil <lokal.profil(a)gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski(a)gmail.com>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>