http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10720
Revision: 10720 Author: xqt Date: 2012-11-14 06:45:31 +0000 (Wed, 14 Nov 2012) Log Message: ----------- some basic methods to read data from wikidata * CAUTION: The names and results of these methods could be changed in next future - get_item() - get one item by id - get_labels() - get labels of one item by id - get_decriptions() - get descriptions of one item by id - get_sitelinks() - get sitelinks of one item by id
Modified Paths: -------------- branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2012-11-13 22:13:13 UTC (rev 10719) +++ branches/rewrite/pywikibot/site.py 2012-11-14 06:45:31 UTC (rev 10720) @@ -1051,9 +1051,10 @@ def data_repository(self): """Return Site object for data repository e.g. wikidata."""
- code, fam = self.shared_data_repository(write) + code, fam = self.shared_data_repository() if bool(code or fam): - return pywikibot.Site(code, fam, self.username()) + return pywikibot.Site(code, fam, self.username(), + interface="DataSite")
def is_image_repository(self): """Return True if Site object is the image repository.""" @@ -3186,6 +3187,95 @@ return wigen
+class DataSite (APISite): + def __init__(self, code, fam=None, user=None, sysop=None): + APISite.__init__(self, code, fam, user, sysop) + self._namespaces[4] = [u"Wikidata", "Projekt"] + self._namespaces[5] = [u"Wikidata talk", "Projekt talk"] + self._namespaces[6] = [u"File", "Image"] + self._namespaces[7] = [u"File talk", "Image talk"] + self._namespaces[120] = [u"Property"] + self._namespaces[120] = [u"Property talk"] + self._namespaces[120] = [u"Query"] + self._namespaces[120] = [u"Query talk"] + return + + def __getattr__(self, attr): + """Calls to methods get_labels, get_descriptions, get_sitelinks""" + + if hasattr(self.__class__, attr): + return getattr(self.__class__, attr) + if attr.startswith("get_"): + props = attr.replace("get_", "") + if props in ['labels', 'descriptions', 'sitelinks']: + method = self._get_propertyitem + f = lambda *args, **params: \ + method(props, *args, **params) + if hasattr(method, "__doc__"): + f.__doc__ = method.__doc__ + return f + return super(APISite, self).__getattr__(attr) + + def _get_propertyitem(self, props, source, **params): + """generic method to get the data for multiple Wikibase items""" + wbdata = self.get_item(source, props=props, **params) + assert props in wbdata, \ + "API wbgetitems response lacks %s key" % props + return wbdata[props] + + def get_item(self, source, **params): + """get the data for multiple Wikibase items""" + if type(source) == int or \ + isinstance(source, basestring) and source.isdigit(): + ids = str(source) + wbrequest = api.Request(site=self, action="wbgetitems", ids=ids, + **params) + wbdata = wbrequest.submit() + print wbdata.keys() + assert 'success' in wbdata, \ + "API wbgetitems response lacks 'success' key" + assert wbdata['success'] == 1, \ + "API 'success' key ist not 1" + assert 'items' in wbdata, \ + "API wbgetitems response lacks 'items' key" + assert ids in wbdata['items'], \ + "API wbgetitems response lacks %s key" % ids + return wbdata['items'][ids] + else: + # not implemented yet + raise NotImplementedError + + # deprecated BaseSite methods + def fam(self): + raise NotImplementedError + def urlEncode(self, *args, **kwargs): + raise NotImplementedError + def getUrl(self, *args, **kwargs): + raise NotImplementedError + def linkto(self, *args, **kwargs): + raise NotImplementedError + def loggedInAs(self, *args, **kwargs): + raise NotImplementedError + def postData(self, *args, **kwargs): + raise NotImplementedError + def postForm(self, *args, **kwargs): + raise NotImplementedError + + # deprecated APISite methods + def isBlocked(self, *args, **kwargs): + raise NotImplementedError + def isAllowed(self, *args, **kwargs): + raise NotImplementedError + def prefixindex(self, *args, **kwargs): + raise NotImplementedError + def categories(self, *args, **kwargs): + raise NotImplementedError + def linksearch(self, *args, **kwargs): + raise NotImplementedError + def newimages(self, *args, **kwargs): + raise NotImplementedError + + #### METHODS NOT IMPLEMENTED YET #### class NotImplementedYet: