def format_claim_value(claim, value): """ Reformat the internal claim as the relevant pywikibot object. @param claim: pywikibot.Claim to which value should be added @param value: str|dict encoding the value to be added @return: pywikibot representation of the claim value """ repo = claim.repo if claim.type == 'wikibase-item': return pywikibot.ItemPage(repo, value) elif claim.type == 'commonsMedia': return pywikibot.FilePage(_get_commons(), value) elif claim.type == 'geo-shape': return pywikibot.WbGeoShape( pywikibot.Page(repo.geo_shape_repository(), value)) elif claim.type == 'tabular-data': return pywikibot.WbTabularData( pywikibot.Page(repo.tabular_data_repository(), value)) elif claim.type == 'monolingualtext': if common.is_str(value): text, _, lang = value.partition('@') value = {'text': text, 'lang': lang} return pywikibot.WbMonolingualText(value.get('text'), value.get('lang')) elif claim.type == 'globe-coordinate': if common.is_str(value): parts = value.replace(',', '@').split('@') value = {parts[1]: parts[0], parts[3]: parts[2]} # set precision to the least precise of the values precision = max(coord_precision(value.get('lat')), coord_precision(value.get('lon'))) return pywikibot.Coordinate(float(value.get('lat')), float(value.get('lon')), precision=precision) elif claim.type == 'quantity': if common.is_str(value): amount, _, unit = value.partition('@') value = {'amount': amount, 'unit': unit} if value.get('unit'): return pywikibot.WbQuantity(value.get('amount'), pywikibot.ItemPage( repo, value.get('unit')), site=repo) return pywikibot.WbQuantity(value.get('amount'), site=repo) elif claim.type == 'time': # note that Wikidata only supports precision down to day # as a result pywikibot.WbTime.fromTimestr will produce an incompatible # result for a fully qualified timestamp/timestr return iso_to_wbtime(value) # simple strings return value
def test_WbGeoShape_edit(self): """Attempt adding a geo-shape with valid input.""" # Clean the slate in preparation for test. testsite = self.get_repo() item = self._clean_item(testsite, 'P27199') # set new claim claim = pywikibot.page.Claim(testsite, 'P27199', datatype='geo-shape') commons_site = pywikibot.Site('commons', 'commons') page = pywikibot.Page(commons_site, 'Data:Lyngby Hovedgade.map') target = pywikibot.WbGeoShape(page) claim.setTarget(target) item.addClaim(claim) # confirm new claim item.get(force=True) claim = item.claims['P27199'][0] self.assertEqual(claim.getTarget(), target)
def test_WbTabularData_edit(self): """Attempt adding a tabular-data with valid input.""" # Clean the slate in preparation for test. testsite = self.get_repo() item = self._clean_item(testsite, 'P30175') # set new claim claim = pywikibot.page.Claim( testsite, 'P30175', datatype='tabular-data') commons_site = pywikibot.Site('commons', 'commons') page = pywikibot.Page(commons_site, 'Data:Bea.gov/GDP by state.tab') target = pywikibot.WbGeoShape(page) claim.setTarget(target) item.addClaim(claim) # confirm new claim item.get(force=True) claim = item.claims['P30175'][0] self.assertEqual(claim.getTarget(), target)