Merge lp:~hopem/charm-helpers/add-core-strutils into lp:charm-helpers

Proposed by Edward Hope-Morley
Status: Merged
Merged at revision: 309
Proposed branch: lp:~hopem/charm-helpers/add-core-strutils
Merge into: lp:charm-helpers
Diff against target: 76 lines (+67/-0)
2 files modified
charmhelpers/core/strutils.py (+38/-0)
tests/core/test_strutils.py (+29/-0)
To merge this branch: bzr merge lp:~hopem/charm-helpers/add-core-strutils
Reviewer Review Type Date Requested Status
Liam Young (community) Approve
Review via email: mp+249798@code.launchpad.net
To post a comment you must log in.
309. By Edward Hope-Morley

simplified

Revision history for this message
Liam Young (gnuoy) wrote :

Approve

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'charmhelpers/core/strutils.py'
2--- charmhelpers/core/strutils.py 1970-01-01 00:00:00 +0000
3+++ charmhelpers/core/strutils.py 2015-02-16 11:02:09 +0000
4@@ -0,0 +1,38 @@
5+#!/usr/bin/env python
6+# -*- coding: utf-8 -*-
7+
8+# Copyright 2014-2015 Canonical Limited.
9+#
10+# This file is part of charm-helpers.
11+#
12+# charm-helpers is free software: you can redistribute it and/or modify
13+# it under the terms of the GNU Lesser General Public License version 3 as
14+# published by the Free Software Foundation.
15+#
16+# charm-helpers is distributed in the hope that it will be useful,
17+# but WITHOUT ANY WARRANTY; without even the implied warranty of
18+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+# GNU Lesser General Public License for more details.
20+#
21+# You should have received a copy of the GNU Lesser General Public License
22+# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
23+
24+
25+def bool_from_string(value):
26+ """Interpret string value as boolean.
27+
28+ Returns True if value translates to True otherwise False.
29+ """
30+ if isinstance(value, str):
31+ value = value.lower()
32+ else:
33+ msg = "Unable to interpret non-string value '%s' as boolean" % (value)
34+ raise ValueError(msg)
35+
36+ if value in ['y', 'yes', 'true', 't']:
37+ return True
38+ elif value in ['n', 'no', 'false', 'f']:
39+ return False
40+
41+ msg = "Unable to interpret string value '%s' as boolean" % (value)
42+ raise ValueError(msg)
43
44=== added file 'tests/core/test_strutils.py'
45--- tests/core/test_strutils.py 1970-01-01 00:00:00 +0000
46+++ tests/core/test_strutils.py 2015-02-16 11:02:09 +0000
47@@ -0,0 +1,29 @@
48+import unittest
49+
50+import charmhelpers.core.strutils as strutils
51+
52+
53+class TestStrUtils(unittest.TestCase):
54+ def setUp(self):
55+ super(TestStrUtils, self).setUp()
56+
57+ def tearDown(self):
58+ super(TestStrUtils, self).tearDown()
59+
60+ def test_bool_from_string(self):
61+ self.assertTrue(strutils.bool_from_string('true'))
62+ self.assertTrue(strutils.bool_from_string('True'))
63+ self.assertTrue(strutils.bool_from_string('yes'))
64+ self.assertTrue(strutils.bool_from_string('Yes'))
65+ self.assertTrue(strutils.bool_from_string('y'))
66+ self.assertTrue(strutils.bool_from_string('Y'))
67+
68+ self.assertFalse(strutils.bool_from_string('False'))
69+ self.assertFalse(strutils.bool_from_string('false'))
70+ self.assertFalse(strutils.bool_from_string('no'))
71+ self.assertFalse(strutils.bool_from_string('No'))
72+ self.assertFalse(strutils.bool_from_string('n'))
73+ self.assertFalse(strutils.bool_from_string('N'))
74+
75+ self.assertRaises(ValueError, strutils.bool_from_string, None)
76+ self.assertRaises(ValueError, strutils.bool_from_string, 'foo')

Subscribers

People subscribed via source and target branches