Browse Source

HUE-2043 [core] database options keys have unicode keys

Moved the dictionary logic from librdbms to python_util.
Abraham Elmahrek 11 years ago
parent
commit
c2ec0e6

+ 21 - 0
desktop/core/src/desktop/lib/python_util.py

@@ -21,6 +21,7 @@ import select
 import socket
 from django.utils.translation import ugettext as _
 from desktop import conf
+from desktop.lib.i18n import smart_str
 
 
 __all__ = ['CaseInsensitiveDict', 'create_synchronous_io_multiplexer']
@@ -95,3 +96,23 @@ def find_unused_port():
   _, port = sock.getsockname()
   sock.close()
   return port
+
+
+def force_dict_to_strings(dictionary):
+  if not dictionary:
+    return dictionary
+
+  new_dict = {}
+  for k in dictionary:
+    new_key = smart_str(k)
+    if isinstance(dictionary[k], basestring):
+      # Strings should not be unicode.
+      new_dict[new_key] = smart_str(dictionary[k])
+    elif isinstance(dictionary[k], dict):
+      # Recursively force dicts to strings.
+      new_dict[new_key] = force_dict_to_strings(dictionary[k])
+    else:
+      # Normal objects, or other literals, should not be converted.
+      new_dict[new_key] = dictionary[k]
+
+  return new_dict

+ 12 - 1
desktop/core/src/desktop/lib/python_util_test.py

@@ -20,7 +20,7 @@ import datetime
 
 from nose.tools import assert_true, assert_equal, assert_not_equal
 
-from desktop.lib.python_util import CaseInsensitiveDict
+from desktop.lib.python_util import CaseInsensitiveDict, force_dict_to_strings
 
 
 class TestPythonUtil(object):
@@ -33,3 +33,14 @@ class TestPythonUtil(object):
     assert_equal("Test", d['test'])
     assert_not_equal("test", d['Test'])
     assert_not_equal("test", d['test'])
+
+  def test_force_dict_to_strings(self):
+    unicode_dict = {u'test': u'test'}
+    string_dict = {'test': 'test'}
+    transformed_dict = force_dict_to_strings(unicode_dict)
+    assert_equal(string_dict, transformed_dict)
+
+    unicode_dict = {u'test': {u'test': u'test'}}
+    string_dict = {'test': {'test': 'test'}}
+    transformed_dict = force_dict_to_strings(unicode_dict)
+    assert_equal(string_dict, transformed_dict)

+ 2 - 1
desktop/core/src/desktop/settings.py

@@ -28,6 +28,7 @@ import pkg_resources
 import desktop.conf
 import desktop.log
 from desktop.lib.paths import get_desktop_root
+from desktop.lib.python_util import force_dict_to_strings
 
 
 HUE_DESKTOP_VERSION = pkg_resources.get_distribution("desktop").version or "Unknown"
@@ -271,7 +272,7 @@ else:
     "PASSWORD" : desktop.conf.DATABASE.PASSWORD.get(),
     "HOST" : desktop.conf.DATABASE.HOST.get(),
     "PORT" : str(desktop.conf.DATABASE.PORT.get()),
-    "OPTIONS": desktop.conf.DATABASE.OPTIONS.get(),
+    "OPTIONS": force_dict_to_strings(desktop.conf.DATABASE.OPTIONS.get()),
     # DB used for tests
     "TEST_NAME" : get_desktop_root('desktop-test.db')
   }

+ 1 - 21
desktop/libs/librdbms/src/librdbms/server/dbms.py

@@ -17,7 +17,7 @@
 
 import logging
 
-from desktop.lib.i18n import smart_str
+from desktop.lib.python_util import force_dict_to_strings
 
 from librdbms.conf import DATABASES
 
@@ -30,26 +30,6 @@ SQLITE = 'sqlite'
 ORACLE = 'oracle'
 
 
-def force_dict_to_strings(dictionary):
-  if not dictionary:
-    return dictionary
-
-  new_dict = {}
-  for k in dictionary:
-    new_key = smart_str(k)
-    if isinstance(dictionary[k], basestring):
-      # Strings should not be unicode.
-      new_dict[new_key] = smart_str(dictionary[k])
-    elif isinstance(dictionary[k], dict):
-      # Recursively force dicts to strings.
-      new_dict[new_key] = force_dict_to_strings(dictionary[k])
-    else:
-      # Normal objects, or other literals, should not be converted.
-      new_dict[new_key] = dictionary[k]
-
-  return new_dict
-
-
 def get(user, query_server=None):
   if query_server is None:
     query_server = get_query_server_config()