• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

localstack / localstack / 311e4cb7-85c6-4815-9a05-f76a2f16b041

27 Jan 2025 05:40PM UTC coverage: 86.9% (-0.004%) from 86.904%
311e4cb7-85c6-4815-9a05-f76a2f16b041

push

circleci

web-flow
Fix: validate schedule_expression for EventBridge Scheduler (#12191)

15 of 15 new or added lines in 1 file covered. (100.0%)

127 existing lines in 14 files now uncovered.

61173 of 70395 relevant lines covered (86.9%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.68
/localstack-core/localstack/utils/strings.py
1
import base64
1✔
2
import binascii
1✔
3
import hashlib
1✔
4
import itertools
1✔
5
import random
1✔
6
import re
1✔
7
import string
1✔
8
import uuid
1✔
9
import zlib
1✔
10
from typing import Dict, List, Union
1✔
11

12
from localstack.config import DEFAULT_ENCODING
1✔
13

14
_unprintables = (
1✔
15
    range(0x00, 0x09),
16
    range(0x0A, 0x0A),
17
    range(0x0B, 0x0D),
18
    range(0x0E, 0x20),
19
    range(0xD800, 0xE000),
20
    range(0xFFFE, 0x10000),
21
)
22

23
# regular expression for unprintable characters
24
# Based on https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html
25
#     #x9 | #xA | #xD | #x20 to #xD7FF | #xE000 to #xFFFD | #x10000 to #x10FFFF
26
REGEX_UNPRINTABLE_CHARS = re.compile(
1✔
27
    f"[{re.escape(''.join(map(chr, itertools.chain(*_unprintables))))}]"
28
)
29

30

31
def to_str(obj: Union[str, bytes], encoding: str = DEFAULT_ENCODING, errors="strict") -> str:
1✔
32
    """If ``obj`` is an instance of ``binary_type``, return
33
    ``obj.decode(encoding, errors)``, otherwise return ``obj``"""
34
    return obj.decode(encoding, errors) if isinstance(obj, bytes) else obj
1✔
35

36

37
def to_bytes(obj: Union[str, bytes], encoding: str = DEFAULT_ENCODING, errors="strict") -> bytes:
1✔
38
    """If ``obj`` is an instance of ``text_type``, return
39
    ``obj.encode(encoding, errors)``, otherwise return ``obj``"""
40
    return obj.encode(encoding, errors) if isinstance(obj, str) else obj
1✔
41

42

43
def truncate(data: str, max_length: int = 100) -> str:
1✔
44
    data = str(data or "")
1✔
45
    return ("%s..." % data[:max_length]) if len(data) > max_length else data
1✔
46

47

48
def is_string(s, include_unicode=True, exclude_binary=False):
1✔
49
    if isinstance(s, bytes) and exclude_binary:
1✔
50
        return False
×
51
    if isinstance(s, str):
1✔
52
        return True
1✔
53
    if include_unicode and isinstance(s, str):
×
54
        return True
×
55
    return False
×
56

57

58
def is_string_or_bytes(s):
1✔
59
    return is_string(s) or isinstance(s, str) or isinstance(s, bytes)
×
60

61

62
def is_base64(s):
1✔
63
    regex = r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
1✔
64
    return is_string(s) and re.match(regex, s)
1✔
65

66

67
_re_camel_to_snake_case = re.compile("((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))")
1✔
68

69

70
def camel_to_snake_case(string: str) -> str:
1✔
71
    return _re_camel_to_snake_case.sub(r"_\1", string).replace("__", "_").lower()
1✔
72

73

74
def snake_to_camel_case(string: str, capitalize_first: bool = True) -> str:
1✔
75
    components = string.split("_")
1✔
76
    start_idx = 0 if capitalize_first else 1
1✔
77
    components = [x.title() for x in components[start_idx:]]
1✔
78
    return "".join(components)
1✔
79

80

81
def canonicalize_bool_to_str(val: bool) -> str:
1✔
82
    return "true" if str(val).lower() == "true" else "false"
1✔
83

84

85
def convert_to_printable_chars(value: Union[List, Dict, str]) -> str:
1✔
86
    """Removes all unprintable characters from the given string."""
87
    from localstack.utils.objects import recurse_object
1✔
88

89
    if isinstance(value, (dict, list)):
1✔
90

91
        def _convert(obj, **kwargs):
1✔
92
            if isinstance(obj, str):
1✔
93
                return convert_to_printable_chars(obj)
1✔
94
            return obj
1✔
95

96
        return recurse_object(value, _convert)
1✔
97

98
    result = REGEX_UNPRINTABLE_CHARS.sub("", value)
1✔
99
    return result
1✔
100

101

102
def first_char_to_lower(s: str) -> str:
1✔
103
    return s and "%s%s" % (s[0].lower(), s[1:])
1✔
104

105

106
def first_char_to_upper(s: str) -> str:
1✔
107
    return s and "%s%s" % (s[0].upper(), s[1:])
1✔
108

109

110
def str_to_bool(value):
1✔
111
    """Return the boolean value of the given string, or the verbatim value if it is not a string"""
112
    if isinstance(value, str):
1✔
113
        true_strings = ["true", "True"]
1✔
114
        return value in true_strings
1✔
115
    return value
1✔
116

117

118
def str_insert(string, index, content):
1✔
119
    """Insert a substring into an existing string at a certain index."""
120
    return "%s%s%s" % (string[:index], content, string[index:])
×
121

122

123
def str_remove(string, index, end_index=None):
1✔
124
    """Remove a substring from an existing string at a certain from-to index range."""
125
    end_index = end_index or (index + 1)
×
126
    return "%s%s" % (string[:index], string[end_index:])
×
127

128

129
def str_startswith_ignore_case(value: str, prefix: str) -> bool:
1✔
130
    return value[: len(prefix)].lower() == prefix.lower()
×
131

132

133
def short_uid() -> str:
1✔
134
    return str(uuid.uuid4())[0:8]
1✔
135

136

137
def long_uid() -> str:
1✔
138
    return str(uuid.uuid4())
1✔
139

140

141
def md5(string: Union[str, bytes]) -> str:
1✔
142
    m = hashlib.md5()
1✔
143
    m.update(to_bytes(string))
1✔
144
    return m.hexdigest()
1✔
145

146

147
def checksum_crc32(string: Union[str, bytes]) -> str:
1✔
148
    bytes = to_bytes(string)
1✔
149
    checksum = zlib.crc32(bytes)
1✔
150
    return base64.b64encode(checksum.to_bytes(4, "big")).decode()
1✔
151

152

153
def checksum_crc32c(string: Union[str, bytes]):
1✔
154
    # import botocore locally here to avoid a dependency of the CLI to botocore
155
    from botocore.httpchecksum import CrtCrc32cChecksum
1✔
156

157
    checksum = CrtCrc32cChecksum()
1✔
158
    checksum.update(to_bytes(string))
1✔
159
    return base64.b64encode(checksum.digest()).decode()
1✔
160

161

162
def checksum_crc64nvme(string: Union[str, bytes]):
1✔
163
    # import botocore locally here to avoid a dependency of the CLI to botocore
164
    from botocore.httpchecksum import CrtCrc64NvmeChecksum
1✔
165

166
    checksum = CrtCrc64NvmeChecksum()
1✔
167
    checksum.update(to_bytes(string))
1✔
168
    return base64.b64encode(checksum.digest()).decode()
1✔
169

170

171
def hash_sha1(string: Union[str, bytes]) -> str:
1✔
172
    digest = hashlib.sha1(to_bytes(string)).digest()
1✔
173
    return base64.b64encode(digest).decode()
1✔
174

175

176
def hash_sha256(string: Union[str, bytes]) -> str:
1✔
177
    digest = hashlib.sha256(to_bytes(string)).digest()
1✔
178
    return base64.b64encode(digest).decode()
1✔
179

180

181
def base64_to_hex(b64_string: str) -> bytes:
1✔
182
    return binascii.hexlify(base64.b64decode(b64_string))
1✔
183

184

185
def base64_decode(data: Union[str, bytes]) -> bytes:
1✔
186
    """Decode base64 data - with optional padding, and able to handle urlsafe encoding (containing -/_)."""
187
    data = to_str(data)
1✔
188
    missing_padding = len(data) % 4
1✔
189
    if missing_padding != 0:
1✔
UNCOV
190
        data = to_str(data) + "=" * (4 - missing_padding)
×
191
    if "-" in data or "_" in data:
1✔
192
        return base64.urlsafe_b64decode(data)
1✔
193
    return base64.b64decode(data)
1✔
194

195

196
def get_random_hex(length: int) -> str:
1✔
197
    return "".join(random.choices(string.hexdigits[:16], k=length)).lower()
1✔
198

199

200
def remove_leading_extra_slashes(input: str) -> str:
1✔
201
    """
202
    Remove leading extra slashes from the given input string.
203
    Example: '///foo/bar' -> '/foo/bar'
204
    """
UNCOV
205
    return re.sub(r"^/+", "/", input)
×
206

207

208
def prepend_with_slash(input: str) -> str:
1✔
209
    """
210
    Prepend a slash `/` to a given string if it does not have one already.
211
    """
212
    if not input.startswith("/"):
1✔
213
        return f"/{input}"
1✔
214
    return input
1✔
215

216

217
def key_value_pairs_to_dict(pairs: str, delimiter: str = ",", separator: str = "=") -> dict:
1✔
218
    """
219
    Converts a string of key-value pairs to a dictionary.
220

221
    Args:
222
        pairs (str): A string containing key-value pairs separated by a delimiter.
223
        delimiter (str): The delimiter used to separate key-value pairs (default is comma ',').
224
        separator (str): The separator between keys and values (default is '=').
225

226
    Returns:
227
        dict: A dictionary containing the parsed key-value pairs.
228
    """
229
    splits = [split_pair.partition(separator) for split_pair in pairs.split(delimiter)]
1✔
230
    return {key.strip(): value.strip() for key, _, value in splits}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc