本文整理汇总了Python中oslo_utils.encodeutils.safe_encode方法的典型用法代码示例。如果您正苦于以下问题:Python encodeutils.safe_encode方法的具体用法?Python encodeutils.safe_encode怎么用?Python encodeutils.safe_encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类oslo_utils.encodeutils
的用法示例。
在下文中一共展示了encodeutils.safe_encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: http_log_req
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def http_log_req(_logger, args, kwargs):
if not _logger.isEnabledFor(logging.DEBUG):
return
string_parts = ['curl -i']
for element in args:
if element in ('GET', 'POST', 'DELETE', 'PUT'):
string_parts.append(' -X %s' % element)
else:
string_parts.append(' %s' % element)
for element in kwargs['headers']:
header = ' -H "%s: %s"' % (element, kwargs['headers'][element])
string_parts.append(header)
if 'body' in kwargs and kwargs['body']:
string_parts.append(" -d '%s'" % (kwargs['body']))
req = encodeutils.safe_encode("".join(string_parts))
_logger.debug("\nREQ: %s\n", req)
示例2: main
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def main(args=None):
try:
if args is None:
args = sys.argv[1:]
CeilometerShell().main(args)
except Exception as e:
if '--debug' in args or '-d' in args:
raise
else:
print(encodeutils.safe_encode(six.text_type(e)), file=sys.stderr)
sys.exit(1)
except KeyboardInterrupt:
print("Stopping Ceilometer Client", file=sys.stderr)
sys.exit(130)
示例3: mark_unhealthy
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def mark_unhealthy(self, stack_id, resource_name,
mark_unhealthy, resource_status_reason):
"""Mark a resource as healthy or unhealthy.
:param stack_id: ID of stack containing the resource
:param resource_name: ID of resource
:param mark_unhealthy: Mark resource unhealthy if set to True
:param resource_status_reason: Reason for resource status change.
"""
stack_id = self._resolve_stack_id(stack_id)
url_str = '/stacks/%s/resources/%s' % (
parse.quote(stack_id, ''),
parse.quote(encodeutils.safe_encode(resource_name), ''))
resp = self.client.patch(
url_str,
data={"mark_unhealthy": mark_unhealthy,
"resource_status_reason": resource_status_reason})
body = utils.get_response_body(resp)
return body
示例4: print_update_list
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def print_update_list(lst, fields, formatters=None):
"""Print the stack-update --dry-run output as a table.
This function is necessary to print the stack-update --dry-run
output, which contains additional information about the update.
"""
formatters = formatters or {}
pt = prettytable.PrettyTable(fields, caching=False, print_empty=False)
pt.align = 'l'
for change in lst:
row = []
for field in fields:
if field in formatters:
row.append(formatters[field](change.get(field, None)))
else:
row.append(change.get(field, None))
pt.add_row(row)
if six.PY3:
print(encodeutils.safe_encode(pt.get_string()).decode())
else:
print(encodeutils.safe_encode(pt.get_string()))
示例5: convert_str
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def convert_str(text):
"""Convert to native string.
Convert bytes and Unicode strings to native strings:
* convert to bytes on Python 2:
encode Unicode using encodeutils.safe_encode()
* convert to Unicode on Python 3: decode bytes from UTF-8
"""
if six.PY2:
return encodeutils.to_utf8(text)
else:
if isinstance(text, bytes):
return text.decode('utf-8')
else:
return text
示例6: convert_str
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def convert_str(text):
"""Convert to native string.
Convert bytes and Unicode strings to native strings:
* convert to bytes on Python 2:
encode Unicode using encodeutils.safe_encode()
* convert to Unicode on Python 3: decode bytes from UTF-8
"""
if six.PY2:
return encodeutils.safe_encode(text)
else:
if isinstance(text, bytes):
return text.decode('utf-8')
else:
return text
示例7: _get_encryption_algorithm
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def _get_encryption_algorithm(self, passphrase):
"""Choose whether to use encryption or not based on passphrase
serialization.BestAvailableEncryption fails if passphrase is not
given or if less than one byte therefore we need to check if it is
valid or not
"""
if passphrase:
# encryption requires password in bytes format
algorithm = serialization.BestAvailableEncryption(
# default encoding is utf-8
encodeutils.safe_encode(passphrase)
)
else:
algorithm = serialization.NoEncryption()
return algorithm
示例8: _transform_message
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def _transform_message(self, message):
"""Transforms message into JSON.
Method executes transformation operation for
single element. Operation is set of following
operations:
* checking if message is valid
(:py:func:`.LogPublisher._is_message_valid`)
* truncating message if necessary
(:py:func:`.LogPublisher._truncate`)
:param model.Envelope message: instance of message
:return: serialized message
:rtype: str
"""
if not self._is_message_valid(message):
raise InvalidMessageException()
truncated = self._truncate(message)
return encodeutils.safe_encode(truncated, incoming='utf-8')
示例9: influxdb_get_post
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def influxdb_get_post(uri, query, db=None):
"""Runs a query using HTTP GET or POST and returns the response as a Python list.
At some InfluxDB release several ops changed from using GET to POST. For example,
CREATE DATABASE. To maintain backward compatibility, this function first trys the
query using POST and if that fails it retries again using GET.
"""
query_params = {"q": query}
if db:
query_params['db'] = db
try:
encoded_params = safe_encode(urlparse.urlencode(query_params))
try:
req = urllib.request.urlopen(uri, encoded_params)
return format_response(req)
except urllib.error.HTTPError:
uri = "{}&{}".format(uri, encoded_params)
req = urllib.request.urlopen(uri)
return format_response(req)
except KeyError:
sys.exit(1)
示例10: _data_request
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def _data_request(self, path, data, content_type='application/json',
method='POST', version=None, params=None):
environ = self._environ(path)
environ['REQUEST_METHOD'] = method
if params:
qs = "&".join(["=".join([k, str(params[k])]) for k in params])
environ['QUERY_STRING'] = qs
req = wsgi.Request(environ)
req.context = utils.dummy_context('api_test_user', self.project)
self.context = req.context
ver = version if version else wsgi.DEFAULT_API_VERSION
req.version_request = vr.APIVersionRequest(ver)
req.body = encodeutils.safe_encode(data) if data else None
return req
示例11: test_resource_call_error_handle
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def test_resource_call_error_handle(self):
class Controller(object):
def delete(self, req, identity):
return (req, identity)
actions = {'action': 'delete', 'id': 12, 'body': 'data'}
env = {'wsgiorg.routing_args': [None, actions]}
request = wsgi.Request.blank('/tests/123', environ=env)
request.body = encodeutils.safe_encode('{"foo" : "value"}')
resource = wsgi.Resource(Controller())
# The Resource does not throw webob.HTTPExceptions, since they
# would be considered responses by wsgi and the request flow would end,
# instead they are wrapped so they can reach the fault application
# where they are converted to a JSON response
e = self.assertRaises(exception.HTTPExceptionDisguise,
resource, request)
self.assertIsInstance(e.exc, webob.exc.HTTPBadRequest)
示例12: encrypt
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def encrypt(value, encryption_key=None):
if value is None:
return None
encryption_key = get_valid_encryption_key(encryption_key)
encoded_key = base64.b64encode(encryption_key.encode('utf-8'))
sym = fernet.Fernet(encoded_key)
res = sym.encrypt(encodeutils.safe_encode(value))
return encodeutils.safe_decode(res)
示例13: decrypt
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def decrypt(data, encryption_key=None):
if data is None:
return None
encryption_key = get_valid_encryption_key(encryption_key)
encoded_key = base64.b64encode(encryption_key.encode('utf-8'))
sym = fernet.Fernet(encoded_key)
try:
value = sym.decrypt(encodeutils.safe_encode(data))
if value is not None:
return encodeutils.safe_decode(value, 'utf-8')
except fernet.InvalidToken:
raise exception.InvalidEncryptionKey()
示例14: json_encode
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def json_encode(obj):
return encodeutils.safe_encode(jsonutils.dumps(obj), 'utf-8')
示例15: base64_encode_psk
# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_encode [as 别名]
def base64_encode_psk(self):
if not self.vpnservice:
return
for ipsec_site_conn in self.vpnservice['ipsec_site_connections']:
psk = ipsec_site_conn['psk']
encoded_psk = base64.b64encode(encodeutils.safe_encode(psk))
# NOTE(huntxu): base64.b64encode returns an instance of 'bytes'
# in Python 3, convert it to a str. For Python 2, after calling
# safe_decode, psk is converted into a unicode not containing any
# non-ASCII characters so it doesn't matter.
psk = encodeutils.safe_decode(encoded_psk, incoming='utf_8')
ipsec_site_conn['psk'] = PSK_BASE64_PREFIX + psk