Python oslo_utils.encodeutils.safe_encode() Examples
The following are 30
code examples of oslo_utils.encodeutils.safe_encode().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
oslo_utils.encodeutils
, or try the search function
.
Example #1
Source File: log_publisher.py From monasca-log-api with Apache License 2.0 | 6 votes |
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')
Example #2
Source File: utils.py From eclcli with Apache License 2.0 | 6 votes |
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)
Example #3
Source File: shell.py From eclcli with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: utils.py From eclcli with Apache License 2.0 | 6 votes |
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)
Example #5
Source File: utils.py From eclcli with Apache License 2.0 | 6 votes |
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)
Example #6
Source File: resources.py From eclcli with Apache License 2.0 | 6 votes |
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
Example #7
Source File: utils.py From eclcli with Apache License 2.0 | 6 votes |
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()))
Example #8
Source File: utils.py From os-brick with Apache License 2.0 | 6 votes |
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
Example #9
Source File: simple_crypto.py From barbican with Apache License 2.0 | 6 votes |
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
Example #10
Source File: log_publisher.py From monasca-api with Apache License 2.0 | 6 votes |
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')
Example #11
Source File: influxdb_setup.py From monasca-api with Apache License 2.0 | 6 votes |
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)
Example #12
Source File: shared.py From senlin with Apache License 2.0 | 6 votes |
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
Example #13
Source File: utils.py From manila with Apache License 2.0 | 6 votes |
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
Example #14
Source File: test_wsgi.py From senlin with Apache License 2.0 | 6 votes |
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)
Example #15
Source File: crypt.py From zun with Apache License 2.0 | 6 votes |
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()
Example #16
Source File: test_wsgi.py From senlin with Apache License 2.0 | 5 votes |
def test_resource_client_exceptions_dont_log_error(self): class Controller(object): def __init__(self, exception_to_raise): self.exception_to_raise = exception_to_raise def raise_exception(self, req, body): raise self.exception_to_raise() actions = {'action': 'raise_exception', '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(self.exception)) e = self.assertRaises(self.exception_catch, resource, request) e = e.exc if hasattr(e, 'exc') else e self.assertNotIn(str(e), self.LOG.output)
Example #17
Source File: utils.py From freezer-api with Apache License 2.0 | 5 votes |
def json_encode(obj): return encodeutils.safe_encode(jsonutils.dumps(obj), 'utf-8')
Example #18
Source File: test_serializers.py From senlin with Apache License 2.0 | 5 votes |
def test_has_body_no_content_length(self): request = wsgi.Request.blank('/') request.method = 'POST' request.body = encodeutils.safe_encode('asdf') request.headers.pop('Content-Length') request.headers['Content-Type'] = 'application/json' obj = serializers.JSONRequestDeserializer() self.assertFalse(obj.has_body(request))
Example #19
Source File: versions.py From senlin with Apache License 2.0 | 5 votes |
def __call__(self, req): """Respond to a request for all OpenStack API versions.""" versions = [] for ver, vc in self.Controllers.items(): versions.append(vc.version_info(req)) body = jsonutils.dumps(dict(versions=versions)) response = webob.Response(request=req, status=http_client.MULTIPLE_CHOICES, content_type='application/json') response.body = encodeutils.safe_encode(body) return response
Example #20
Source File: version.py From senlin with Apache License 2.0 | 5 votes |
def __call__(self, req): info = self.version(req) body = jsonutils.dumps(info) response = webob.Response(request=req, content_type='application/json') response.body = encodeutils.safe_encode(body) return response
Example #21
Source File: ipsec.py From neutron-vpnaas with Apache License 2.0 | 5 votes |
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
Example #22
Source File: serializers.py From senlin with Apache License 2.0 | 5 votes |
def default(self, response, result): response.content_type = 'application/json' response.body = encodeutils.safe_encode(self.to_json(result))
Example #23
Source File: test_serializers.py From senlin with Apache License 2.0 | 5 votes |
def test_has_body_zero_content_length(self): request = wsgi.Request.blank('/') request.method = 'POST' request.body = encodeutils.safe_encode('asdf') request.headers['Content-Length'] = 0 request.headers['Content-Type'] = 'application/json' obj = serializers.JSONRequestDeserializer() self.assertFalse(obj.has_body(request))
Example #24
Source File: test_serializers.py From senlin with Apache License 2.0 | 5 votes |
def test_has_body_has_content_length_no_content_type(self): request = wsgi.Request.blank('/') request.method = 'POST' request.body = encodeutils.safe_encode('{"key": "value"}') self.assertIn('Content-Length', request.headers) obj = serializers.JSONRequestDeserializer() self.assertTrue(obj.has_body(request))
Example #25
Source File: test_serializers.py From senlin with Apache License 2.0 | 5 votes |
def test_has_body_has_content_length_plain_content_type(self): request = wsgi.Request.blank('/') request.method = 'POST' request.body = encodeutils.safe_encode('{"key": "value"}') self.assertIn('Content-Length', request.headers) request.headers['Content-Type'] = 'text/plain' obj = serializers.JSONRequestDeserializer() self.assertTrue(obj.has_body(request))
Example #26
Source File: test_serializers.py From senlin with Apache License 2.0 | 5 votes |
def test_has_body_has_content_type(self): request = wsgi.Request.blank('/') request.method = 'POST' request.body = encodeutils.safe_encode('{"key": "value"}') self.assertIn('Content-Length', request.headers) request.headers['Content-Type'] = 'application/json' obj = serializers.JSONRequestDeserializer() self.assertTrue(obj.has_body(request))
Example #27
Source File: test_serializers.py From senlin with Apache License 2.0 | 5 votes |
def test_has_body_has_wrong_content_type(self): request = wsgi.Request.blank('/') request.method = 'POST' request.body = encodeutils.safe_encode('{"key": "value"}') self.assertIn('Content-Length', request.headers) request.headers['Content-Type'] = 'application/xml' obj = serializers.JSONRequestDeserializer() self.assertFalse(obj.has_body(request))
Example #28
Source File: test_serializers.py From senlin with Apache License 2.0 | 5 votes |
def test_has_body_has_aws_content_type_only(self): request = wsgi.Request.blank('/?ContentType=JSON') request.method = 'GET' request.body = encodeutils.safe_encode('{"key": "value"}') self.assertIn('Content-Length', request.headers) obj = serializers.JSONRequestDeserializer() self.assertTrue(obj.has_body(request))
Example #29
Source File: test_serializers.py From senlin with Apache License 2.0 | 5 votes |
def test_default_with_body(self): request = wsgi.Request.blank('/') request.method = 'POST' request.body = encodeutils.safe_encode('{"key": "value"}') actual = serializers.JSONRequestDeserializer().default(request) expected = {"body": {"key": "value"}} self.assertEqual(expected, actual)
Example #30
Source File: test_serializers.py From senlin with Apache License 2.0 | 5 votes |
def test_has_body_content_type_with_get(self): request = wsgi.Request.blank('/') request.method = 'GET' request.body = encodeutils.safe_encode('{"key": "value"}') self.assertIn('Content-Length', request.headers) obj = serializers.JSONRequestDeserializer() self.assertTrue(obj.has_body(request))