當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。