~ubuntu-branches/ubuntu/trusty/ceilometer/trusty-proposed

« back to all changes in this revision

Viewing changes to ceilometer/openstack/common/strutils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, James Page, Chuck Short
  • Date: 2014-01-23 15:08:11 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20140123150811-1zaismsuyh1hcl8y
Tags: 2014.1~b2-0ubuntu1
[ James Page ]
* d/control: Add python-jsonpath-rw to BD's.
* d/p/fix-setup-requirements.patch: Bump WebOb to support < 1.4.
 (LP: #1261101)

[ Chuck Short ]
* New upstream version.
* debian/control, debian/ceilometer-common.install: Split out
  ceilometer-alarm-evaluator and ceilometer-alarm-notifier into their
  own packages. (LP: #1250002)
* debian/ceilometer-agent-central.logrotate,
  debian/ceilometer-agent-compute.logrotate,
  debian/ceilometer-api.logrotate,
  debian/ceilometer-collector.logrotate: Add logrotate files, 
  thanks to Ahmed Rahal. (LP: #1224223)
* Fix typos in upstart files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
import six
25
25
 
26
 
from ceilometer.openstack.common.gettextutils import _  # noqa
 
26
from ceilometer.openstack.common.gettextutils import _
27
27
 
28
28
 
29
29
# Used for looking up extensions of text
58
58
    return bool_from_string(subject) and 1 or 0
59
59
 
60
60
 
61
 
def bool_from_string(subject, strict=False):
 
61
def bool_from_string(subject, strict=False, default=False):
62
62
    """Interpret a string as a boolean.
63
63
 
64
64
    A case-insensitive match is performed such that strings matching 't',
65
65
    'true', 'on', 'y', 'yes', or '1' are considered True and, when
66
 
    `strict=False`, anything else is considered False.
 
66
    `strict=False`, anything else returns the value specified by 'default'.
67
67
 
68
68
    Useful for JSON-decoded stuff and config file parsing.
69
69
 
88
88
                                      'acceptable': acceptable}
89
89
        raise ValueError(msg)
90
90
    else:
91
 
        return False
 
91
        return default
92
92
 
93
93
 
94
94
def safe_decode(text, incoming=None, errors='strict'):
152
152
                    sys.getdefaultencoding())
153
153
 
154
154
    if isinstance(text, six.text_type):
155
 
        return text.encode(encoding, errors)
 
155
        if six.PY3:
 
156
            return text.encode(encoding, errors).decode(incoming)
 
157
        else:
 
158
            return text.encode(encoding, errors)
156
159
    elif text and encoding != incoming:
157
160
        # Decode text before encoding it with `encoding`
158
161
        text = safe_decode(text, incoming, errors)
159
 
        return text.encode(encoding, errors)
 
162
        if six.PY3:
 
163
            return text.encode(encoding, errors).decode(incoming)
 
164
        else:
 
165
            return text.encode(encoding, errors)
160
166
 
161
167
    return text
162
168