From e30a1a8a808d6e13363ca043ea7129f9e3779e4d Mon Sep 17 00:00:00 2001 From: "ChangBo Guo(gcb)" Date: Thu, 14 Jan 2016 14:27:23 +0800 Subject: [PATCH] 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 --- oslo_utils/strutils.py | 32 +++++++++++++++++++++++++++++ oslo_utils/tests/test_strutils.py | 34 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/oslo_utils/strutils.py b/oslo_utils/strutils.py index 3fb3efad..ff3d43cf 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 eba10c94..51aad0a3 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)