Lokal Profil has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/339389 )
Change subject: [WIP]Allow retrieval of unit as ItemPage for WbQuantity
......................................................................
[WIP]Allow retrieval of unit as ItemPage for WbQuantity
WIP awaiting the final shape of unit4 branch
@todo: tests!
Change-Id: I9746881ada22cb7dd47a76c0597737f063cd31f6
---
M pywikibot/__init__.py
1 file changed, 36 insertions(+), 1 deletion(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/89/339389/1
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 0233583..41c9b47 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -65,7 +65,9 @@
)
from pywikibot.family import Family
from pywikibot.i18n import translate
-from pywikibot.site import BaseSite
+from pywikibot.site import (
+ BaseSite, DataSite
+)
from pywikibot.tools import (
# __ to avoid conflict with ModuleDeprecationWrapper._deprecated
deprecated as __deprecated,
@@ -676,6 +678,7 @@
self.amount = self._todecimal(amount)
self._unit = unit
+ self.site = site or Site().data_repository()
# also allow entity urls to be provided via unit parameter
if isinstance(unit, basestring) and \
@@ -703,6 +706,38 @@
return self._unit.concept_url()
return self._unit or '1'
+ def get_unit_item(self, repo=None):
+ """
+ Return the ItemPage corresponding to the unit.
+
+ Note that the unit need not be in the same data repository as the
+ WbQuantity itself.
+
+ @param repo: the Wikibase site for the unit, if different from that
+ provided with the WbQuantity.
+ @type repo: pywikibot.site.DataSite
+ @return: pywikibot.ItemPage
+ """
+ if not isinstance(self._unit, basestring):
+ return self._unit
+
+ # attempt to idenify the ItemPage
+ repo = repo or self.site
+ if not isinstance(repo, DataSite):
+ # wrong type or undefined
+ raise TypeError('The supplied site was not a data repository.')
+ base_uri, _, qid = self._unit.rpartition('/')
+ if base_uri != repo.concept_base_uri:
+ raise ValueError(
+ 'The supplied data repository ({repo}) does not correspond to '
+ 'that of the unit ({unit})'.format(
+ repo=repo.concept_base_uri,
+ unit=base_uri))
+
+ # Store ItemPage as internal value to avoid repeated lookups
+ self._unit = ItemPage(repo, qid)
+ return self._unit
+
def toWikibase(self):
"""
Convert the data to a JSON object for the Wikibase API.
--
To view, visit https://gerrit.wikimedia.org/r/339389
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I9746881ada22cb7dd47a76c0597737f063cd31f6
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Lokal Profil <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits