Permalink
Browse files

Merge "Move out of the oslo namespace package"

  • Loading branch information...
2 parents c86629e + ba05e9a commit 1215edcb8da4d616e0f84ea9c2cfd7cb46d882ea Jenkins committed with openstack-gerrit Dec 18, 2014
View
@@ -2,37 +2,37 @@
API
=====
-oslo.i18n
+oslo_i18n
=========
-.. automodule:: oslo.i18n
+.. automodule:: oslo_i18n
-.. autoclass:: oslo.i18n.TranslatorFactory
+.. autoclass:: oslo_i18n.TranslatorFactory
:members:
.. seealso::
An example of using a :class:`TranslatorFactory` is provided in
:ref:`integration-module`.
-.. autofunction:: oslo.i18n.enable_lazy
+.. autofunction:: oslo_i18n.enable_lazy
.. seealso::
:ref:`lazy-translation`
-.. autofunction:: oslo.i18n.translate
+.. autofunction:: oslo_i18n.translate
-.. autofunction:: oslo.i18n.get_available_languages
+.. autofunction:: oslo_i18n.get_available_languages
-oslo.i18n.log
+oslo_i18n.log
=============
-.. automodule:: oslo.i18n.log
+.. automodule:: oslo_i18n.log
:members:
-oslo.i18n.fixture
+oslo_i18n.fixture
=================
-.. automodule:: oslo.i18n.fixture
+.. automodule:: oslo_i18n.fixture
:members:
@@ -4,7 +4,7 @@
Text messages the user sees via exceptions or API calls should be
translated using
-:py:attr:`TranslatorFactory.primary <oslo.i18n.TranslatorFactory.primary>`, which should
+:py:attr:`TranslatorFactory.primary <oslo_i18n.TranslatorFactory.primary>`, which should
be installed as ``_()`` in the integration module.
.. seealso::
View
@@ -16,16 +16,16 @@ Creating an Integration Module
To use oslo.i18n in a project, you will need to create a small
integration module to hold an instance of
-:class:`~oslo.i18n.TranslatorFactory` and references to
+:class:`~oslo_i18n.TranslatorFactory` and references to
the marker functions the factory creates.
::
# app/i18n.py
- from oslo import i18n
+ import oslo_i18n
- _translators = i18n.TranslatorFactory(domain='myapp')
+ _translators = oslo_i18n.TranslatorFactory(domain='myapp')
# The primary translation function using the well-known name "_"
_ = _translators.primary
@@ -151,14 +151,14 @@ To enable lazy translation, call :func:`enable_lazy`.
::
- from oslo import i18n
+ import oslo_i18n
- i18n.enable_lazy()
+ oslo_i18n.enable_lazy()
Translating Messages
====================
-Use :func:`~oslo.i18n.translate` to translate strings to
+Use :func:`~oslo_i18n.translate` to translate strings to
a specific locale. :func:`translate` handles delayed translation and
strings that have already been translated immediately. It should be
used at the point where the locale to be used is known, which is often
@@ -167,9 +167,9 @@ emitted.
::
- from oslo import i18n
+ import oslo_i18n
- trans_msg = i18n.translate(msg, desired_locale=my_locale)
+ trans_msg = oslo_i18n.translate(msg, desired_locale=my_locale)
if desired_locale is not specified then the default locale is used.
@@ -178,14 +178,14 @@ Available Languages
Only the languages that have translations provided are available for
translation. To determine which languages are available the
-:func:`~oslo.i18n.get_available_languages` is provided. Since different languages
+:func:`~oslo_i18n.get_available_languages` is provided. Since different languages
can be installed for each domain, the domain must be specified.
::
- from oslo import i18n
+ import oslo_i18n
- avail_lang = i18n.get_available_languages('myapp')
+ avail_lang = oslo_i18n.get_available_languages('myapp')
.. seealso::
View
@@ -10,7 +10,22 @@
# License for the specific language governing permissions and limitations
# under the License.
-from ._factory import *
-from ._gettextutils import *
-from ._lazy import *
-from ._translate import *
+import warnings
+
+from oslo_i18n._factory import *
+from oslo_i18n._gettextutils import *
+from oslo_i18n._lazy import *
+from oslo_i18n._translate import *
+
+
+def deprecated():
+ new_name = __name__.replace('.', '_')
+ warnings.warn(
+ ('The oslo namespace package is deprecated. Please use %s instead.' %
+ new_name),
+ DeprecationWarning,
+ stacklevel=3,
+ )
+
+
+deprecated()
View
@@ -9,57 +9,5 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
-"""Test fixtures for working with oslo.i18n.
-"""
-
-import fixtures
-import six
-
-from oslo.i18n import _message
-
-
-class Translation(fixtures.Fixture):
- """Fixture for managing translatable strings.
-
- This class provides methods for creating translatable strings
- using both lazy translation and immediate translation. It can be
- used to generate the different types of messages returned from
- oslo.i18n to test code that may need to know about the type to
- handle them differently (for example, error handling in WSGI apps,
- or logging).
-
- Use this class to generate messages instead of toggling the global
- lazy flag and using the regular translation factory.
-
- """
-
- def __init__(self, domain='test-domain'):
- """Initialize the fixture.
-
- :param domain: The translation domain. This is not expected to
- coincide with an actual set of message
- catalogs, but it can.
- :type domain: str
- """
- self.domain = domain
-
- def lazy(self, msg):
- """Return a lazily translated message.
-
- :param msg: Input message string. May optionally include
- positional or named string interpolation markers.
- :type msg: str or unicode
-
- """
- return _message.Message(msg, domain=self.domain)
-
- def immediate(self, msg):
- """Return a string as though it had been translated immediately.
-
- :param msg: Input message string. May optionally include
- positional or named string interpolation markers.
- :type msg: str or unicode
-
- """
- return six.text_type(msg)
+from oslo_i18n.fixture import * # noqa
View
@@ -1,7 +1,3 @@
-# Copyright 2012 Red Hat, Inc.
-# Copyright 2013 IBM Corp.
-# All Rights Reserved.
-#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
@@ -14,84 +10,4 @@
# License for the specific language governing permissions and limitations
# under the License.
-"""logging utilities for translation
-"""
-
-from logging import handlers
-
-from oslo.i18n import _translate
-
-
-class TranslationHandler(handlers.MemoryHandler):
- """Handler that translates records before logging them.
-
- When lazy translation is enabled in the application (see
- :func:`~oslo.i18n.enable_lazy`), the :class:`TranslationHandler`
- uses its locale configuration setting to determine how to
- translate LogRecord objects before forwarding them to the
- logging.Handler.
-
- When lazy translation is disabled, the message in the LogRecord is
- converted to unicode without any changes and then forwarded to the
- logging.Handler.
-
- The handler can be configured declaratively in the
- ``logging.conf`` as follows::
-
- [handlers]
- keys = translatedlog, translator
-
- [handler_translatedlog]
- class = handlers.WatchedFileHandler
- args = ('/var/log/api-localized.log',)
- formatter = context
-
- [handler_translator]
- class = oslo.i18n.log.TranslationHandler
- target = translatedlog
- args = ('zh_CN',)
-
- If the specified locale is not available in the system, the handler will
- log in the default locale.
-
- """
-
- def __init__(self, locale=None, target=None):
- """Initialize a TranslationHandler
-
- :param locale: locale to use for translating messages
- :param target: logging.Handler object to forward
- LogRecord objects to after translation
- """
- # NOTE(luisg): In order to allow this handler to be a wrapper for
- # other handlers, such as a FileHandler, and still be able to
- # configure it using logging.conf, this handler has to extend
- # MemoryHandler because only the MemoryHandlers' logging.conf
- # parsing is implemented such that it accepts a target handler.
- handlers.MemoryHandler.__init__(self, capacity=0, target=target)
- self.locale = locale
-
- def setFormatter(self, fmt):
- self.target.setFormatter(fmt)
-
- def emit(self, record):
- # We save the message from the original record to restore it
- # after translation, so other handlers are not affected by this
- original_msg = record.msg
- original_args = record.args
-
- try:
- self._translate_and_log_record(record)
- finally:
- record.msg = original_msg
- record.args = original_args
-
- def _translate_and_log_record(self, record):
- record.msg = _translate.translate(record.msg, self.locale)
-
- # In addition to translating the message, we also need to translate
- # arguments that were passed to the log method that were not part
- # of the main message e.g., log.info(_('Some message %s'), this_one))
- record.args = _translate.translate_args(record.args, self.locale)
-
- self.target.emit(record)
+from oslo_i18n.log import * # noqa
View
@@ -0,0 +1,16 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from ._factory import *
+from ._gettextutils import *
+from ._lazy import *
+from ._translate import *
@@ -21,9 +21,9 @@
import six
-from oslo.i18n import _lazy
-from oslo.i18n import _locale
-from oslo.i18n import _message
+from oslo_i18n import _lazy
+from oslo_i18n import _locale
+from oslo_i18n import _message
__all__ = [
@@ -75,7 +75,7 @@ def _make_translation_func(self, domain=None):
m = t.gettext if six.PY3 else t.ugettext
def f(msg):
- """oslo.i18n.gettextutils translation function."""
+ """oslo_i18n.gettextutils translation function."""
if _lazy.USE_LAZY:
return _message.Message(msg, domain=domain)
return m(msg)
@@ -24,8 +24,8 @@
from babel import localedata
import six
-from oslo.i18n import _factory
-from oslo.i18n import _locale
+from oslo_i18n import _factory
+from oslo_i18n import _locale
__all__ = [
'install',
@@ -16,10 +16,10 @@
"""Translation support for messages in this library.
"""
-from oslo.i18n import _factory
+from oslo_i18n import _factory
# Create the global translation functions.
-_translators = _factory.TranslatorFactory('oslo.i18n')
+_translators = _factory.TranslatorFactory('oslo_i18n')
# The primary translation function using the well-known name "_"
_ = _translators.primary
File renamed without changes.
File renamed without changes.
@@ -23,8 +23,8 @@
import six
-from oslo.i18n import _locale
-from oslo.i18n import _translate
+from oslo_i18n import _locale
+from oslo_i18n import _translate
class Message(six.text_type):
@@ -149,7 +149,7 @@ def _copy_param(self, param):
return six.text_type(param)
def __add__(self, other):
- from oslo.i18n._i18n import _
+ from oslo_i18n._i18n import _
msg = _('Message objects do not support addition.')
raise TypeError(msg)
@@ -160,7 +160,7 @@ def __radd__(self, other):
def __str__(self):
# NOTE(luisg): Logging in python 2.6 tries to str() log records,
# and it expects specifically a UnicodeError in order to proceed.
- from oslo.i18n._i18n import _
+ from oslo_i18n._i18n import _
msg = _('Message objects do not support str() because they may '
'contain non-ascii characters. '
'Please use unicode() or translate() instead.')
Oops, something went wrong.

0 comments on commit 1215edc

Please sign in to comment.