[PATCH 3/5] first attempt to use GeoIP information

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



---
 pyanaconda/iw/language_gui.py            |   20 ++++++++++++--
 pyanaconda/language.py                   |   43 +++++++++++++++++++++++++----
 pyanaconda/localeinfo.py                 |    7 ++++-
 scripts/getlangnames.py                  |    2 +-
 tests/pyanaconda_test/language_test.py   |   22 ++++++++++++---
 tests/pyanaconda_test/localeinfo_test.py |    2 +-
 6 files changed, 79 insertions(+), 17 deletions(-)

diff --git a/pyanaconda/iw/language_gui.py b/pyanaconda/iw/language_gui.py
index 34aa88d..245c5fa 100644
--- a/pyanaconda/iw/language_gui.py
+++ b/pyanaconda/iw/language_gui.py
@@ -69,6 +69,17 @@ class LanguageWindow (InstallWindow):
 
     # LanguageWindow tag="lang"
     def getScreen (self, anaconda):
+	self.instLang = anaconda.instLanguage
+
+        wait_window = gui.WaitWindow(_("Getting GeoIP information"), _("Trying to get GeoIP information."))
+        current = None
+        try:
+            guess = self.instLang.getGeoIPinfo()
+            current = self.instLang.getCountryLang(guess)
+        except ValueError:
+            pass
+        wait_window.pop()
+
         mainBox = gtk.VBox (False, 10)
 
         hbox = gtk.HBox(False, 5)
@@ -84,8 +95,6 @@ class LanguageWindow (InstallWindow):
         label.set_size_request(350, -1)
         hbox.pack_start(label, False)
 
-	self.instLang = anaconda.instLanguage
-
         self.listStore = gtk.ListStore(gobject.TYPE_STRING,
                                        gobject.TYPE_STRING,
                                        gobject.TYPE_STRING)
@@ -107,7 +116,12 @@ class LanguageWindow (InstallWindow):
         self.listView.append_column(col)
         self.listView.set_property("headers-visible", False)
 
-        current = self.instLang.getLangName(self.instLang.instLang)
+        if not current:
+            try:
+                current = self.instLang.getLangName(self.instLang.instLang)
+            except ValueError:
+                current = self.instLang.defaultLangName
+
         iter = self.listStore.get_iter_first()
         while iter:
             if self.listStore.get_value(iter, 1) == current:
diff --git a/pyanaconda/language.py b/pyanaconda/language.py
index 8a0d7f6..3ebacb3 100644
--- a/pyanaconda/language.py
+++ b/pyanaconda/language.py
@@ -31,9 +31,15 @@ import localeinfo
 from simpleconfig import SimpleConfigFile
 import system_config_keyboard.keyboard as keyboard
 
+import httplib
+import pyanaconda.network
+
 import logging
 log = logging.getLogger("anaconda")
 
+MIRROR_MANAGER_URL = "mirrors.fedoraproject.org"
+MIRROR_LIST_REQUEST = "/mirrorlist?repo=fedora-15&arch=i386"
+
 def langComponents(astring):
     pattern = re.compile("(?P<language>[A-Za-z]+)(_(?P<territory>[A-Za-z]+))?(\.(?P<codeset>[-\w]+))?(@(?P<modifier>[-\w]+))?")
     m = pattern.match(astring)
@@ -137,7 +143,7 @@ class Language(object):
                 f.close()
                 break
 
-        self.localeInfo = localeinfo.get(self._default)
+        (self.localeInfo, self.countriesLangs) = localeinfo.get(self._default)
 
         # instLang must be set after localeInfo is populated, in case the
         # current setting is unsupported by anaconda..
@@ -225,11 +231,8 @@ class Language(object):
         return self.localeInfo[l][2]
 
     def getLangName(self, lang):
-        try:
-            l = self._canonLang(lang)
-        except ValueError:
-            l = self._default
-
+        """@raise ValueError: if lang is not recognized"""
+        l = self._canonLang(lang)
         return self.localeInfo[l][0]
 
     def getLangByName(self, name):
@@ -251,3 +254,31 @@ class Language(object):
 
     def writeKS(self, f):
         f.write("lang %s\n" % self.info['LANG'])
+
+    def getCountryLang(self, country_code):
+        return self.countriesLangs.get(country_code, self.defaultLangName)
+
+    @property
+    def defaultLangName(self):
+        return self.localeInfo[self._default][0]
+
+    def getGeoIPinfo(self):
+        if not pyanaconda.network.hasActiveNetDev():
+            log.info("Cannot get GeoIP information: network down")
+            raise ValueError
+        conn = httplib.HTTPSConnection(MIRROR_MANAGER_URL)
+        conn.request("GET", MIRROR_LIST_REQUEST)
+        response = conn.getresponse()
+        data = response.read()
+        lines = data.split("\n")
+
+        match = re.search(r'country\s+=\s+([A-Z]+)', lines[0])
+        if match:
+            country = match.group(1).upper()
+            log.debug("Country guessed by GeoIP: {0}".format(country))
+            return country
+        else:
+            log.info("Cannot get GeoIP information: {0} {1}".format(
+                            response.status, response.reason))
+            raise ValueError
+
diff --git a/pyanaconda/localeinfo.py b/pyanaconda/localeinfo.py
index eefdf2e..9f90770 100644
--- a/pyanaconda/localeinfo.py
+++ b/pyanaconda/localeinfo.py
@@ -30,6 +30,7 @@ import string
 
 def get(default):
     localeInfo = {}
+    countriesLangs = {} #country code (e.g. US) -> language code (e.g. en)
     # nick -> (name, short name, font, keyboard, timezone) mapping
     search = ('lang-table', '/tmp/updates/lang-table', '/etc/lang-table',
               '/usr/share/anaconda/lang-table')
@@ -46,12 +47,16 @@ def get(default):
 
                 localeInfo[l[3]] = (l[0], l[1], l[2], l[4], string.strip(l[5]))
 
+                if len(l) > 6:
+                    for country in l[6].split(","):
+                        countriesLangs[country] = l[0]
+
             f.close()
             break
 
     # Hard code this to prevent errors in the build environment.
     localeInfo['C'] = localeInfo[default]
-    return localeInfo
+    return (localeInfo, countriesLangs)
 
 # Converts a single language into a "language search path". For example,
 # fr_FR.utf8@euro would become "fr_FR.utf8@euro fr_FR.utf8 fr_FR fr"
diff --git a/scripts/getlangnames.py b/scripts/getlangnames.py
index 2e579a4..b794f5f 100644
--- a/scripts/getlangnames.py
+++ b/scripts/getlangnames.py
@@ -23,7 +23,7 @@ import localeinfo
 
 import gettext
 
-localeInfo = localeinfo.get("en_US.UTF-8")
+localeInfo = localeinfo.get("en_US.UTF-8")[0]
 names = {}
 for k in localeInfo.keys():
     found = False
diff --git a/tests/pyanaconda_test/language_test.py b/tests/pyanaconda_test/language_test.py
index daf500b..1eb31db 100644
--- a/tests/pyanaconda_test/language_test.py
+++ b/tests/pyanaconda_test/language_test.py
@@ -211,26 +211,38 @@ class LanguageTest(mock.TestCase):
     def get_lang_name_1_test(self):
         import pyanaconda.language
         lang = pyanaconda.language.Language()
-        ret = lang.getLangName('en')
+        try:
+            ret = lang.getLangName('en')
+        except ValueError:
+            ret = lang.defaultLangName
         self.assertEqual(ret, 'English')
 
     def get_lang_name_2_test(self):
         import pyanaconda.language
         lang = pyanaconda.language.Language()
-        ret = lang.getLangName('cs')
+        try:
+            ret = lang.getLangName('cs')
+        except ValueError:
+            ret = lang.defaultLangName
         self.assertEqual(ret, 'Czech')
 
     def get_lang_name_3_test(self):
         import pyanaconda.language
         lang = pyanaconda.language.Language()
-        ret = lang.getLangName('he')
+        try:
+            ret = lang.getLangName('he')
+        except ValueError:
+            ret = lang.defaultLangName
         self.assertEqual(ret, 'Hebrew')
 
     def get_lang_name_4_test(self):
         import pyanaconda.language
         lang = pyanaconda.language.Language()
-        ret = lang.getLangName('foo')
-        self.assertEqual(ret, 'English')
+        try:
+            ret = lang.getLangName('foo')
+        except ValueError:
+            ret = lang.defaultLangName
+        self.assertEqual(ret, lang.defaultLangName)
 
     def get_lang_by_name_1_test(self):
         import pyanaconda.language
diff --git a/tests/pyanaconda_test/localeinfo_test.py b/tests/pyanaconda_test/localeinfo_test.py
index 32f0916..bd42444 100644
--- a/tests/pyanaconda_test/localeinfo_test.py
+++ b/tests/pyanaconda_test/localeinfo_test.py
@@ -26,7 +26,7 @@ English	en	latarcyrheb-sun16	en_US.UTF-8	us	America/New_York\n
 Hebrew	he	none	he_IL.UTF-8	us	Asia/Jerusalem"""
         self.take_over_io(fs, localeinfo)
 
-        info = localeinfo.get("en_US.UTF-8")
+        info = localeinfo.get("en_US.UTF-8")[0]
         self.assertEqual(
             info,
             {'C': ('English', 'en', 'latarcyrheb-sun16', 'us', 'America/New_York'),
-- 
1.7.4.4

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list


[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux