本文整理汇总了Python中six.text_type函数的典型用法代码示例。如果您正苦于以下问题:Python text_type函数的具体用法?Python text_type怎么用?Python text_type使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了text_type函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_thread_group_handles_child_exception
def test_thread_group_handles_child_exception(self):
try:
with context.ThreadGroup() as tg:
tg.spawn('raiser1', self._raise_test_exc, 'exc1')
except ex.ThreadException as te:
self.assertIn('exc1', six.text_type(te))
self.assertIn('raiser1', six.text_type(te))
示例2: pool_entries
def pool_entries(controller, pool_ctrl, count):
"""Context manager to create several catalogue entries for testing.
The entries are automatically deleted when the context manager
goes out of scope.
:param controller: storage handler
:type controller: queues.storage.base:CatalogueBase
:param count: number of entries to create
:type count: int
:returns: [(project, queue, pool)]
:rtype: [(six.text_type, six.text_type, six.text_type)]
"""
spec = [(u'_', six.text_type(uuid.uuid1()), six.text_type(i))
for i in range(count)]
for p, q, s in spec:
pool_ctrl.create(s, 100, s)
controller.insert(p, q, s)
yield spec
for p, q, s in spec:
controller.delete(p, q)
pool_ctrl.delete(s)
示例3: __init__
def __init__(self, error=None, path=None, message=None,
resource=None):
if path is None:
path = []
elif isinstance(path, six.string_types):
path = [path]
if resource is not None and not path:
path = [resource.stack.t.get_section_name(
resource.stack.t.RESOURCES), resource.name]
if isinstance(error, Exception):
if isinstance(error, StackValidationFailed):
str_error = error.error
message = error.error_message
path = path + error.path
# This is a hack to avoid the py3 (chained exception)
# json serialization circular reference error from
# oslo.messaging.
self.args = error.args
else:
str_error = six.text_type(type(error).__name__)
message = six.text_type(error)
else:
str_error = error
super(StackValidationFailed, self).__init__(error=str_error, path=path,
message=message)
示例4: test_node_handle_exception
def test_node_handle_exception(self, mock_warning):
ex = exception.ResourceStatusError(resource_id='FAKE_ID',
status='FAKE_STATUS',
reason='FAKE_REASON')
node = nodem.Node('node1', self.profile.id, None, self.context)
node.store(self.context)
node._handle_exception(self.context, 'ACTION', 'STATUS', ex)
db_node = db_api.node_get(self.context, node.id)
self.assertEqual(node.ERROR, db_node.status)
self.assertEqual('Profile failed in ACTIOing resource '
'(FAKE_ID) due to: %s' % six.text_type(ex),
db_node.status_reason)
self.assertEqual('FAKE_ID', db_node.physical_id)
mock_warning.assert_called_with(self.context, node, 'ACTION',
'STATUS', six.text_type(ex))
# Exception happens before physical node creation started.
ex = exception.ResourceCreationFailure(rtype='stack',
code=400,
message='Bad request')
node = nodem.Node('node1', self.profile.id, None, self.context)
node.store(self.context)
node._handle_exception(self.context, 'CREATE', 'STATUS', ex)
db_node = db_api.node_get(self.context, node.id)
self.assertEqual(node.ERROR, db_node.status)
self.assertEqual('Profile failed in creating node due to: '
'%s' % six.text_type(ex), db_node.status_reason)
self.assertEqual(None, db_node.physical_id)
mock_warning.assert_called_with(self.context, node, 'CREATE',
'STATUS', six.text_type(ex))
示例5: create
def create(self, req, body):
"""Creates a new share group snapshot."""
context = req.environ['manila.context']
if not self.is_valid_body(body, 'share_group_snapshot'):
msg = _("'share_group_snapshot' is missing from the request body.")
raise exc.HTTPBadRequest(explanation=msg)
share_group_snapshot = body.get('share_group_snapshot', {})
share_group_id = share_group_snapshot.get('share_group_id')
if not share_group_id:
msg = _("Must supply 'share_group_id' attribute.")
raise exc.HTTPBadRequest(explanation=msg)
if not uuidutils.is_uuid_like(share_group_id):
msg = _("The 'share_group_id' attribute must be a uuid.")
raise exc.HTTPBadRequest(explanation=six.text_type(msg))
kwargs = {"share_group_id": share_group_id}
if 'name' in share_group_snapshot:
kwargs['name'] = share_group_snapshot.get('name')
if 'description' in share_group_snapshot:
kwargs['description'] = share_group_snapshot.get('description')
try:
new_snapshot = self.share_group_api.create_share_group_snapshot(
context, **kwargs)
except exception.ShareGroupNotFound as e:
raise exc.HTTPBadRequest(explanation=six.text_type(e))
except exception.InvalidShareGroup as e:
raise exc.HTTPConflict(explanation=six.text_type(e))
return self._view_builder.detail(req, dict(new_snapshot.items()))
示例6: test_matrix_environments
def test_matrix_environments(tmpdir):
conf = config.Config()
conf.env_dir = six.text_type(tmpdir.join("env"))
conf.pythons = ["2.7", "3.4"]
conf.matrix = {
"six": ["1.4", None],
"colorama": ["0.3.1", "0.3.3"]
}
environments = list(environment.get_environments(conf))
assert len(environments) == 2 * 2 * 2
# Only test the first two environments, since this is so time
# consuming
for env in environments[:2]:
env.create()
output = env.run(
['-c', 'import six, sys; sys.stdout.write(six.__version__)'],
valid_return_codes=None)
if 'six' in env._requirements:
assert output.startswith(six.text_type(env._requirements['six']))
output = env.run(
['-c', 'import colorama, sys; sys.stdout.write(colorama.__version__)'])
assert output.startswith(six.text_type(env._requirements['colorama']))
示例7: __exit__
def __exit__(self, ex_type, ex_value, ex_traceback):
if not ex_value:
return True
if isinstance(ex_value, exception.NotAuthorized):
msg = six.text_type(ex_value)
raise Fault(webob.exc.HTTPForbidden(explanation=msg))
elif isinstance(ex_value, exception.Invalid):
raise Fault(exception.ConvertedException(
code=ex_value.code, explanation=six.text_type(ex_value)))
elif isinstance(ex_value, TypeError):
exc_info = (ex_type, ex_value, ex_traceback)
LOG.error(_(
'Exception handling resource: %s') %
ex_value, exc_info=exc_info)
raise Fault(webob.exc.HTTPBadRequest())
elif isinstance(ex_value, Fault):
LOG.info(_("Fault thrown: %s"), six.text_type(ex_value))
raise ex_value
elif isinstance(ex_value, webob.exc.HTTPException):
LOG.info(_("HTTP exception thrown: %s"), six.text_type(ex_value))
raise Fault(ex_value)
# We didn't handle the exception
return False
示例8: test_simple
def test_simple(self):
user = self.create_user()
org = self.create_organization(owner=user)
auth_provider = AuthProvider.objects.create(
organization=org,
provider='dummy',
)
auth_identity = AuthIdentity.objects.create(
auth_provider=auth_provider,
ident=user.email,
user=user,
)
auth = Authenticator.objects.create(
type=available_authenticators(ignore_backup=True)[0].type,
user=user,
)
result = serialize(user, user, DetailedUserSerializer())
assert result['id'] == six.text_type(user.id)
assert result['has2fa'] is True
assert len(result['emails']) == 1
assert result['emails'][0]['email'] == user.email
assert result['emails'][0]['is_verified']
assert 'identities' in result
assert len(result['identities']) == 1
assert result['identities'][0]['id'] == six.text_type(auth_identity.id)
assert result['identities'][0]['name'] == auth_identity.ident
assert 'authenticators' in result
assert len(result['authenticators']) == 1
assert result['authenticators'][0]['id'] == six.text_type(auth.id)
示例9: _get_context
def _get_context(self, networks_per_tenant=2, neutron_network_provider=True):
tenants = {}
for t_id in range(self.TENANTS_AMOUNT):
tenants[six.text_type(t_id)] = {"name": six.text_type(t_id)}
tenants[six.text_type(t_id)]["networks"] = []
for i in range(networks_per_tenant):
network = {"id": "fake_net_id_%s" % i}
if neutron_network_provider:
network["subnets"] = ["fake_subnet_id_of_net_%s" % i]
else:
network["cidr"] = "101.0.5.0/24"
tenants[six.text_type(t_id)]["networks"].append(network)
users = []
for t_id in tenants.keys():
for i in range(self.USERS_PER_TENANT):
users.append({"id": i, "tenant_id": t_id, "credential": "fake"})
context = {
"config": {
"users": {
"tenants": self.TENANTS_AMOUNT,
"users_per_tenant": self.USERS_PER_TENANT,
"random_user_choice": False,
},
consts.SHARE_NETWORKS_CONTEXT_NAME: {"use_share_networks": True, "share_networks": []},
"network": {"networks_per_tenant": networks_per_tenant, "start_cidr": "101.0.5.0/24"},
},
"admin": {"credential": mock.MagicMock()},
"task": mock.MagicMock(),
"users": users,
"tenants": tenants,
}
return context
示例10: output_param_to_log
def output_param_to_log(self, storage_protocol):
essential_inherited_param = ['volume_backend_name', 'volume_driver']
conf = self.configuration
msg = basic_lib.set_msg(1, config_group=conf.config_group)
LOG.info(msg)
version = self.command.get_comm_version()
if conf.hitachi_unit_name:
prefix = 'HSNM2 version'
else:
prefix = 'RAID Manager version'
LOG.info('\t%-35s%s' % (prefix + ': ', six.text_type(version)))
for param in essential_inherited_param:
value = conf.safe_get(param)
LOG.info('\t%-35s%s' % (param + ': ', six.text_type(value)))
for opt in volume_opts:
if not opt.secret:
value = getattr(conf, opt.name)
LOG.info('\t%-35s%s' % (opt.name + ': ',
six.text_type(value)))
if storage_protocol == 'iSCSI':
value = getattr(conf, 'hitachi_group_request')
LOG.info('\t%-35s%s' % ('hitachi_group_request: ',
six.text_type(value)))
示例11: copy_sync_data
def copy_sync_data(self, src_ldev, dest_ldev, size):
src_vol = {'provider_location': six.text_type(src_ldev),
'id': 'src_vol'}
dest_vol = {'provider_location': six.text_type(dest_ldev),
'id': 'dest_vol'}
properties = utils.brick_get_connector_properties()
driver = self.generated_from
src_info = None
dest_info = None
try:
dest_info = driver._attach_volume(self.context, dest_vol,
properties)
src_info = driver._attach_volume(self.context, src_vol,
properties)
volume_utils.copy_volume(src_info['device']['path'],
dest_info['device']['path'], size * 1024,
self.configuration.volume_dd_blocksize)
finally:
if dest_info:
driver._detach_volume(self.context, dest_info,
dest_vol, properties)
if src_info:
driver._detach_volume(self.context, src_info,
src_vol, properties)
self.command.discard_zero_page(dest_ldev)
示例12: test_get_network_id_by_label
def test_get_network_id_by_label(self):
"""Tests the get_net_id_by_label function."""
net = mock.MagicMock()
net.id = str(uuid.uuid4())
self.nova_client.networks.find.side_effect = [
net, nova_exceptions.NotFound(404),
nova_exceptions.NoUniqueMatch()]
self.assertEqual(net.id,
self.nova_plugin.get_net_id_by_label('net_label'))
exc = self.assertRaises(
exception.NovaNetworkNotFound,
self.nova_plugin.get_net_id_by_label, 'idontexist')
expected = 'The Nova network (idontexist) could not be found'
self.assertIn(expected, six.text_type(exc))
exc = self.assertRaises(
exception.PhysicalResourceNameAmbiguity,
self.nova_plugin.get_net_id_by_label, 'notUnique')
expected = ('Multiple physical resources were found '
'with name (notUnique)')
self.assertIn(expected, six.text_type(exc))
calls = [mock.call(label='net_label'),
mock.call(label='idontexist'),
mock.call(label='notUnique')]
self.nova_client.networks.find.assert_has_calls(calls)
示例13: unmanage
def unmanage(self, req, id):
"""Unmanage a share."""
context = req.environ['manila.context']
authorize(context)
LOG.info(_LI("Unmanage share with id: %s"), id, context=context)
try:
share = self.share_api.get(context, id)
if share.get('share_server_id'):
msg = _("Operation 'unmanage' is not supported for shares "
"that are created on top of share servers "
"(created with share-networks).")
raise exc.HTTPForbidden(explanation=msg)
elif share['status'] in constants.TRANSITIONAL_STATUSES:
msg = _("Share with transitional state can not be unmanaged. "
"Share '%(s_id)s' is in '%(state)s' state.") % dict(
state=share['status'], s_id=share['id'])
raise exc.HTTPForbidden(explanation=msg)
snapshots = self.share_api.db.share_snapshot_get_all_for_share(
context, id)
if snapshots:
msg = _("Share '%(s_id)s' can not be unmanaged because it has "
"'%(amount)s' dependent snapshot(s).") % {
's_id': id, 'amount': len(snapshots)}
raise exc.HTTPForbidden(explanation=msg)
self.share_api.unmanage(context, share)
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=six.text_type(e))
except (exception.InvalidShare, exception.PolicyNotAuthorized) as e:
raise exc.HTTPForbidden(explanation=six.text_type(e))
return webob.Response(status_int=202)
示例14: store_add_to_backend
def store_add_to_backend(image_id, data, size, store):
"""
A wrapper around a call to each stores add() method. This gives glance
a common place to check the output
:param image_id: The image add to which data is added
:param data: The data to be stored
:param size: The length of the data in bytes
:param store: The store to which the data is being added
:return: The url location of the file,
the size amount of data,
the checksum of the data
the storage systems metadata dictionary for the location
"""
(location, size, checksum, metadata) = store.add(image_id, data, size)
if metadata is not None:
if not isinstance(metadata, dict):
msg = (_("The storage driver %(store)s returned invalid metadata "
"%(metadata)s. This must be a dictionary type") %
{'store': six.text_type(store),
'metadata': six.text_type(metadata)})
LOG.error(msg)
raise BackendException(msg)
try:
check_location_metadata(metadata)
except BackendException as e:
e_msg = (_("A bad metadata structure was returned from the "
"%(store)s storage driver: %(metadata)s. %(error)s.") %
{'store': six.text_type(store),
'metadata': six.text_type(metadata),
'error': six.text_type(e)})
LOG.error(e_msg)
raise BackendException(e_msg)
return (location, size, checksum, metadata)
示例15: wrapper
def wrapper(*args, **kwargs):
try:
return method(*args, **kwargs)
except db_exception.DBDuplicateEntry as e:
# LOG the exception for debug purposes, do not send the
# exception details out with the raised Conflict exception
# as it can contain raw SQL.
LOG.debug(_conflict_msg, {'conflict_type': conflict_type,
'details': six.text_type(e)})
raise exception.Conflict(type=conflict_type,
details=_('Duplicate Entry'))
except db_exception.DBError as e:
# TODO(blk-u): inspecting inner_exception breaks encapsulation;
# oslo_db should provide exception we need.
if isinstance(e.inner_exception, IntegrityError):
# LOG the exception for debug purposes, do not send the
# exception details out with the raised Conflict exception
# as it can contain raw SQL.
LOG.debug(_conflict_msg, {'conflict_type': conflict_type,
'details': six.text_type(e)})
# NOTE(morganfainberg): This is really a case where the SQL
# failed to store the data. This is not something that the
# user has done wrong. Example would be a ForeignKey is
# missing; the code that is executed before reaching the
# SQL writing to the DB should catch the issue.
raise exception.UnexpectedError(
_('An unexpected error occurred when trying to '
'store %s') % conflict_type)
raise