* Add types.py from devel to support backport. * [stable-2.9] Backport ansible-test CI provider support. (#71614) * Add encoding.py from devel to support backports. * Add io.py from devel to support backports. * Update ansible-test support for CI providers. (#69522) Refactored CI provider code to simplify multiple provider support and addition of new providers. (cherry picked from commitd8e0aadc0d) * Add Shippable request signing to ansible-test. (#69526) (cherry picked from commite7c2eb519b) * ansible-test local change detection: use --base-branch if specified (#69508) (cherry picked from commit43acd61901) * Add Azure Pipelines support to ansible-test. (cherry picked from commit8ffaed00f8) * Update ansible-test remote endpoint handling. (#71413) * Request ansible-core-ci resources by provider. * Remove obsolete us-east-2 CI endpoint. * Add new --remote-endpoint option. * Add warning for --remote-aws-region option. * Update service endpoints. * Allow non-standard remote stages. * Add changelog fragment. (cherry picked from commitd099591964) * Fix ansible-test coverage traceback. (#71446) * Add integration test for ansible-test coverage. * Fix ansible-test coverage traceback. * Fix coverage reporting on Python 2.6. (cherry picked from commitf5b6df14ab) * Use new endpoint for Parallels based instances. (cherry picked from commit98febab975) * Add pause to avoid same mtime in test. (cherry picked from commit3d769f3a76) Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit417e408f59)
42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
"""Functions for encoding and decoding strings."""
|
|
from __future__ import (absolute_import, division, print_function)
|
|
__metaclass__ = type
|
|
|
|
from . import types as t
|
|
|
|
ENCODING = 'utf-8'
|
|
|
|
Text = type(u'')
|
|
|
|
|
|
def to_optional_bytes(value, errors='strict'): # type: (t.Optional[t.AnyStr], str) -> t.Optional[bytes]
|
|
"""Return the given value as bytes encoded using UTF-8 if not already bytes, or None if the value is None."""
|
|
return None if value is None else to_bytes(value, errors)
|
|
|
|
|
|
def to_optional_text(value, errors='strict'): # type: (t.Optional[t.AnyStr], str) -> t.Optional[t.Text]
|
|
"""Return the given value as text decoded using UTF-8 if not already text, or None if the value is None."""
|
|
return None if value is None else to_text(value, errors)
|
|
|
|
|
|
def to_bytes(value, errors='strict'): # type: (t.AnyStr, str) -> bytes
|
|
"""Return the given value as bytes encoded using UTF-8 if not already bytes."""
|
|
if isinstance(value, bytes):
|
|
return value
|
|
|
|
if isinstance(value, Text):
|
|
return value.encode(ENCODING, errors)
|
|
|
|
raise Exception('value is not bytes or text: %s' % type(value))
|
|
|
|
|
|
def to_text(value, errors='strict'): # type: (t.AnyStr, str) -> t.Text
|
|
"""Return the given value as text decoded using UTF-8 if not already text."""
|
|
if isinstance(value, bytes):
|
|
return value.decode(ENCODING, errors)
|
|
|
|
if isinstance(value, Text):
|
|
return value
|
|
|
|
raise Exception('value is not bytes or text: %s' % type(value))
|