http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11715
Revision: 11715 Author: legoktm Date: 2013-07-06 21:05:53 +0000 (Sat, 06 Jul 2013) Log Message: ----------- Add the ability to parse and read existing qualifiers
Modified Paths: -------------- branches/rewrite/pywikibot/page.py
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2013-07-06 05:09:46 UTC (rev 11714) +++ branches/rewrite/pywikibot/page.py 2013-07-06 21:05:53 UTC (rev 11715) @@ -2628,7 +2628,8 @@ """ Claims are standard claims as well as references. """ - def __init__(self, site, pid, snak=None, hash=None, isReference=False): + def __init__(self, site, pid, snak=None, hash=None, isReference=False, + isQualifier=False): """ Defined by the "snak" value, supplemented by site + pid """ @@ -2636,7 +2637,11 @@ self.snak = snak self.hash = hash self.isReference = isReference + self.isQualifier = isQualifier + if self.isQualifier and self.isReference: + raise ValueError(u'Claim cannot be both a qualifier and reference.') self.sources = [] + self.qualifiers = [] self.target = None self.snaktype = 'value' self.on_item = None # The item it's on @@ -2650,8 +2655,11 @@ claim = Claim(site, data['mainsnak']['property']) if 'id' in data: claim.snak = data['id'] + elif 'hash' in data: + claim.isReference = True + claim.hash = data['hash'] else: - claim.isReference = True + claim.isQualifier = True claim.snaktype = data['mainsnak']['snaktype'] if claim.getSnakType() == 'value': if claim.getType() == 'wikibase-item': @@ -2668,6 +2676,10 @@ if 'references' in data: for source in data['references']: claim.sources.append(Claim.referenceFromJSON(site, source)) + if 'qualifiers' in data: + for prop in data['qualifiers']: + for qualifier in data['qualifiers'][prop]: + claim.qualifiers.append(Claim.qualifierFromJSON(site, qualifier)) return claim
@staticmethod @@ -2678,11 +2690,20 @@ more handling. """ mainsnak = data['snaks'].values()[0][0] - wrap = {'mainsnak': mainsnak} - c = Claim.fromJSON(site, wrap) - c.hash = data['hash'] - return c + wrap = {'mainsnak': mainsnak, 'hash': data['hash']} + return Claim.fromJSON(site, wrap)
+ @staticmethod + def qualifierFromJSON(site, data): + """ + Qualifier objects are represented a bit + differently like references, but I'm not + sure if this even requires it's own function. + """ + wrap = {'mainsnak': data} + return Claim.fromJSON(site, wrap) + + def setTarget(self, value): """ Sets the target to the passed value.