Skip to content

Utils

blake2signer.utils

Miscellaneous utilities.

b32decode(data)

Decode data encoded as Base 32 without padding.

Parameters:

Name Type Description Default
data bytes

Data to decode.

required

Returns:

Type Description
bytes

Original data.

Source code in blake2signer/utils.py
64
65
66
67
68
69
70
71
72
73
def b32decode(data: bytes) -> bytes:
    """Decode data encoded as Base 32 without padding.

    Args:
        data: Data to decode.

    Returns:
        Original data.
    """
    return base64.b32decode(data + (b'=' * ((8 - (len(data) % 8)) % 8)))

b32encode(data)

Encode data as Base 32, stripping padding.

Parameters:

Name Type Description Default
data bytes

Data to encode.

required

Returns:

Type Description
bytes

Encoded data.

Source code in blake2signer/utils.py
52
53
54
55
56
57
58
59
60
61
def b32encode(data: bytes) -> bytes:
    """Encode data as Base 32, stripping padding.

    Args:
        data: Data to encode.

    Returns:
        Encoded data.
    """
    return base64.b32encode(data).rstrip(b'=')

b64decode(data)

Decode data encoded as Base 64 URL safe without padding.

Parameters:

Name Type Description Default
data bytes

Data to decode.

required

Returns:

Type Description
bytes

Original data.

Source code in blake2signer/utils.py
40
41
42
43
44
45
46
47
48
49
def b64decode(data: bytes) -> bytes:
    """Decode data encoded as Base 64 URL safe without padding.

    Args:
        data: Data to decode.

    Returns:
        Original data.
    """
    return base64.urlsafe_b64decode(data + (b'=' * (len(data) % 4)))

b64encode(data)

Encode data as Base 64 URL safe, stripping padding.

Parameters:

Name Type Description Default
data bytes

Data to encode.

required

Returns:

Type Description
bytes

Encoded data.

Source code in blake2signer/utils.py
28
29
30
31
32
33
34
35
36
37
def b64encode(data: bytes) -> bytes:
    """Encode data as Base 64 URL safe, stripping padding.

    Args:
        data: Data to encode.

    Returns:
        Encoded data.
    """
    return base64.urlsafe_b64encode(data).rstrip(b'=')

file_mode_is_text(file)

Check if given file is opened in text mode, or otherwise in binary mode.

Parameters:

Name Type Description Default
file typing.IO

File to check its mode.

required

Returns:

Type Description
bool

True if file is opened in text mode, False otherwise.

Source code in blake2signer/utils.py
112
113
114
115
116
117
118
119
120
121
122
123
124
def file_mode_is_text(file: typing.IO) -> bool:
    """Check if given file is opened in text mode, or otherwise in binary mode.

    Args:
        file: File to check its mode.

    Returns:
        True if file is opened in text mode, False otherwise.
    """
    # Is there a better way to determine this? I thought on reading one byte/char,
    # then checking the type, but the file might be writable only, so it wouldn't
    # work.
    return isinstance(file, io.TextIOBase)

force_bytes(value)

Force a given value into bytes.

Parameters:

Name Type Description Default
value typing.Any

Value to convert to bytes.

required

Returns:

Type Description
bytes

Converted value into bytes.

Source code in blake2signer/utils.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def force_bytes(value: typing.Any) -> bytes:
    """Force a given value into bytes.

    Args:
        value: Value to convert to bytes.

    Returns:
        Converted value into bytes.
    """
    if isinstance(value, bytes):
        return value

    if isinstance(value, str):
        return value.encode('utf-8', errors='strict')

    return bytes(value)

hexdecode(data)

Decode data encoded as hexadecimal (uppercase).

Parameters:

Name Type Description Default
data bytes

Data to decode.

required

Returns:

Type Description
bytes

Original data.

Source code in blake2signer/utils.py
88
89
90
91
92
93
94
95
96
97
def hexdecode(data: bytes) -> bytes:
    """Decode data encoded as hexadecimal (uppercase).

    Args:
        data: Data to decode.

    Returns:
        Original data.
    """
    return base64.b16decode(data)

hexencode(data)

Encode data as hexadecimal (uppercase).

Parameters:

Name Type Description Default
data bytes

Data to encode.

required

Returns:

Type Description
bytes

Encoded data.

Source code in blake2signer/utils.py
76
77
78
79
80
81
82
83
84
85
def hexencode(data: bytes) -> bytes:
    """Encode data as hexadecimal (uppercase).

    Args:
        data: Data to encode.

    Returns:
        Encoded data.
    """
    return base64.b16encode(data)

ordinal(number)

Convert an integer into its ordinal representation.

Parameters:

Name Type Description Default
number int

integer number to get its ordinal representation.

required

Returns:

Type Description
str

The ordinal string representation of the number as the number + ordinal suffix.

Examples:

>>> ordinal(0)
'0th'
>>> ordinal(3)
'3rd'
Source code in blake2signer/utils.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def ordinal(number: int) -> str:
    """Convert an integer into its ordinal representation.

    Args:
        number: integer number to get its ordinal representation.

    Returns:
        The ordinal string representation of the number as the number + ordinal suffix.

    Examples:
        >>> ordinal(0)
        '0th'
        >>> ordinal(3)
        '3rd'
    """
    # From https://stackoverflow.com/a/50992575
    if 11 <= (number % 100) <= 13:
        suffix = 'th'
    else:
        suffixes = ('th', 'st', 'nd', 'rd', 'th')
        idx = min(number % 10, 4)
        suffix = suffixes[idx]

    return f'{number}{suffix}'

timestamp_to_aware_datetime(timestamp)

Convert a UNIX timestamp into an aware datetime in UTC.

Parameters:

Name Type Description Default
timestamp typing.Union[int, float]

UNIX timestamp to convert.

required

Returns:

Type Description
datetime

Converted timestamp into an aware datetime in UTC.

Source code in blake2signer/utils.py
100
101
102
103
104
105
106
107
108
109
def timestamp_to_aware_datetime(timestamp: typing.Union[int, float]) -> datetime:
    """Convert a UNIX timestamp into an aware datetime in UTC.

    Args:
        timestamp: UNIX timestamp to convert.

    Returns:
        Converted timestamp into an aware datetime in UTC.
    """
    return datetime.fromtimestamp(timestamp, tz=timezone.utc)