Update oslo-incubator to 037dee004c3e2239

This brought in/includes the following changes:

ad248f6658d5 Specify namedtuple_as_object=False when using simplejson
3d90045d2d Backport code for i18n to check lazy at runtime

Change-Id: I6ffb317f7e8fa57d79a7d0c6a9fc2517bc169e4b
This commit is contained in:
Joshua Harlow
2014-08-14 18:13:53 -07:00
parent 1f6e1aa8e3
commit 2b15d09dc1
2 changed files with 32 additions and 41 deletions

View File

@@ -23,7 +23,6 @@ Usual usage in an openstack.common module:
""" """
import copy import copy
import functools
import gettext import gettext
import locale import locale
from logging import handlers from logging import handlers
@@ -42,7 +41,7 @@ class TranslatorFactory(object):
"""Create translator functions """Create translator functions
""" """
def __init__(self, domain, lazy=False, localedir=None): def __init__(self, domain, localedir=None):
"""Establish a set of translation functions for the domain. """Establish a set of translation functions for the domain.
:param domain: Name of translation domain, :param domain: Name of translation domain,
@@ -55,7 +54,6 @@ class TranslatorFactory(object):
:type localedir: str :type localedir: str
""" """
self.domain = domain self.domain = domain
self.lazy = lazy
if localedir is None: if localedir is None:
localedir = os.environ.get(domain.upper() + '_LOCALEDIR') localedir = os.environ.get(domain.upper() + '_LOCALEDIR')
self.localedir = localedir self.localedir = localedir
@@ -75,16 +73,19 @@ class TranslatorFactory(object):
""" """
if domain is None: if domain is None:
domain = self.domain domain = self.domain
if self.lazy: t = gettext.translation(domain,
return functools.partial(Message, domain=domain) localedir=self.localedir,
t = gettext.translation( fallback=True)
domain, # Use the appropriate method of the translation object based
localedir=self.localedir, # on the python version.
fallback=True, m = t.gettext if six.PY3 else t.ugettext
)
if six.PY3: def f(msg):
return t.gettext """oslo.i18n.gettextutils translation function."""
return t.ugettext if USE_LAZY:
return Message(msg, domain=domain)
return m(msg)
return f
@property @property
def primary(self): def primary(self):
@@ -147,19 +148,11 @@ def enable_lazy():
your project is importing _ directly instead of using the your project is importing _ directly instead of using the
gettextutils.install() way of importing the _ function. gettextutils.install() way of importing the _ function.
""" """
# FIXME(dhellmann): This function will be removed in oslo.i18n, global USE_LAZY
# because the TranslatorFactory makes it superfluous.
global _, _LI, _LW, _LE, _LC, USE_LAZY
tf = TranslatorFactory('taskflow', lazy=True)
_ = tf.primary
_LI = tf.log_info
_LW = tf.log_warning
_LE = tf.log_error
_LC = tf.log_critical
USE_LAZY = True USE_LAZY = True
def install(domain, lazy=False): def install(domain):
"""Install a _() function using the given translation domain. """Install a _() function using the given translation domain.
Given a translation domain, install a _() function using gettext's Given a translation domain, install a _() function using gettext's
@@ -170,26 +163,14 @@ def install(domain, lazy=False):
a translation-domain-specific environment variable (e.g. a translation-domain-specific environment variable (e.g.
NOVA_LOCALEDIR). NOVA_LOCALEDIR).
Note that to enable lazy translation, enable_lazy must be
called.
:param domain: the translation domain :param domain: the translation domain
:param lazy: indicates whether or not to install the lazy _() function.
The lazy _() introduces a way to do deferred translation
of messages by installing a _ that builds Message objects,
instead of strings, which can then be lazily translated into
any available locale.
""" """
if lazy: from six import moves
from six import moves tf = TranslatorFactory(domain)
tf = TranslatorFactory(domain, lazy=True) moves.builtins.__dict__['_'] = tf.primary
moves.builtins.__dict__['_'] = tf.primary
else:
localedir = '%s_LOCALEDIR' % domain.upper()
if six.PY3:
gettext.install(domain,
localedir=os.environ.get(localedir))
else:
gettext.install(domain,
localedir=os.environ.get(localedir),
unicode=True)
class Message(six.text_type): class Message(six.text_type):

View File

@@ -38,11 +38,13 @@ import inspect
import itertools import itertools
import sys import sys
is_simplejson = False
if sys.version_info < (2, 7): if sys.version_info < (2, 7):
# On Python <= 2.6, json module is not C boosted, so try to use # On Python <= 2.6, json module is not C boosted, so try to use
# simplejson module if available # simplejson module if available
try: try:
import simplejson as json import simplejson as json
is_simplejson = True
except ImportError: except ImportError:
import json import json
else: else:
@@ -165,9 +167,17 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
def dumps(value, default=to_primitive, **kwargs): def dumps(value, default=to_primitive, **kwargs):
if is_simplejson:
kwargs['namedtuple_as_object'] = False
return json.dumps(value, default=default, **kwargs) return json.dumps(value, default=default, **kwargs)
def dump(obj, fp, *args, **kwargs):
if is_simplejson:
kwargs['namedtuple_as_object'] = False
return json.dump(obj, fp, *args, **kwargs)
def loads(s, encoding='utf-8', **kwargs): def loads(s, encoding='utf-8', **kwargs):
return json.loads(strutils.safe_decode(s, encoding), **kwargs) return json.loads(strutils.safe_decode(s, encoding), **kwargs)