http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11287
Revision: 11287 Author: legoktm Date: 2013-03-29 02:54:48 +0000 (Fri, 29 Mar 2013) Log Message: ----------- Make it possible to read and write claims with a snakvalue of "novalue" or "somevalue"
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-28 17:08:58 UTC (rev 11286) +++ branches/rewrite/pywikibot/page.py 2013-03-29 02:54:48 UTC (rev 11287) @@ -2546,6 +2546,7 @@ self.isReference = isReference self.sources = [] self.target = None + self.snaktype = 'value'
@staticmethod def fromJSON(site, data): @@ -2558,12 +2559,14 @@ claim.snak = data['id'] else: claim.isReference = True - claim.type = data['mainsnak']['datavalue']['type'] - if claim.type == 'wikibase-entityid': - claim.target = ItemPage(site, 'Q' + - str(data['mainsnak']['datavalue']['value']['numeric-id'])) - else: - claim.target = data['mainsnak']['datavalue']['value'] + claim.snaktype = data['mainsnak']['snaktype'] + if claim.getSnakType() == 'value': + claim.type = data['mainsnak']['datavalue']['type'] + if claim.type == 'wikibase-entityid': + claim.target = ItemPage(site, 'Q' + + str(data['mainsnak']['datavalue']['value']['numeric-id'])) + else: + claim.target = data['mainsnak']['datavalue']['value'] if 'references' in data: for source in data['references']: claim.sources.append(Claim.referenceFromJSON(site, source)) @@ -2616,6 +2619,28 @@ """ return self.target
+ def getSnakType(self): + """ + Returns the "snaktype" + Can be "value", "somevalue" or "novalue" + """ + return self.snaktype + + def setSnakType(self, value): + if value in ['value', 'somevalue', 'novalue']: + self.snaktype = value + else: + raise ValueError("snaktype must be 'value', 'somevalue', or 'novalue'.") + + def changeSnakType(self, value=None, **kwargs): + """ + This actually saves the new snakvalue. + TODO: Is this function really needed? + """ + if value: + self.setSnakType(value) + self.changeTarget(snaktype=self.getSnakType(), **kwargs) + def getSources(self): """ Returns a list of Claims
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2013-03-28 17:08:58 UTC (rev 11286) +++ branches/rewrite/pywikibot/site.py 2013-03-29 02:54:48 UTC (rev 11287) @@ -3350,18 +3350,19 @@ params = dict(action='wbcreateclaim', entity=item.getID(), baserevid=item.latestRevision(), - snaktype='value', - property=claim.getID() - ) + snaktype=claim.getSnakType(), + property=claim.getID(), + ) if bot: params['bot'] = 1 - if claim.getType() == 'wikibase-item': - params['value'] = json.dumps({'entity-type': 'item', - 'numeric-id': claim.getTarget().getID(numeric=True)}) - elif claim.getType() == 'string': - params['value'] = '"' + claim.getTarget() + '"' - else: - raise NotImplementedError('%s datatype is not supported yet.' % claim.getType()) + if claim.getSnakType() == 'value': + if claim.getType() == 'wikibase-item': + params['value'] = json.dumps({'entity-type': 'item', + 'numeric-id': claim.getTarget().getID(numeric=True)}) + elif claim.getType() == 'string': + params['value'] = '"' + claim.getTarget() + '"' + else: + raise NotImplementedError('%s datatype is not supported yet.' % claim.getType()) params['token'] = self.token(item, 'edit') req = api.Request(site=self, **params) data = req.submit()
pywikipedia-svn@lists.wikimedia.org