Sync with oslo-incubator and add importutils
From oslo-incubator commit: c4bfdb94c25b4488da61d77184d97f8784f21a11 Change-Id: I81d1113d113faa609ab7713a0e04667b11786247
This commit is contained in:
parent
1eb7aba2c9
commit
b1663c96e6
@ -1,7 +1,9 @@
|
|||||||
[DEFAULT]
|
[DEFAULT]
|
||||||
|
|
||||||
# The list of modules to copy from openstack-common
|
# The list of modules to copy from openstack-common
|
||||||
|
module=gettextutils
|
||||||
module=install_venv_common
|
module=install_venv_common
|
||||||
|
module=importutils
|
||||||
module=strutils
|
module=strutils
|
||||||
|
|
||||||
# The base module to hold the copy of openstack.common
|
# The base module to hold the copy of openstack.common
|
||||||
|
@ -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)
|
|
||||||
t = gettext.translation(
|
|
||||||
domain,
|
|
||||||
localedir=self.localedir,
|
localedir=self.localedir,
|
||||||
fallback=True,
|
fallback=True)
|
||||||
)
|
# Use the appropriate method of the translation object based
|
||||||
if six.PY3:
|
# on the python version.
|
||||||
return t.gettext
|
m = t.gettext if six.PY3 else t.ugettext
|
||||||
return t.ugettext
|
|
||||||
|
def f(msg):
|
||||||
|
"""oslo.i18n.gettextutils translation function."""
|
||||||
|
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('openstackclient', 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, lazy=True)
|
tf = TranslatorFactory(domain)
|
||||||
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):
|
||||||
@ -373,8 +354,8 @@ def get_available_languages(domain):
|
|||||||
'zh_Hant_HK': 'zh_HK',
|
'zh_Hant_HK': 'zh_HK',
|
||||||
'zh_Hant': 'zh_TW',
|
'zh_Hant': 'zh_TW',
|
||||||
'fil': 'tl_PH'}
|
'fil': 'tl_PH'}
|
||||||
for (locale, alias) in six.iteritems(aliases):
|
for (locale_, alias) in six.iteritems(aliases):
|
||||||
if locale in language_list and alias not in language_list:
|
if locale_ in language_list and alias not in language_list:
|
||||||
language_list.append(alias)
|
language_list.append(alias)
|
||||||
|
|
||||||
_AVAILABLE_LANGUAGES[domain] = language_list
|
_AVAILABLE_LANGUAGES[domain] = language_list
|
||||||
|
73
openstackclient/openstack/common/importutils.py
Normal file
73
openstackclient/openstack/common/importutils.py
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
# Copyright 2011 OpenStack Foundation.
|
||||||
|
# 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
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
"""
|
||||||
|
Import related utilities and helper functions.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
|
||||||
|
def import_class(import_str):
|
||||||
|
"""Returns a class from a string including module and class."""
|
||||||
|
mod_str, _sep, class_str = import_str.rpartition('.')
|
||||||
|
__import__(mod_str)
|
||||||
|
try:
|
||||||
|
return getattr(sys.modules[mod_str], class_str)
|
||||||
|
except AttributeError:
|
||||||
|
raise ImportError('Class %s cannot be found (%s)' %
|
||||||
|
(class_str,
|
||||||
|
traceback.format_exception(*sys.exc_info())))
|
||||||
|
|
||||||
|
|
||||||
|
def import_object(import_str, *args, **kwargs):
|
||||||
|
"""Import a class and return an instance of it."""
|
||||||
|
return import_class(import_str)(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def import_object_ns(name_space, import_str, *args, **kwargs):
|
||||||
|
"""Tries to import object from default namespace.
|
||||||
|
|
||||||
|
Imports a class and return an instance of it, first by trying
|
||||||
|
to find the class in a default namespace, then failing back to
|
||||||
|
a full path if not found in the default namespace.
|
||||||
|
"""
|
||||||
|
import_value = "%s.%s" % (name_space, import_str)
|
||||||
|
try:
|
||||||
|
return import_class(import_value)(*args, **kwargs)
|
||||||
|
except ImportError:
|
||||||
|
return import_class(import_str)(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def import_module(import_str):
|
||||||
|
"""Import a module."""
|
||||||
|
__import__(import_str)
|
||||||
|
return sys.modules[import_str]
|
||||||
|
|
||||||
|
|
||||||
|
def import_versioned_module(version, submodule=None):
|
||||||
|
module = 'openstackclient.v%s' % version
|
||||||
|
if submodule:
|
||||||
|
module = '.'.join((module, submodule))
|
||||||
|
return import_module(module)
|
||||||
|
|
||||||
|
|
||||||
|
def try_import(import_str, default=None):
|
||||||
|
"""Try to import a module and if it fails return default."""
|
||||||
|
try:
|
||||||
|
return import_module(import_str)
|
||||||
|
except ImportError:
|
||||||
|
return default
|
@ -50,6 +50,39 @@ SLUGIFY_STRIP_RE = re.compile(r"[^\w\s-]")
|
|||||||
SLUGIFY_HYPHENATE_RE = re.compile(r"[-\s]+")
|
SLUGIFY_HYPHENATE_RE = re.compile(r"[-\s]+")
|
||||||
|
|
||||||
|
|
||||||
|
# NOTE(flaper87): The following globals are used by `mask_password`
|
||||||
|
_SANITIZE_KEYS = ['adminPass', 'admin_pass', 'password', 'admin_password']
|
||||||
|
|
||||||
|
# NOTE(ldbragst): Let's build a list of regex objects using the list of
|
||||||
|
# _SANITIZE_KEYS we already have. This way, we only have to add the new key
|
||||||
|
# to the list of _SANITIZE_KEYS and we can generate regular expressions
|
||||||
|
# for XML and JSON automatically.
|
||||||
|
_SANITIZE_PATTERNS_2 = []
|
||||||
|
_SANITIZE_PATTERNS_1 = []
|
||||||
|
|
||||||
|
# NOTE(amrith): Some regular expressions have only one parameter, some
|
||||||
|
# have two parameters. Use different lists of patterns here.
|
||||||
|
_FORMAT_PATTERNS_1 = [r'(%(key)s\s*[=]\s*)[^\s^\'^\"]+']
|
||||||
|
_FORMAT_PATTERNS_2 = [r'(%(key)s\s*[=]\s*[\"\']).*?([\"\'])',
|
||||||
|
r'(%(key)s\s+[\"\']).*?([\"\'])',
|
||||||
|
r'([-]{2}%(key)s\s+)[^\'^\"^=^\s]+([\s]*)',
|
||||||
|
r'(<%(key)s>).*?(</%(key)s>)',
|
||||||
|
r'([\"\']%(key)s[\"\']\s*:\s*[\"\']).*?([\"\'])',
|
||||||
|
r'([\'"].*?%(key)s[\'"]\s*:\s*u?[\'"]).*?([\'"])',
|
||||||
|
r'([\'"].*?%(key)s[\'"]\s*,\s*\'--?[A-z]+\'\s*,\s*u?'
|
||||||
|
'[\'"]).*?([\'"])',
|
||||||
|
r'(%(key)s\s*--?[A-z]+\s*)\S+(\s*)']
|
||||||
|
|
||||||
|
for key in _SANITIZE_KEYS:
|
||||||
|
for pattern in _FORMAT_PATTERNS_2:
|
||||||
|
reg_ex = re.compile(pattern % {'key': key}, re.DOTALL)
|
||||||
|
_SANITIZE_PATTERNS_2.append(reg_ex)
|
||||||
|
|
||||||
|
for pattern in _FORMAT_PATTERNS_1:
|
||||||
|
reg_ex = re.compile(pattern % {'key': key}, re.DOTALL)
|
||||||
|
_SANITIZE_PATTERNS_1.append(reg_ex)
|
||||||
|
|
||||||
|
|
||||||
def int_from_bool_as_string(subject):
|
def int_from_bool_as_string(subject):
|
||||||
"""Interpret a string as a boolean and return either 1 or 0.
|
"""Interpret a string as a boolean and return either 1 or 0.
|
||||||
|
|
||||||
@ -237,3 +270,42 @@ def to_slug(value, incoming=None, errors="strict"):
|
|||||||
"ascii", "ignore").decode("ascii")
|
"ascii", "ignore").decode("ascii")
|
||||||
value = SLUGIFY_STRIP_RE.sub("", value).strip().lower()
|
value = SLUGIFY_STRIP_RE.sub("", value).strip().lower()
|
||||||
return SLUGIFY_HYPHENATE_RE.sub("-", value)
|
return SLUGIFY_HYPHENATE_RE.sub("-", value)
|
||||||
|
|
||||||
|
|
||||||
|
def mask_password(message, secret="***"):
|
||||||
|
"""Replace password with 'secret' in message.
|
||||||
|
|
||||||
|
:param message: The string which includes security information.
|
||||||
|
:param secret: value with which to replace passwords.
|
||||||
|
:returns: The unicode value of message with the password fields masked.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
>>> mask_password("'adminPass' : 'aaaaa'")
|
||||||
|
"'adminPass' : '***'"
|
||||||
|
>>> mask_password("'admin_pass' : 'aaaaa'")
|
||||||
|
"'admin_pass' : '***'"
|
||||||
|
>>> mask_password('"password" : "aaaaa"')
|
||||||
|
'"password" : "***"'
|
||||||
|
>>> mask_password("'original_password' : 'aaaaa'")
|
||||||
|
"'original_password' : '***'"
|
||||||
|
>>> mask_password("u'original_password' : u'aaaaa'")
|
||||||
|
"u'original_password' : u'***'"
|
||||||
|
"""
|
||||||
|
message = six.text_type(message)
|
||||||
|
|
||||||
|
# NOTE(ldbragst): Check to see if anything in message contains any key
|
||||||
|
# specified in _SANITIZE_KEYS, if not then just return the message since
|
||||||
|
# we don't have to mask any passwords.
|
||||||
|
if not any(key in message for key in _SANITIZE_KEYS):
|
||||||
|
return message
|
||||||
|
|
||||||
|
substitute = r'\g<1>' + secret + r'\g<2>'
|
||||||
|
for pattern in _SANITIZE_PATTERNS_2:
|
||||||
|
message = re.sub(pattern, substitute, message)
|
||||||
|
|
||||||
|
substitute = r'\g<1>' + secret
|
||||||
|
for pattern in _SANITIZE_PATTERNS_1:
|
||||||
|
message = re.sub(pattern, substitute, message)
|
||||||
|
|
||||||
|
return message
|
||||||
|
Loading…
Reference in New Issue
Block a user