http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11636
Revision: 11636 Author: legoktm Date: 2013-06-10 20:09:17 +0000 (Mon, 10 Jun 2013) Log Message: ----------- Implement the new geocoordinate datatype in Wikibase. This creates a "Coordinate" class which is used to store data.
Modified Paths: -------------- branches/rewrite/pywikibot/__init__.py branches/rewrite/pywikibot/page.py branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/__init__.py =================================================================== --- branches/rewrite/pywikibot/__init__.py 2013-06-10 08:55:21 UTC (rev 11635) +++ branches/rewrite/pywikibot/__init__.py 2013-06-10 20:09:17 UTC (rev 11636) @@ -87,6 +87,66 @@ return newdt
+class Coordinate(object): + """ + Class for handling and storing Coordinates. + For now its just being used for DataSite, but + in the future we can use it for the GeoData extension. + """ + def __init__(self, lat, lon, alt=None, precision=0.1, globe='earth'): + """ + @param lat: Latitude + @type lat: float + @param lon: Longitute + @param lon: float + @param alt: Altitute? TODO FIXME + @param precision: precision + @type precision: float + @param globe: Which Wikidata globe to use + @type globe: str + """ + self.lat = lat + self.lon = lon + self.alt = alt + self.precision = precision + self.globe = globe + #Copied from [[mw:Extension:GeoData]] + if not globe in ['earth', 'mercury', 'venus', 'moon', + 'mars', 'phobos', 'deimos', 'ganymede', + 'callisto', 'io', 'europa', 'mimas', + 'enceladus', 'tethys', 'dione', + 'rhea', 'titan', 'hyperion', 'iapetus', + 'phoebe', 'miranda', 'ariel', 'umbriel', + 'titania', 'oberon', 'triton', 'pluto']: + raise ValueError(u"%s is not a supported globe." % globe) + + def toWikibase(self): + """ + Function which converts the data to a JSON object + for the Wikibase API. + FIXME Should this be in the DataSite object? + """ + globes = {'earth': 'http://www.wikidata.org/entity/Q2%27%7D + if not self.globe in globes: + raise NotImplementedError(u"%s is not supported in Wikibase yet." % self.globe) + return {'latitude': self.lat, + 'longitude': self.lon, + 'altitude': self.alt, + 'globe': globes[self.globe], + 'precision': self.precision, + } + + @staticmethod + def fromWikibase(data): + """Constructor to create an object from Wikibase's JSON output""" + globes = {'http://www.wikidata.org/entity/Q2': 'earth'} + # TODO FIXME ^ + return Coordinate(data['latitude'], data['longitude'], + data['altitude'], data['precision'], + globes[data['globe']]) + + + def deprecated(instead=None): """Decorator to output a method deprecation warning.
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2013-06-10 08:55:21 UTC (rev 11635) +++ branches/rewrite/pywikibot/page.py 2013-06-10 20:09:17 UTC (rev 11636) @@ -2623,6 +2623,8 @@ elif claim.getType() == 'commonsMedia': claim.target = ImagePage(site.image_repository(), 'File:' + data['mainsnak']['datavalue']['value']) + elif claim.getType() == 'globecoordinate': + claim.target = pywikibot.Coordinate.fromWikibase(data['mainsnak']['datavalue']['value']) else: #This covers string type claim.target = data['mainsnak']['datavalue']['value'] @@ -2652,6 +2654,7 @@ types = {'wikibase-item': ItemPage, 'string': basestring, 'commonsMedia': ImagePage, + 'globecoordinate': pywikibot.Coordinate, } if self.getType() in types: if not isinstance(value, types[self.getType()]):
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2013-06-10 08:55:21 UTC (rev 11635) +++ branches/rewrite/pywikibot/site.py 2013-06-10 20:09:17 UTC (rev 11636) @@ -3350,7 +3350,11 @@ #Store it for 100 years req = api.CachedRequest(expiry, site=self, **params) data = req.submit() - return data['entities'][prop.getID()]['datatype'] + dtype = data['entities'][prop.getID()]['datatype'] + if dtype == 'globe-coordinate': + dtype = 'globecoordinate' + #TODO Fix this + return dtype
@must_be(group='user') def editEntity(self, identification, data, bot=True, **kwargs): @@ -3388,6 +3392,8 @@ params['value'] = json.dumps(claim.getTarget()) elif claim.getType() == 'commonsMedia': params['value'] = json.dumps(claim.getTarget().title(withNamespace=False)) + elif claim.getType() == 'globecoordinate': + params['value'] = json.dumps(claim.getTarget().toWikibase()) else: raise NotImplementedError('%s datatype is not supported yet.' % claim.getType()) params['token'] = self.token(item, 'edit') @@ -3428,6 +3434,8 @@ params['value'] = json.dumps(claim.getTarget()) elif claim.getType() == 'commonsMedia': params['value'] = json.dumps(claim.getTarget().title(withNamespace=False)) + elif claim.getType() == 'globecoordinate': + params['value'] = json.dumps(claim.getTarget().toWikibase()) else: raise NotImplementedError('%s datatype is not supported yet.' % claim.getType())
pywikipedia-svn@lists.wikimedia.org