changeset 7751:d4bcbe1b06f4

lib/locale: move locale check to separate file Create kallithea.lib.locale to group locale-related wrappers/checks. As a first step, move existing locale-related logic into it. A subsequent commit will add more.
author Thomas De Schampheleire <thomas.de_schampheleire@nokia.com>
date Mon, 24 Jun 2019 21:42:02 +0200
parents f2900ebaac0d
children 3929ff3f21c6
files kallithea/config/app_cfg.py kallithea/lib/locale.py
diffstat 2 files changed, 39 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/config/app_cfg.py	Mon Jun 24 19:42:02 2019 +0200
+++ b/kallithea/config/app_cfg.py	Mon Jun 24 21:42:02 2019 +0200
@@ -30,6 +30,7 @@
 from sqlalchemy import create_engine
 import mercurial
 
+import kallithea.lib.locale
 from kallithea.lib.middleware.permanent_repo_url import PermanentRepoUrl
 from kallithea.lib.middleware.https_fixup import HttpsFixup
 from kallithea.lib.middleware.simplegit import SimpleGit
@@ -121,19 +122,7 @@
 def setup_configuration(app):
     config = app.config
 
-    # Verify that things work when Dulwich passes unicode paths to the file system layer.
-    # Note: UTF-8 is preferred, but for example ISO-8859-1 or mbcs should also work under the right cirumstances.
-    try:
-        u'\xe9'.encode(sys.getfilesystemencoding()) # Test using é (&eacute;)
-    except UnicodeEncodeError:
-        log.error("Cannot encode Unicode paths to file system encoding %r", sys.getfilesystemencoding())
-        for var in ['LC_ALL', 'LC_CTYPE', 'LANG']:
-            if var in os.environ:
-                val = os.environ[var]
-                log.error("Note: Environment variable %s is %r - perhaps change it to some other value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'", var, val)
-                break
-        else:
-            log.error("Note: No locale setting found in environment variables - perhaps set LC_CTYPE to some value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'")
+    if not kallithea.lib.locale.current_locale_is_valid():
         log.error("Terminating ...")
         sys.exit(1)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/lib/locale.py	Mon Jun 24 21:42:02 2019 +0200
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+import logging
+import os
+import sys
+
+log = logging.getLogger(__name__)
+
+def current_locale_is_valid():
+    """Verify that things work when Dulwich passes unicode paths to the file system layer.
+
+    Note: UTF-8 is preferred, but for example ISO-8859-1 or mbcs should also
+    work under the right circumstances."""
+    try:
+        u'\xe9'.encode(sys.getfilesystemencoding()) # Test using é (&eacute;)
+    except UnicodeEncodeError:
+        log.error("Cannot encode Unicode paths to file system encoding %r", sys.getfilesystemencoding())
+        for var in ['LC_ALL', 'LC_CTYPE', 'LANG']:
+            if var in os.environ:
+                val = os.environ[var]
+                log.error("Note: Environment variable %s is %r - perhaps change it to some other value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'", var, val)
+                break
+        else:
+            log.error("Note: No locale setting found in environment variables - perhaps set LC_CTYPE to some value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'")
+        return False
+    return True