def base_opts_from_json(json, include_primary=True): use_dhcp = strutils.bool_from_string(str(json.get('use_dhcp', False))) use_dhcpv6 = strutils.bool_from_string(str(json.get('use_dhcpv6', False))) mtu = json.get('mtu', 1500) primary = strutils.bool_from_string(str(json.get('primary', False))) addresses = [] routes = [] # addresses addresses_json = json.get('addresses') if addresses_json: if isinstance(addresses_json, list): for address in addresses_json: addresses.append(Address.from_json(address)) else: msg = 'Addresses must be a list.' raise InvalidConfigException(msg) # routes routes_json = json.get('routes') if routes_json: if isinstance(routes_json, list): for route in routes_json: routes.append(Route.from_json(route)) else: msg = 'Routes must be a list.' raise InvalidConfigException(msg) if include_primary: return (use_dhcp, use_dhcpv6, addresses, routes, mtu, primary) else: return (use_dhcp, use_dhcpv6, addresses, routes, mtu)
def base_opts_from_json(json): use_dhcp = strutils.bool_from_string(str(json.get('use_dhcp', False))) use_dhcpv6 = strutils.bool_from_string( str(json.get('use_dhcpv6', False))) mtu = json.get('mtu', 1500) addresses = [] routes = [] # addresses addresses_json = json.get('addresses') if addresses_json: if isinstance(addresses_json, list): for address in addresses_json: addresses.append(Address.from_json(address)) else: msg = 'Addresses must be a list.' raise InvalidConfigException(msg) # routes routes_json = json.get('routes') if routes_json: if isinstance(routes_json, list): for route in routes_json: routes.append(Route.from_json(route)) else: msg = 'Routes must be a list.' raise InvalidConfigException(msg) return (use_dhcp, use_dhcpv6, addresses, routes, mtu)
def test_unicode_bool_from_string(self): self._test_bool_from_string(six.text_type) self.assertFalse(strutils.bool_from_string(u'使用', strict=False)) exc = self.assertRaises(ValueError, strutils.bool_from_string, u'使用', strict=True) expected_msg = (u"Unrecognized value '使用', acceptable values are:" u" '0', '1', 'f', 'false', 'n', 'no', 'off', 'on'," u" 't', 'true', 'y', 'yes'") self.assertEqual(expected_msg, six.text_type(exc))
def test_unicode_bool_from_string(self): self._test_bool_from_string(six.text_type) self.assertFalse(strutils.bool_from_string(u'使用', strict=False)) exc = self.assertRaises(ValueError, strutils.bool_from_string, u'使用', strict=True) expected_msg = (u"Unrecognized value '使用', acceptable values are:" u" '0', '1', 'f', 'false', 'n', 'no', 'off', 'on'," u" 't', 'true', 'y', 'yes'") self.assertEqual(expected_msg, unicode(exc))
def test_strict_bool_from_string(self): # None isn't allowed in strict mode exc = self.assertRaises(ValueError, strutils.bool_from_string, None, strict=True) expected_msg = ("Unrecognized value 'None', acceptable values are:" " '0', '1', 'f', 'false', 'n', 'no', 'off', 'on'," " 't', 'true', 'y', 'yes'") self.assertEqual(expected_msg, str(exc)) # Unrecognized strings aren't allowed self.assertFalse(strutils.bool_from_string('Other', strict=False)) exc = self.assertRaises(ValueError, strutils.bool_from_string, 'Other', strict=True) expected_msg = ("Unrecognized value 'Other', acceptable values are:" " '0', '1', 'f', 'false', 'n', 'no', 'off', 'on'," " 't', 'true', 'y', 'yes'") self.assertEqual(expected_msg, str(exc)) # Unrecognized numbers aren't allowed exc = self.assertRaises(ValueError, strutils.bool_from_string, 2, strict=True) expected_msg = ("Unrecognized value '2', acceptable values are:" " '0', '1', 'f', 'false', 'n', 'no', 'off', 'on'," " 't', 'true', 'y', 'yes'") self.assertEqual(expected_msg, str(exc)) # False-like values are allowed self.assertFalse(strutils.bool_from_string('f', strict=True)) self.assertFalse(strutils.bool_from_string('false', strict=True)) self.assertFalse(strutils.bool_from_string('off', strict=True)) self.assertFalse(strutils.bool_from_string('n', strict=True)) self.assertFalse(strutils.bool_from_string('no', strict=True)) self.assertFalse(strutils.bool_from_string('0', strict=True)) self.assertTrue(strutils.bool_from_string('1', strict=True)) # Avoid font-similarity issues (one looks like lowercase-el, zero like # oh, etc...) for char in ('O', 'o', 'L', 'l', 'I', 'i'): self.assertRaises(ValueError, strutils.bool_from_string, char, strict=True)
def get_password(max_password_prompts=3): """Read password from TTY.""" verify = strutils.bool_from_string(env("OS_VERIFY_PASSWORD")) pw = None if hasattr(sys.stdin, "isatty") and sys.stdin.isatty(): # Check for Ctrl-D try: for __ in moves.range(max_password_prompts): pw1 = getpass.getpass("OS Password: "******"Please verify: ") else: pw2 = pw1 if pw1 == pw2 and pw1: pw = pw1 break except EOFError: pass return pw
def get_password(max_password_prompts=3): """Read password from TTY.""" verify = strutils.bool_from_string(env("OS_VERIFY_PASSWORD")) pw = None if hasattr(sys.stdin, "isatty") and sys.stdin.isatty(): # Check for Ctrl-D try: for _ in xrange(max_password_prompts): pw1 = getpass.getpass("OS Password: "******"Please verify: ") else: pw2 = pw1 if pw1 == pw2 and pw1: pw = pw1 break except EOFError: pass return pw
def _test_bool_from_string(self, c): self.assertTrue(strutils.bool_from_string(c('true'))) self.assertTrue(strutils.bool_from_string(c('TRUE'))) self.assertTrue(strutils.bool_from_string(c('on'))) self.assertTrue(strutils.bool_from_string(c('On'))) self.assertTrue(strutils.bool_from_string(c('yes'))) self.assertTrue(strutils.bool_from_string(c('YES'))) self.assertTrue(strutils.bool_from_string(c('yEs'))) self.assertTrue(strutils.bool_from_string(c('1'))) self.assertTrue(strutils.bool_from_string(c('T'))) self.assertTrue(strutils.bool_from_string(c('t'))) self.assertTrue(strutils.bool_from_string(c('Y'))) self.assertTrue(strutils.bool_from_string(c('y'))) self.assertFalse(strutils.bool_from_string(c('false'))) self.assertFalse(strutils.bool_from_string(c('FALSE'))) self.assertFalse(strutils.bool_from_string(c('off'))) self.assertFalse(strutils.bool_from_string(c('OFF'))) self.assertFalse(strutils.bool_from_string(c('no'))) self.assertFalse(strutils.bool_from_string(c('0'))) self.assertFalse(strutils.bool_from_string(c('42'))) self.assertFalse(strutils.bool_from_string(c( 'This should not be True'))) self.assertFalse(strutils.bool_from_string(c('F'))) self.assertFalse(strutils.bool_from_string(c('f'))) self.assertFalse(strutils.bool_from_string(c('N'))) self.assertFalse(strutils.bool_from_string(c('n'))) # Whitespace should be stripped self.assertTrue(strutils.bool_from_string(c(' 1 '))) self.assertTrue(strutils.bool_from_string(c(' true '))) self.assertFalse(strutils.bool_from_string(c(' 0 '))) self.assertFalse(strutils.bool_from_string(c(' false ')))
def test_bool_bool_from_string_default(self): self.assertTrue(strutils.bool_from_string('', default=True)) self.assertFalse(strutils.bool_from_string('wibble', default=False))
def test_bool_bool_from_string(self): self.assertTrue(strutils.bool_from_string(True)) self.assertFalse(strutils.bool_from_string(False))
def test_unicode_bool_from_string(self): self.assertTrue(strutils.bool_from_string(u'true')) self.assertTrue(strutils.bool_from_string(u'TRUE')) self.assertTrue(strutils.bool_from_string(u'on')) self.assertTrue(strutils.bool_from_string(u'On')) self.assertTrue(strutils.bool_from_string(u'yes')) self.assertTrue(strutils.bool_from_string(u'YES')) self.assertTrue(strutils.bool_from_string(u'yEs')) self.assertTrue(strutils.bool_from_string(u'1')) self.assertFalse(strutils.bool_from_string(u'false')) self.assertFalse(strutils.bool_from_string(u'FALSE')) self.assertFalse(strutils.bool_from_string(u'off')) self.assertFalse(strutils.bool_from_string(u'OFF')) self.assertFalse(strutils.bool_from_string(u'no')) self.assertFalse(strutils.bool_from_string(u'NO')) self.assertFalse(strutils.bool_from_string(u'0')) self.assertFalse(strutils.bool_from_string(u'42')) self.assertFalse(strutils.bool_from_string(u'This should not be True'))
def test_str_bool_from_string(self): self.assertTrue(strutils.bool_from_string('true')) self.assertTrue(strutils.bool_from_string('TRUE')) self.assertTrue(strutils.bool_from_string('on')) self.assertTrue(strutils.bool_from_string('On')) self.assertTrue(strutils.bool_from_string('yes')) self.assertTrue(strutils.bool_from_string('YES')) self.assertTrue(strutils.bool_from_string('yEs')) self.assertTrue(strutils.bool_from_string('1')) self.assertFalse(strutils.bool_from_string('false')) self.assertFalse(strutils.bool_from_string('FALSE')) self.assertFalse(strutils.bool_from_string('off')) self.assertFalse(strutils.bool_from_string('OFF')) self.assertFalse(strutils.bool_from_string('no')) self.assertFalse(strutils.bool_from_string('0')) self.assertFalse(strutils.bool_from_string('42')) self.assertFalse(strutils.bool_from_string('This should not be True'))
def test_other_bool_from_string(self): self.assertFalse(strutils.bool_from_string(mock.Mock()))
def _test_bool_from_string(self, c): self.assertTrue(strutils.bool_from_string(c('true'))) self.assertTrue(strutils.bool_from_string(c('TRUE'))) self.assertTrue(strutils.bool_from_string(c('on'))) self.assertTrue(strutils.bool_from_string(c('On'))) self.assertTrue(strutils.bool_from_string(c('yes'))) self.assertTrue(strutils.bool_from_string(c('YES'))) self.assertTrue(strutils.bool_from_string(c('yEs'))) self.assertTrue(strutils.bool_from_string(c('1'))) self.assertTrue(strutils.bool_from_string(c('T'))) self.assertTrue(strutils.bool_from_string(c('t'))) self.assertTrue(strutils.bool_from_string(c('Y'))) self.assertTrue(strutils.bool_from_string(c('y'))) self.assertFalse(strutils.bool_from_string(c('false'))) self.assertFalse(strutils.bool_from_string(c('FALSE'))) self.assertFalse(strutils.bool_from_string(c('off'))) self.assertFalse(strutils.bool_from_string(c('OFF'))) self.assertFalse(strutils.bool_from_string(c('no'))) self.assertFalse(strutils.bool_from_string(c('0'))) self.assertFalse(strutils.bool_from_string(c('42'))) self.assertFalse( strutils.bool_from_string(c('This should not be True'))) self.assertFalse(strutils.bool_from_string(c('F'))) self.assertFalse(strutils.bool_from_string(c('f'))) self.assertFalse(strutils.bool_from_string(c('N'))) self.assertFalse(strutils.bool_from_string(c('n'))) # Whitespace should be stripped self.assertTrue(strutils.bool_from_string(c(' 1 '))) self.assertTrue(strutils.bool_from_string(c(' true '))) self.assertFalse(strutils.bool_from_string(c(' 0 '))) self.assertFalse(strutils.bool_from_string(c(' false ')))
def test_int_bool_from_string(self): self.assertTrue(strutils.bool_from_string(1)) self.assertFalse(strutils.bool_from_string(-1)) self.assertFalse(strutils.bool_from_string(0)) self.assertFalse(strutils.bool_from_string(2))
from openstack.common import strutils # 1. The following operations are supported: # =, s==, s!=, s>=, s>, s<=, s<, <in>, <is>, <or>, ==, !=, >=, <= # 2. Note that <or> is handled in a different way below. # 3. If the first word in the extra_specs is not one of the operators, # it is ignored. _op_methods = { '=': lambda x, y: float(x) >= float(y), '<in>': lambda x, y: y in x, '<is>': lambda x, y: (strutils.bool_from_string(x) is strutils.bool_from_string(y)), '==': lambda x, y: float(x) == float(y), '!=': lambda x, y: float(x) != float(y), '>=': lambda x, y: float(x) >= float(y), '<=': lambda x, y: float(x) <= float(y), 's==': operator.eq, 's!=': operator.ne, 's<': operator.lt, 's<=':
def from_json(json): next_hop = _get_required_field(json, 'next_hop', 'Route') ip_netmask = json.get('ip_netmask', "") default = strutils.bool_from_string(str(json.get('default', False))) return Route(next_hop, ip_netmask, default)
def test_other_bool_from_string(self): self.assertFalse(strutils.bool_from_string(None)) self.assertFalse(strutils.bool_from_string(mock.Mock()))
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import operator from openstack.common import strutils # 1. The following operations are supported: # =, s==, s!=, s>=, s>, s<=, s<, <in>, <is>, <or>, ==, !=, >=, <= # 2. Note that <or> is handled in a different way below. # 3. If the first word in the extra_specs is not one of the operators, # it is ignored. _op_methods = {'=': lambda x, y: float(x) >= float(y), '<in>': lambda x, y: y in x, '<is>': lambda x, y: (strutils.bool_from_string(x) is strutils.bool_from_string(y)), '==': lambda x, y: float(x) == float(y), '!=': lambda x, y: float(x) != float(y), '>=': lambda x, y: float(x) >= float(y), '<=': lambda x, y: float(x) <= float(y), 's==': operator.eq, 's!=': operator.ne, 's<': operator.lt, 's<=': operator.le, 's>': operator.gt, 's>=': operator.ge} def match(value, req): words = req.split()