http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11309
Revision: 11309 Author: legoktm Date: 2013-03-31 16:44:16 +0000 (Sun, 31 Mar 2013) Log Message: ----------- Split getting a property's datatype into its own request so it can be cached.
Since a property's datatype will never change, we can safely cache it "forever". I also fixed it so that we don't guess the datatype based on what was returned in a "datavalue" object, since that is unreliable.
Modified Paths: -------------- branches/rewrite/pywikibot/page.py branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2013-03-30 16:56:02 UTC (rev 11308) +++ branches/rewrite/pywikibot/page.py 2013-03-31 16:44:16 UTC (rev 11309) @@ -2515,9 +2515,7 @@ Examples: item, commons media file, StringValue, NumericalValue """ if not hasattr(self, 'type'): - self.get() - if self.type == 'wikibase-entityid': - self.type = 'wikibase-item' + self.type = self.repo.getPropertyType(self) return self.type
@@ -2561,8 +2559,7 @@ claim.isReference = True claim.snaktype = data['mainsnak']['snaktype'] if claim.getSnakType() == 'value': - claim.type = data['mainsnak']['datavalue']['type'] - if claim.type == 'wikibase-entityid': + if claim.getType() == 'wikibase-item': claim.target = ItemPage(site, 'Q' + str(data['mainsnak']['datavalue']['value']['numeric-id'])) else:
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2013-03-30 16:56:02 UTC (rev 11308) +++ branches/rewrite/pywikibot/site.py 2013-03-31 16:44:16 UTC (rev 11309) @@ -14,6 +14,7 @@ from hashlib import md5 except ImportError: from md5 import md5 +import datetime import itertools import os import re @@ -3331,6 +3332,21 @@ raise pywikibot.data.api.APIError, data['errors'] return data['entities']
+ def getPropertyType(self, prop): + """ + This is used sepecifically because we can cache + the value for a much longer time (near infinite). + """ + params = dict(action='wbgetentities', + ids=prop.getID(), + props='datatype', + ) + expiry = datetime.timedelta(days=365*100) + #Store it for 100 years + req = api.CachedRequest(expiry, site=self, **params) + data = req.submit() + return data['entities'][prop.getID()]['datatype'] + def editEntity(self, identification, data, **kwargs): params = dict(**identification) params['action'] = 'wbeditentity'
pywikipedia-svn@lists.wikimedia.org