本文整理汇总了Python中botocore.compat.six.text_type方法的典型用法代码示例。如果您正苦于以下问题:Python six.text_type方法的具体用法?Python six.text_type怎么用?Python six.text_type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类botocore.compat.six
的用法示例。
在下文中一共展示了six.text_type方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _convert_to_string
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def _convert_to_string(data):
scalar_types = six.string_types + six.integer_types
if isinstance(data, six.binary_type):
if sys.version_info[0] >= 3:
return data.decode('utf8')
else:
return data
elif isinstance(data, six.text_type):
if sys.version_info[0] >= 3:
return data
else:
return data.encode('utf8')
elif isinstance(data, scalar_types) or hasattr(data, '__str__'):
return str(data)
else:
LOG.error('echo called with an unsupported data type')
LOG.debug('data class = ' + data.__class__.__name__)
示例2: percent_encode
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def percent_encode(input_str, safe=SAFE_CHARS):
"""Urlencodes a string.
Whereas percent_encode_sequence handles taking a dict/sequence and
producing a percent encoded string, this function deals only with
taking a string (not a dict/sequence) and percent encoding it.
If given the binary type, will simply URL encode it. If given the
text type, will produce the binary type by UTF-8 encoding the
text. If given something else, will convert it to the text type
first.
"""
# If its not a binary or text string, make it a text string.
if not isinstance(input_str, (six.binary_type, six.text_type)):
input_str = six.text_type(input_str)
# If it's not bytes, make it bytes by UTF-8 encoding it.
if not isinstance(input_str, six.binary_type):
input_str = input_str.encode('utf-8')
return quote(input_str, safe=safe)
示例3: type_check
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def type_check(valid_types):
def _create_type_check_guard(func):
def _on_passes_type_check(self, param, shape, errors, name):
if _type_check(param, errors, name):
return func(self, param, shape, errors, name)
def _type_check(param, errors, name):
if not isinstance(param, valid_types):
valid_type_names = [six.text_type(t) for t in valid_types]
errors.report(name, 'invalid type', param=param,
valid_types=valid_type_names)
return False
return True
return _on_passes_type_check
return _create_type_check_guard
示例4: reset_stream
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def reset_stream(self):
"""Resets the streaming body to it's initial position.
If the request contains a streaming body (a streamable file-like object)
seek to the object's initial position to ensure the entire contents of
the object is sent. This is a no-op for static bytes-like body types.
"""
# Trying to reset a stream when there is a no stream will
# just immediately return. It's not an error, it will produce
# the same result as if we had actually reset the stream (we'll send
# the entire body contents again if we need to).
# Same case if the body is a string/bytes/bytearray type.
non_seekable_types = (six.binary_type, six.text_type, bytearray)
if self.body is None or isinstance(self.body, non_seekable_types):
return
try:
logger.debug("Rewinding stream: %s", self.body)
self.body.seek(0)
except Exception as e:
logger.debug("Unable to rewind stream: %s", e)
raise UnseekableStreamError(stream_object=self.body)
示例5: __exit__
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def __exit__(self, exc_type, exc_value, *args):
cancel = False
cancel_msg = ''
cancel_exc_type = FatalError
# If a exception was raised in the context handler, signal to cancel
# all of the inprogress futures in the shutdown.
if exc_type:
cancel = True
cancel_msg = six.text_type(exc_value)
if not cancel_msg:
cancel_msg = repr(exc_value)
# If it was a KeyboardInterrupt, the cancellation was initiated
# by the user.
if isinstance(exc_value, KeyboardInterrupt):
cancel_exc_type = CancelledError
self._shutdown(cancel, cancel_msg, cancel_exc_type)
示例6: _document_str
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def _document_str(self, section, value, path):
# We do the string conversion because this might accept a type that
# we don't specifically address.
section.write(u"'%s'," % six.text_type(value))
示例7: _get_base64
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def _get_base64(self, value):
# Returns the base64-encoded version of value, handling
# both strings and bytes. The returned value is a string
# via the default encoding.
if isinstance(value, six.text_type):
value = value.encode(self.DEFAULT_ENCODING)
return base64.b64encode(value).strip().decode(
self.DEFAULT_ENCODING)
示例8: _encode_payload
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def _encode_payload(self, body):
if isinstance(body, six.text_type):
return body.encode(self.DEFAULT_ENCODING)
return body
示例9: _default_serialize
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def _default_serialize(self, xmlnode, params, shape, name):
node = ElementTree.SubElement(xmlnode, name)
node.text = six.text_type(params)
示例10: calc_signature
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def calc_signature(self, request, params):
logger.debug("Calculating signature using v2 auth.")
split = urlsplit(request.url)
path = split.path
if len(path) == 0:
path = '/'
string_to_sign = '%s\n%s\n%s\n' % (request.method,
split.netloc,
path)
lhmac = hmac.new(self.credentials.secret_key.encode('utf-8'),
digestmod=sha256)
pairs = []
for key in sorted(params):
# Any previous signature should not be a part of this
# one, so we skip that particular key. This prevents
# issues during retries.
if key == 'Signature':
continue
value = six.text_type(params[key])
pairs.append(quote(key.encode('utf-8'), safe='') + '=' +
quote(value.encode('utf-8'), safe='-_~'))
qs = '&'.join(pairs)
string_to_sign += qs
logger.debug('String to sign: %s', string_to_sign)
lhmac.update(string_to_sign.encode('utf-8'))
b64 = base64.b64encode(lhmac.digest()).strip().decode('utf-8')
return (qs, b64)
示例11: _validate_blob
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def _validate_blob(self, param, shape, errors, name):
if isinstance(param, (bytes, bytearray, six.text_type)):
return
elif hasattr(param, 'read'):
# File like objects are also allowed for blob types.
return
else:
errors.report(name, 'invalid type', param=param,
valid_types=[str(bytes), str(bytearray),
'file-like object'])
示例12: _validate_timestamp
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def _validate_timestamp(self, param, shape, errors, name):
# We don't use @type_check because datetimes are a bit
# more flexible. You can either provide a datetime
# object, or a string that parses to a datetime.
is_valid_type = self._type_check_datetime(param)
if not is_valid_type:
valid_type_names = [six.text_type(datetime), 'timestamp-string']
errors.report(name, 'invalid type', param=param,
valid_types=valid_type_names)
示例13: generate_presigned_url
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def generate_presigned_url(self, url, date_less_than=None, policy=None):
"""Creates a signed CloudFront URL based on given parameters.
:type url: str
:param url: The URL of the protected object
:type date_less_than: datetime
:param date_less_than: The URL will expire after that date and time
:type policy: str
:param policy: The custom policy, possibly built by self.build_policy()
:rtype: str
:return: The signed URL.
"""
if (date_less_than is not None and policy is not None or
date_less_than is None and policy is None):
e = 'Need to provide either date_less_than or policy, but not both'
raise ValueError(e)
if date_less_than is not None:
# We still need to build a canned policy for signing purpose
policy = self.build_policy(url, date_less_than)
if isinstance(policy, six.text_type):
policy = policy.encode('utf8')
if date_less_than is not None:
params = ['Expires=%s' % int(datetime2timestamp(date_less_than))]
else:
params = ['Policy=%s' % self._url_b64encode(policy).decode('utf8')]
signature = self.rsa_signer(policy)
params.extend([
'Signature=%s' % self._url_b64encode(signature).decode('utf8'),
'Key-Pair-Id=%s' % self.key_id,
])
return self._build_url(url, params)
示例14: _convert_to_bytes
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def _convert_to_bytes(self, mixed_buffer):
# Take a list of mixed str/bytes and convert it
# all into a single bytestring.
# Any six.text_types will be encoded as utf-8.
bytes_buffer = []
for chunk in mixed_buffer:
if isinstance(chunk, six.text_type):
bytes_buffer.append(chunk.encode('utf-8'))
else:
bytes_buffer.append(chunk)
msg = b"\r\n".join(bytes_buffer)
return msg
示例15: body
# 需要导入模块: from botocore.compat import six [as 别名]
# 或者: from botocore.compat.six import text_type [as 别名]
def body(self):
p = models.PreparedRequest()
p.prepare_headers({})
p.prepare_body(self.data, self.files)
if isinstance(p.body, six.text_type):
p.body = p.body.encode('utf-8')
return p.body