[PATCH 1/4] Add geolocation module

Brian C. Lane bcl at redhat.com
Fri Apr 5 18:16:38 UTC 2013


On Wed, Apr 03, 2013 at 05:45:58PM +0200, Martin Kolman wrote:
> The geolocation module exposes an API that makes it possible to get current territory code using either GeoIP or alternativelly nearby WiFi accesspoints. The module has multiple location backends, the default backend is using the Fedora MirrorManager.
> The geolocation module is a singleton, that is instantiated
> by the init_geolocation() call and refreshes geolocation info
> using a thread after refresh() is called.
> 
> Signed-off-by: Martin Kolman <mkolman at gmail.com>
> ---
>  pyanaconda/constants.py |   1 +
>  pyanaconda/geoloc.py    | 715 ++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 716 insertions(+)
>  create mode 100644 pyanaconda/geoloc.py

> +
> +MIRRORMANAGER_GEOIP_PROVIDER = 1
> +GOOGLE_WIFI_LOCATION = 2
> +HOSTIP_GEOIP_PROVIDER = 3
> +
> +GEOCODER_NOMINATIM = 1
> +
> +DEFAULT_GEOCODER = GEOCODER_NOMINATIM
> +DEFAULT_PROVIDER = MIRRORMANAGER_GEOIP_PROVIDER

Should these go here or in constants? I'm not sure.

> +def get_territory_code():
> +    """
> +    Returns the current country code or None if it is not known.
> +    None might also be returned even if refresh was called, but
> +    the look-up is still in progress.
> +
> +    @return current country code or None if not known
> +    """
> +    if location_info_instance:
> +        return location_info_instance.get_territory_code()
> +    else:
> +        raise GeolocationError("init_geolocation() not called")
> +        return None

return after raise won't get hit.

> +def _get_provider(provider):
> +    """
> +    Return GeoIP provider instance based on the provider ID
> +    """
> +    if provider == MIRRORMANAGER_GEOIP_PROVIDER:
> +        return MirrorManagerGeoIPProvider()
> +    elif provider == GOOGLE_WIFI_LOCATION:
> +        return GoogleWiFiLocationProvider()
> +    elif provider == HOSTIP_GEOIP_PROVIDER:
> +        return HostipGeoIPProvider()

I'd make this a dict mapping, and also handle unknown providers.

> +class LocationInfo(object):
> +    """
> +    Determines current location based on IP address or
> +    nearby WiFi access points (depending on what backend is used)
> +    """
> +
> +    def __init__(self, provider_id=DEFAULT_PROVIDER, refresh_now=False):
> +        """
> +        @param provider_id: GeoIP provider id specified by module constant
> +        @param refresh_now: if True, a GeoIP information refresh will be
> +                    done once the class is initialized
> +        """
> +        self._provider = None
> +        self._provider = _get_provider(provider_id)

Set self._provider twice

> +    def _refresh(self):
> +        try:
> +            reply = urllib2.urlopen(self.API_URL)

Do we want to set the timeout here? I'm not sure what the global default
it. It will be running in a thread so may not really matter.

> +            if device_type == self.NETWORK_MANAGER_DEVICE_TYPE_WIFI:
> +                # iterate over all APs
> +                for ap_path in device.GetAccessPoints():
> +                    network = bus.get_object('org.freedesktop.NetworkManager',
> +                                             ap_path)
> +                    network_properties = dbus.Interface(
> +                        network,
> +                        dbus_interface='org.freedesktop.DBus.Properties')
> +                    bssid = str(network_properties.Get(
> +                        "org.freedesktop.NetworkManager.AccessPoint",
> +                        "HwAddress"))
> +                    essid = str(network_properties.Get(
> +                                "org.freedesktop.NetworkManager.AccessPoint",
> +                                "Ssid", byte_arrays=True))
> +                    rssi = int(network_properties.Get(
> +                        "org.freedesktop.NetworkManager.AccessPoint",
> +                        "Strength"))

The line wrapping in this block makes it hard to read.

-- 
Brian C. Lane | Anaconda Team | IRC: bcl #anaconda | Port Orchard, WA (PST8PDT)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 482 bytes
Desc: not available
URL: <https://lists.fedorahosted.org/pipermail/anaconda-patches/attachments/20130405/1bd015ab/attachment.sig>


More information about the anaconda-patches mailing list