当前位置: 首页>>代码示例>>Python>>正文


Python encodeutils.safe_decode方法代码示例

本文整理汇总了Python中oslo_utils.encodeutils.safe_decode方法的典型用法代码示例。如果您正苦于以下问题:Python encodeutils.safe_decode方法的具体用法?Python encodeutils.safe_decode怎么用?Python encodeutils.safe_decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在oslo_utils.encodeutils的用法示例。


在下文中一共展示了encodeutils.safe_decode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_req_version_matches_with_if

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def test_req_version_matches_with_if(self, version, ret_val):
        version_request = api_version_request.APIVersionRequest(version)
        self.mock_object(api_version_request,
                         'max_api_version',
                         mock.Mock(return_value=version_request))

        class Controller(wsgi.Controller):

            def index(self, req):
                req_version = req.api_version_request
                if req_version.matches('2.1', '2.8'):
                    return 'older'
                if req_version.matches('2.9', '2.88'):
                    return 'newer'

        req = fakes.HTTPRequest.blank('/tests', base_url='http://localhost/v2')
        req.headers = {version_header_name: version}
        app = fakes.TestRouter(Controller())

        response = req.get_response(app)

        resp = encodeutils.safe_decode(response.body, incoming='utf-8')
        self.assertEqual(ret_val, resp)
        self.assertEqual(200, response.status_int) 
开发者ID:openstack,项目名称:manila,代码行数:26,代码来源:test_versions.py

示例2: test_req_version_matches_with_None

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def test_req_version_matches_with_None(self, version, ret_val):
        version_request = api_version_request.APIVersionRequest(version)
        self.mock_object(api_version_request,
                         'max_api_version',
                         mock.Mock(return_value=version_request))

        class Controller(wsgi.Controller):

            def index(self, req):
                req_version = req.api_version_request
                if req_version.matches(None, '2.8'):
                    return 'older'
                if req_version.matches('2.9', None):
                    return 'newer'

        req = fakes.HTTPRequest.blank('/tests', base_url='http://localhost/v2')
        req.headers = {version_header_name: version}
        app = fakes.TestRouter(Controller())

        response = req.get_response(app)

        resp = encodeutils.safe_decode(response.body, incoming='utf-8')
        self.assertEqual(ret_val, resp)
        self.assertEqual(200, response.status_int) 
开发者ID:openstack,项目名称:manila,代码行数:26,代码来源:test_versions.py

示例3: test_req_version_matches_with_None_None

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def test_req_version_matches_with_None_None(self):
        version_request = api_version_request.APIVersionRequest('2.39')
        self.mock_object(api_version_request,
                         'max_api_version',
                         mock.Mock(return_value=version_request))

        class Controller(wsgi.Controller):

            def index(self, req):
                req_version = req.api_version_request
                # This case is artificial, and will return True
                if req_version.matches(None, None):
                    return "Pass"

        req = fakes.HTTPRequest.blank('/tests', base_url='http://localhost/v2')
        req.headers = {version_header_name: '2.39'}
        app = fakes.TestRouter(Controller())

        response = req.get_response(app)

        resp = encodeutils.safe_decode(response.body, incoming='utf-8')
        self.assertEqual("Pass", resp)
        self.assertEqual(200, response.status_int) 
开发者ID:openstack,项目名称:manila,代码行数:25,代码来源:test_versions.py

示例4: _run_proc

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def _run_proc(cmd):
    """Simple wrapper to run a process.

    Will return the return code along with the stdout and stderr.  It is the
    decision of the caller if it wishes to honor or ignore the return code.

    :return: The return code, stdout and stderr from the command.
    """
    process = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                               close_fds=True, env=None)
    process.wait()
    stdout, stderr = process.communicate()
    # Convert the stdout/stderr output from a byte-string to a unicode-string
    # so it doesn't blow up later on anything doing an implicit conversion
    stdout = encodeutils.safe_decode(stdout)
    stderr = encodeutils.safe_decode(stderr)
    return process.returncode, stdout, stderr 
开发者ID:powervm,项目名称:pypowervm,代码行数:20,代码来源:vterm.py

示例5: get_query_param

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def get_query_param(req, param_name, required=False, default_val=None):
    try:
        params = falcon.uri.parse_query_string(req.query_string)
        if param_name in params:
            if isinstance(params[param_name], list):
                param_val = encodeutils.safe_decode(params[param_name][0], 'utf8')
            else:
                param_val = encodeutils.safe_decode(params[param_name], 'utf8')

            return param_val
        else:
            if required:
                raise Exception("Missing " + param_name)
            else:
                return default_val
    except Exception as ex:
        LOG.debug(ex)
        raise HTTPUnprocessableEntityError('Unprocessable Entity', str(ex)) 
开发者ID:openstack,项目名称:monasca-api,代码行数:20,代码来源:helpers.py

示例6: test_call

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def test_call(self, mock_v1):
        mock_v1.return_value = {'foo': 'bar'}
        conf = mock.Mock()
        controller = versions.Controller(conf)
        environ = {
            'REQUEST_METHOD': 'GET',
            'SERVER_NAME': 'host',
            'SERVER_PORT': 8778,
            'SCRIPT_NAME': '/',
            'PATH_INFO': '/',
            'wsgi.url_scheme': 'http',
        }
        req = wsgi.Request(environ)
        expected_dict = {
            'versions': [{'foo': 'bar'}]
        }
        expected_body = jsonutils.dumps(expected_dict)

        resp = controller(req)

        self.assertIsInstance(resp, webob.Response)
        self.assertEqual(expected_body, encodeutils.safe_decode(resp.body))
        self.assertEqual(http_client.MULTIPLE_CHOICES, resp.status_code)
        self.assertEqual('application/json', resp.content_type) 
开发者ID:openstack,项目名称:senlin,代码行数:26,代码来源:test_versions.py

示例7: test_resource_call_with_version_header

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def test_resource_call_with_version_header(self):
        class Controller(object):
            def dance(self, req):
                return {'foo': 'bar'}

        actions = {'action': 'dance'}
        env = {'wsgiorg.routing_args': [None, actions]}
        request = wsgi.Request.blank('/tests/123', environ=env)
        request.version_request = vr.APIVersionRequest('1.0')

        resource = wsgi.Resource(Controller())
        resp = resource(request)
        self.assertEqual('{"foo": "bar"}', encodeutils.safe_decode(resp.body))
        self.assertTrue(hasattr(resp, 'headers'))
        expected = 'clustering 1.0'
        self.assertEqual(expected, resp.headers['OpenStack-API-Version'])
        self.assertEqual('OpenStack-API-Version', resp.headers['Vary']) 
开发者ID:openstack,项目名称:senlin,代码行数:19,代码来源:test_wsgi.py

示例8: test_safe_decode

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def test_safe_decode(self):
        safe_decode = encodeutils.safe_decode
        self.assertRaises(TypeError, safe_decode, True)
        self.assertEqual(six.u('ni\xf1o'), safe_decode(six.b("ni\xc3\xb1o"),
                         incoming="utf-8"))
        if six.PY2:
            # In Python 3, bytes.decode() doesn't support anymore
            # bytes => bytes encodings like base64
            self.assertEqual(six.u("test"), safe_decode("dGVzdA==",
                             incoming='base64'))

        self.assertEqual(six.u("strange"), safe_decode(six.b('\x80strange'),
                         errors='ignore'))

        self.assertEqual(six.u('\xc0'), safe_decode(six.b('\xc0'),
                         incoming='iso-8859-1'))

        # Forcing incoming to ascii so it falls back to utf-8
        self.assertEqual(six.u('ni\xf1o'), safe_decode(six.b('ni\xc3\xb1o'),
                         incoming='ascii'))

        self.assertEqual(six.u('foo'), safe_decode(b'foo')) 
开发者ID:openstack,项目名称:oslo.utils,代码行数:24,代码来源:tests_encodeutils.py

示例9: read_tar_image

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def read_tar_image(self, image):
        image_path = image['path']
        with tarfile.open(image_path, 'r') as fil:
            fest = fil.extractfile('manifest.json')
            data = fest.read()
            data = jsonutils.loads(encodeutils.safe_decode(data))
            repo_tags = data[0]['RepoTags']
            if repo_tags:
                repo, tag = repo_tags[0].split(":")
                image['repo'], image['tag'] = repo, tag
            else:
                image_uuid = data[0]['Config'].split('.')[0]
                image['repo'], image['tag'] = image_uuid, '' 
开发者ID:openstack,项目名称:zun,代码行数:15,代码来源:utils.py

示例10: encrypt

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [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) 
开发者ID:openstack,项目名称:zun,代码行数:11,代码来源:crypt.py

示例11: decrypt

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [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() 
开发者ID:openstack,项目名称:zun,代码行数:15,代码来源:crypt.py

示例12: process_result_value

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def process_result_value(self, value, dialect):
        return encodeutils.safe_decode(zlib.decompress(value)) 
开发者ID:dmsimard,项目名称:ara-archive,代码行数:4,代码来源:models.py

示例13: base64_encode_psk

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [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 
开发者ID:openstack,项目名称:neutron-vpnaas,代码行数:14,代码来源:ipsec.py

示例14: _safe_header

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def _safe_header(self, name, value):
        if name in SENSITIVE_HEADERS:
            # because in python3 byte string handling is ... ug
            v = value.encode('utf-8')
            h = hashlib.sha1(v)
            d = h.hexdigest()
            return encodeutils.safe_decode(name), "{SHA1}%s" % d
        else:
            return (encodeutils.safe_decode(name),
                    encodeutils.safe_decode(value)) 
开发者ID:nttcom,项目名称:eclcli,代码行数:12,代码来源:client.py

示例15: safe_header

# 需要导入模块: from oslo_utils import encodeutils [as 别名]
# 或者: from oslo_utils.encodeutils import safe_decode [as 别名]
def safe_header(self, name, value):
        if name in SENSITIVE_HEADERS:
            # because in python3 byte string handling is ... ug
            v = value.encode('utf-8')
            h = hashlib.sha1(v)
            d = h.hexdigest()
            return encodeutils.safe_decode(name), "{SHA1}%s" % d
        else:
            return (encodeutils.safe_decode(name),
                    encodeutils.safe_decode(value)) 
开发者ID:nttcom,项目名称:eclcli,代码行数:12,代码来源:http.py


注:本文中的oslo_utils.encodeutils.safe_decode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。