from __future__ import annotations
from typing import TYPE_CHECKING
from ._license import _license
from ._version import _version
if TYPE_CHECKING:
from ._typing import Buffer
try:
from ._pybase64 import (
_get_simd_name,
_get_simd_path,
b64decode,
b64decode_as_bytearray,
b64encode,
b64encode_as_string,
encodebytes,
)
except ImportError:
from ._fallback import (
_get_simd_name,
_get_simd_path,
b64decode,
b64decode_as_bytearray,
b64encode,
b64encode_as_string,
encodebytes,
)
__all__ = (
"b64decode",
"b64decode_as_bytearray",
"b64encode",
"b64encode_as_string",
"encodebytes",
"standard_b64encode",
"standard_b64decode",
"urlsafe_b64encode",
"urlsafe_b64decode",
)
__version__ = _version
[docs]
def get_license_text() -> str:
"""Returns pybase64 license information as a :class:`str` object.
The result includes libbase64 license information as well.
"""
return _license
[docs]
def get_version() -> str:
"""Returns pybase64 version as a :class:`str` object.
The result reports if the C extension is used or not.
e.g. `1.0.0 (C extension active - AVX2)`
"""
simd_name = _get_simd_name(_get_simd_path())
if simd_name != "fallback":
return f"{__version__} (C extension active - {simd_name})"
return f"{__version__} (C extension inactive)"
[docs]
def standard_b64encode(s: Buffer) -> bytes:
"""Encode bytes using the standard Base64 alphabet.
Argument ``s`` is a :term:`bytes-like object` to encode.
The result is returned as a :class:`bytes` object.
"""
return b64encode(s)
[docs]
def standard_b64decode(s: str | Buffer) -> bytes:
"""Decode bytes encoded with the standard Base64 alphabet.
Argument ``s`` is a :term:`bytes-like object` or ASCII string to
decode.
The result is returned as a :class:`bytes` object.
A :exc:`binascii.Error` is raised if the input is incorrectly padded.
Characters that are not in the standard alphabet are discarded prior
to the padding check.
"""
return b64decode(s)
[docs]
def urlsafe_b64encode(s: Buffer) -> bytes:
"""Encode bytes using the URL- and filesystem-safe Base64 alphabet.
Argument ``s`` is a :term:`bytes-like object` to encode.
The result is returned as a :class:`bytes` object.
The alphabet uses '-' instead of '+' and '_' instead of '/'.
"""
return b64encode(s, b"-_")
[docs]
def urlsafe_b64decode(s: str | Buffer) -> bytes:
"""Decode bytes using the URL- and filesystem-safe Base64 alphabet.
Argument ``s`` is a :term:`bytes-like object` or ASCII string to
decode.
The result is returned as a :class:`bytes` object.
A :exc:`binascii.Error` is raised if the input is incorrectly padded.
Characters that are not in the URL-safe base-64 alphabet, and are not
a plus '+' or slash '/', are discarded prior to the padding check.
The alphabet uses '-' instead of '+' and '_' instead of '/'.
"""
return b64decode(s, b"-_")