Merge "Synchronize code from oslo"
This commit is contained in:
commit
2925d5bbda
novaclient/openstack/common
50
novaclient/openstack/common/gettextutils.py
Normal file
50
novaclient/openstack/common/gettextutils.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||||
|
|
||||||
|
# Copyright 2012 Red Hat, Inc.
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
"""
|
||||||
|
gettext for openstack-common modules.
|
||||||
|
|
||||||
|
Usual usage in an openstack.common module:
|
||||||
|
|
||||||
|
from novaclient.openstack.common.gettextutils import _
|
||||||
|
"""
|
||||||
|
|
||||||
|
import gettext
|
||||||
|
import os
|
||||||
|
|
||||||
|
_localedir = os.environ.get('novaclient'.upper() + '_LOCALEDIR')
|
||||||
|
_t = gettext.translation('novaclient', localedir=_localedir, fallback=True)
|
||||||
|
|
||||||
|
|
||||||
|
def _(msg):
|
||||||
|
return _t.ugettext(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def install(domain):
|
||||||
|
"""Install a _() function using the given translation domain.
|
||||||
|
|
||||||
|
Given a translation domain, install a _() function using gettext's
|
||||||
|
install() function.
|
||||||
|
|
||||||
|
The main difference from gettext.install() is that we allow
|
||||||
|
overriding the default localedir (e.g. /usr/share/locale) using
|
||||||
|
a translation-domain-specific environment variable (e.g.
|
||||||
|
NOVA_LOCALEDIR).
|
||||||
|
"""
|
||||||
|
gettext.install(domain,
|
||||||
|
localedir=os.environ.get(domain.upper() + '_LOCALEDIR'),
|
||||||
|
unicode=True)
|
@ -21,6 +21,12 @@ System-level utilities and helper functions.
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from novaclient.openstack.common.gettextutils import _
|
||||||
|
|
||||||
|
|
||||||
|
TRUE_STRINGS = ('1', 't', 'true', 'on', 'y', 'yes')
|
||||||
|
FALSE_STRINGS = ('0', 'f', 'false', 'off', 'n', 'no')
|
||||||
|
|
||||||
|
|
||||||
def int_from_bool_as_string(subject):
|
def int_from_bool_as_string(subject):
|
||||||
"""
|
"""
|
||||||
@ -37,23 +43,37 @@ def int_from_bool_as_string(subject):
|
|||||||
return bool_from_string(subject) and 1 or 0
|
return bool_from_string(subject) and 1 or 0
|
||||||
|
|
||||||
|
|
||||||
def bool_from_string(subject):
|
def bool_from_string(subject, strict=False):
|
||||||
"""
|
"""
|
||||||
Interpret a string as a boolean.
|
Interpret a string as a boolean.
|
||||||
|
|
||||||
Any string value in:
|
A case-insensitive match is performed such that strings matching 't',
|
||||||
|
'true', 'on', 'y', 'yes', or '1' are considered True and, when
|
||||||
|
`strict=False`, anything else is considered False.
|
||||||
|
|
||||||
('True', 'true', 'On', 'on', 'Yes', 'yes', '1')
|
Useful for JSON-decoded stuff and config file parsing.
|
||||||
|
|
||||||
is interpreted as a boolean True.
|
If `strict=True`, unrecognized values, including None, will raise a
|
||||||
|
ValueError which is useful when parsing values passed in from an API call.
|
||||||
Useful for JSON-decoded stuff and config file parsing
|
Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'.
|
||||||
"""
|
"""
|
||||||
if isinstance(subject, bool):
|
if not isinstance(subject, basestring):
|
||||||
return subject
|
subject = str(subject)
|
||||||
if isinstance(subject, basestring):
|
|
||||||
if subject.strip().lower() in ('true', 'on', 'yes', '1'):
|
lowered = subject.strip().lower()
|
||||||
|
|
||||||
|
if lowered in TRUE_STRINGS:
|
||||||
return True
|
return True
|
||||||
|
elif lowered in FALSE_STRINGS:
|
||||||
|
return False
|
||||||
|
elif strict:
|
||||||
|
acceptable = ', '.join(
|
||||||
|
"'%s'" % s for s in sorted(TRUE_STRINGS + FALSE_STRINGS))
|
||||||
|
msg = _("Unrecognized value '%(val)s', acceptable values are:"
|
||||||
|
" %(acceptable)s") % {'val': subject,
|
||||||
|
'acceptable': acceptable}
|
||||||
|
raise ValueError(msg)
|
||||||
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user