diff options
author | ChangBo Guo(gcb) <eric.guo@easystack.cn> | 2016-01-14 14:27:23 +0800 |
---|---|---|
committer | Alexis Lee <lxsli@hpe.com> | 2016-02-23 09:34:28 +0000 |
commit | e30a1a8a808d6e13363ca043ea7129f9e3779e4d (patch) | |
tree | 488416503f1cd52cc5f8c8c107cbbc3e55053f79 | |
parent | 6341be4ff23c23e3bed6b44ea50d62548190159c (diff) | |
download | oslo-utils-e30a1a8a808d6e13363ca043ea7129f9e3779e4d.tar.gz |
Add method check_string_length
This method exists in Nova[1] and Cinder[2], and it seems other
projects can also leverage it.
[1]http://git.openstack.org/cgit/openstack/nova/tree/nova/utils.py#n1088
[2]http://git.openstack.org/cgit/openstack/cinder/tree/cinder/utils.py#n676
Change-Id: I2e595404b564dbd363af545945a6bde37f70a4c2
-rw-r--r-- | oslo_utils/strutils.py | 32 | ||||
-rw-r--r-- | oslo_utils/tests/test_strutils.py | 34 |
2 files changed, 66 insertions, 0 deletions
diff --git a/oslo_utils/strutils.py b/oslo_utils/strutils.py index 3fb3efa..ff3d43c 100644 --- a/oslo_utils/strutils.py +++ b/oslo_utils/strutils.py @@ -373,3 +373,35 @@ def is_int_like(val): return six.text_type(int(val)) == six.text_type(val) except (TypeError, ValueError): return False + + +def check_string_length(value, name=None, min_length=0, max_length=None): + """Check the length of specified string. + + :param value: the value of the string + :param name: the name of the string + :param min_length: the min_length of the string + :param max_length: the max_length of the string + :raises TypeError, ValueError: For any invalid input. + + .. versionadded:: 3.7 + """ + if name is None: + name = value + + if not isinstance(value, six.string_types): + msg = _("%s is not a string or unicode") % name + raise TypeError(msg) + + length = len(value) + if length < min_length: + msg = _("%(name)s has %(length)s characters, less than " + "%(min_length)s.") % {'name': name, 'length': length, + 'min_length': min_length} + raise ValueError(msg) + + if max_length and length > max_length: + msg = _("%(name)s has %(length)s characters, more than " + "%(max_length)s.") % {'name': name, 'length': length, + 'max_length': max_length} + raise ValueError(msg) diff --git a/oslo_utils/tests/test_strutils.py b/oslo_utils/tests/test_strutils.py index eba10c9..51aad0a 100644 --- a/oslo_utils/tests/test_strutils.py +++ b/oslo_utils/tests/test_strutils.py @@ -660,3 +660,37 @@ class IsIntLikeTestCase(test_base.BaseTestCase): # NOTE(viktors): Check integer numbers with base not 10 self.assertFalse(strutils.is_int_like("0o51")) self.assertFalse(strutils.is_int_like("0xDEADBEEF")) + + +class StringLengthTestCase(test_base.BaseTestCase): + def test_check_string_length(self): + self.assertIsNone(strutils.check_string_length( + 'test', 'name', max_length=255)) + self.assertRaises(ValueError, + strutils.check_string_length, + '', 'name', min_length=1) + self.assertRaises(ValueError, + strutils.check_string_length, + 'a' * 256, 'name', max_length=255) + self.assertRaises(TypeError, + strutils.check_string_length, + 11, 'name', max_length=255) + self.assertRaises(TypeError, + strutils.check_string_length, + dict(), 'name', max_length=255) + + def test_check_string_length_noname(self): + self.assertIsNone(strutils.check_string_length( + 'test', max_length=255)) + self.assertRaises(ValueError, + strutils.check_string_length, + '', min_length=1) + self.assertRaises(ValueError, + strutils.check_string_length, + 'a' * 256, max_length=255) + self.assertRaises(TypeError, + strutils.check_string_length, + 11, max_length=255) + self.assertRaises(TypeError, + strutils.check_string_length, + dict(), max_length=255) |