def _refresh(self): try: reply = self._session.get( self.API_URL, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True) if reply.status_code == requests.codes.ok: json_reply = reply.json() territory = json_reply.get("country_code", None) timezone_source = "GeoIP" timezone_code = json_reply.get("time_zone", None) # check if the timezone returned by the API is valid if not is_valid_timezone(timezone_code): # try to get a timezone from the territory code timezone_code = get_preferred_timezone(territory) timezone_source = "territory code" if territory or timezone_code: self._set_result( LocationResult(territory_code=territory, timezone=timezone_code, timezone_source=timezone_source)) else: log.error( "Geoloc: Fedora GeoIP API lookup failed with status code: %s", reply.status_code) except requests.exceptions.RequestException as e: log.debug( "Geoloc: RequestException for Fedora GeoIP API lookup:\n%s", e) except ValueError as e: log.debug("Geoloc: Unable to decode GeoIP JSON:\n%s", e)
def _refresh(self): try: reply = requests.get(self.API_URL, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True) if reply.status_code == requests.codes.ok: json_reply = reply.json() territory = json_reply.get("country_code", None) timezone_source = "GeoIP" timezone_code = json_reply.get("time_zone", None) if timezone_code is not None: # the timezone code is returned as Unicode, # it needs to be converted to UTF-8 encoded string, # otherwise some string processing in Anaconda might fail timezone_code = timezone_code.encode("utf8") # check if the timezone returned by the API is valid if not is_valid_timezone(timezone_code): # try to get a timezone from the territory code timezone_code = get_preferred_timezone(territory) timezone_source = "territory code" if territory or timezone_code: self._set_result(LocationResult( territory_code=territory, timezone=timezone_code, timezone_source=timezone_source)) else: log.error("Geoloc: Fedora GeoIP API lookup failed with status code: %s", reply.status_code) except requests.exceptions.RequestException as e: log.debug("Geoloc: RequestException for Fedora GeoIP API lookup:\n%s", e) except ValueError as e: log.debug("Geoloc: Unable to decode GeoIP JSON:\n%s", e)
def _refresh(self): try: reply = urllib2.urlopen(self.API_URL, timeout= constants.NETWORK_CONNECTION_TIMEOUT) if reply: json_reply = json.load(reply) territory = json_reply.get("country_code", None) timezone_source = "GeoIP" timezone_code = json_reply.get("time_zone", None) if timezone_code is not None: # the timezone code is returned as Unicode, # it needs to be converted to UTF-8 encoded string, # otherwise some string processing in Anaconda might fail timezone_code = timezone_code.encode("utf8") # check if the timezone returned by the API is valid if not is_valid_timezone(timezone_code): # try to get a timezone from the territory code timezone_code = get_preferred_timezone(territory) timezone_source = "territory code" if territory or timezone_code: self._set_result(LocationResult( territory_code=territory, timezone=timezone_code, timezone_source=timezone_source)) except urllib2.HTTPError as e: log.debug("Geoloc: HTTPError for Fedora GeoIP API lookup:\n%s (%s)", e, self.API_URL) except urllib2.URLError as e: log.debug("Geoloc: URLError for Fedora GeoIP API lookup:\n%s (%s)", e, self.API_URL)
def apply(self): selected = self.builder.get_object("languageViewSelection") (store, itr) = selected.get_selected() lang = store[itr][2] self.language.select_translation(lang) self.data.lang.lang = lang # Skip timezone and keyboard default setting for kickstart installs. # The user may have provided these values via kickstart and if not, we # need to prompt for them. if flags.flags.automatedInstall: return lang_timezone = None # check if the geolocation lookup returned a time zone # (the geolocation module makes sure that the returned timezone is # either a valid timezone or None) geoloc_timezone = geoloc.get_timezone() if geoloc_timezone: lang_timezone = geoloc_timezone # if no data is provided by Geolocation, # try to get timezone from the current language elif self.language.territory and not self.data.timezone.timezone: lang_timezone = timezone.get_preferred_timezone(self.language.territory) if lang_timezone: self.data.timezone.timezone = lang_timezone lang_country = self.language.preferred_locale.territory self._set_keyboard_defaults(store[itr][1], lang_country)
def _refresh(self): try: reply = urllib2.urlopen( self.API_URL, timeout=constants.NETWORK_CONNECTION_TIMEOUT) if reply: json_reply = json.load(reply) territory = json_reply.get("country_code", None) timezone_source = "GeoIP" timezone_code = json_reply.get("time_zone", None) if timezone_code is not None: # the timezone code is returned as Unicode, # it needs to be converted to UTF-8 encoded string, # otherwise some string processing in Anaconda might fail timezone_code = timezone_code.encode("utf8") # check if the timezone returned by the API is valid if not is_valid_timezone(timezone_code): # try to get a timezone from the territory code timezone_code = get_preferred_timezone(territory) timezone_source = "territory code" if territory or timezone_code: self._set_result( LocationResult(territory_code=territory, timezone=timezone_code, timezone_source=timezone_source)) except urllib2.HTTPError as e: log.debug("Geoloc: HTTPError for Fedora GeoIP API lookup:\n%s", e) except urllib2.URLError as e: log.debug("Geoloc: URLError for Fedora GeoIP API lookup:\n%s", e) except ValueError as e: log.debug("Geoloc: Unable to decode GeoIP JSON:\n%s", e)
def _locate(self, url): """Geolocate the computer using the service at given URL :param str url: URL to query :return GeolocationData: data structure describing geolocation results """ try: log.info("Geoloc: querying the API") reply = requests.get(url, timeout=NETWORK_CONNECTION_TIMEOUT, verify=True) if reply.status_code == requests.codes.ok: # pylint: disable=no-member json_reply = reply.json() territory = json_reply.get("country_code", "") timezone = json_reply.get("time_zone", "") # check if the timezone returned by the API is valid if not is_valid_timezone(timezone): # try to get a timezone from the territory code timezone = get_preferred_timezone(territory) if territory or timezone: return GeolocationData.from_values( territory=territory, timezone=timezone, ) else: log.error("Geoloc: API lookup failed with status code: %s", reply.status_code) except requests.exceptions.RequestException as exc: log.debug("Geoloc: RequestException for API lookup:\n%s", exc) except ValueError as exc: log.debug("Geoloc: Unable to decode JSON:\n%s", exc) return GeolocationData()
def test_invalid_territory_zones(self): """Check if None is return for an invalid territory.""" self.assertIsNone(timezone.get_preferred_timezone("nonexistent"))
def test_string_valid_territory_zone(self): """Check if the returned value is string for a valid territory.""" zone = timezone.get_preferred_timezone("CZ") self.assertIsInstance(zone, str)
def invalid_territory_zones_test(self): """Check if None is return for an invalid territory.""" self.assertIsNone(timezone.get_preferred_timezone("nonexistent"))
def string_valid_territory_zone_test(self): """Check if the returned value is string for a valid territory.""" zone = timezone.get_preferred_timezone("CZ") self.assertIsInstance(zone, str)