| Total Complexity | 7 |
| Total Lines | 26 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # -*- coding: utf-8 -*- |
||
| 22 | class Geolocator(object): |
||
| 23 | def __init__(self): |
||
| 24 | self._geocoder = geocoders.Nominatim(timeout=5, country_bias='fr') |
||
| 25 | |||
| 26 | def geocode(self, address): |
||
| 27 | if not isinstance(address, basestring) \ |
||
| 28 | and not isinstance(address, dict): |
||
| 29 | err_msg = u"address should either be of type: %s, or of type %s." \ |
||
| 30 | % (basestring, dict) |
||
| 31 | raise TypeError(err_msg) |
||
| 32 | |||
| 33 | try: |
||
| 34 | geolocation = self._geocoder.geocode(address) |
||
| 35 | |||
| 36 | if not geolocation: |
||
| 37 | err_msg = u"Couldn't resolve the following address: '%s'" \ |
||
| 38 | % address |
||
| 39 | raise GeolocationFailure(err_msg) |
||
| 40 | except (exc.GeocoderQuotaExceeded, |
||
| 41 | exc.GeocoderUnavailable, |
||
| 42 | exc.GeocoderTimedOut) as e: |
||
| 43 | raise TemporaryError(u'Geolocator error: %s' % e) |
||
| 44 | except exc.GeocoderServiceError as e: |
||
| 45 | raise GeolocationError(u'Geolocator error: %s' % e) |
||
| 46 | |||
| 47 | return geolocation |
||
| 48 |