summaryrefslogtreecommitdiffstats
path: root/openstack/common/strutils.py
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2012-12-03 17:43:03 +0000
committerGerrit Code Review <review@openstack.org>2012-12-03 17:43:03 +0000
commitdf24a6affa59c1b26853bffcca2003290c7a4020 (patch)
tree89f8ec84e161d25c8cb53a1a8b7cb15f7d2447ad /openstack/common/strutils.py
parent9ad422b825180a05df9af50d3a1dabea005b4861 (diff)
parent7cf239c4fbc0f3bd9d8881f5922451989a89b223 (diff)
downloadoslo-df24a6affa59c1b26853bffcca2003290c7a4020.tar.gz
oslo-df24a6affa59c1b26853bffcca2003290c7a4020.tar.xz
oslo-df24a6affa59c1b26853bffcca2003290c7a4020.zip
Merge "Rename utils.py to strutils.py"
Diffstat (limited to 'openstack/common/strutils.py')
-rw-r--r--openstack/common/strutils.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/openstack/common/strutils.py b/openstack/common/strutils.py
new file mode 100644
index 0000000..05f0e9f
--- /dev/null
+++ b/openstack/common/strutils.py
@@ -0,0 +1,59 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2011 OpenStack LLC.
+# 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.
+
+"""
+System-level utilities and helper functions.
+"""
+
+import logging
+
+LOG = logging.getLogger(__name__)
+
+
+def int_from_bool_as_string(subject):
+ """
+ Interpret a string as a boolean and return either 1 or 0.
+
+ Any string value in:
+
+ ('True', 'true', 'On', 'on', '1')
+
+ is interpreted as a boolean True.
+
+ Useful for JSON-decoded stuff and config file parsing
+ """
+ return bool_from_string(subject) and 1 or 0
+
+
+def bool_from_string(subject):
+ """
+ Interpret a string as a boolean.
+
+ Any string value in:
+
+ ('True', 'true', 'On', 'on', 'Yes', 'yes', '1')
+
+ is interpreted as a boolean True.
+
+ Useful for JSON-decoded stuff and config file parsing
+ """
+ if isinstance(subject, bool):
+ return subject
+ if isinstance(subject, basestring):
+ if subject.strip().lower() in ('true', 'on', 'yes', '1'):
+ return True
+ return False