~ibm-charms/charms/trusty/nova-compute-power/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/strutils.py

  • Committer: james.page at ubuntu
  • Date: 2015-06-17 09:08:47 UTC
  • mfrom: (74.2.46 nova-compute-power)
  • Revision ID: james.page@ubuntu.com-20150617090847-ork7zgglosiiqs7y
Merge updates for OpenStack Kilo release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# Copyright 2014-2015 Canonical Limited.
 
5
#
 
6
# This file is part of charm-helpers.
 
7
#
 
8
# charm-helpers is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU Lesser General Public License version 3 as
 
10
# published by the Free Software Foundation.
 
11
#
 
12
# charm-helpers is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU Lesser General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU Lesser General Public License
 
18
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
import six
 
21
 
 
22
 
 
23
def bool_from_string(value):
 
24
    """Interpret string value as boolean.
 
25
 
 
26
    Returns True if value translates to True otherwise False.
 
27
    """
 
28
    if isinstance(value, six.string_types):
 
29
        value = six.text_type(value)
 
30
    else:
 
31
        msg = "Unable to interpret non-string value '%s' as boolean" % (value)
 
32
        raise ValueError(msg)
 
33
 
 
34
    value = value.strip().lower()
 
35
 
 
36
    if value in ['y', 'yes', 'true', 't', 'on']:
 
37
        return True
 
38
    elif value in ['n', 'no', 'false', 'f', 'off']:
 
39
        return False
 
40
 
 
41
    msg = "Unable to interpret string value '%s' as boolean" % (value)
 
42
    raise ValueError(msg)