-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
60 lines (43 loc) · 1.73 KB
/
Copy pathutils.py
File metadata and controls
60 lines (43 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import logging
import re
from functools import lru_cache
_LOGGER = logging.getLogger(__name__)
def _sanitize_session_id(session_id):
"""Obfuscates a session ID, preserving the first 8 characters and dashes.
This function retains the first 8 characters of the given session ID, and replaces
the subsequent characters with 'X', except for dashes, which are preserved.
Args:
session_id (str): The session ID to be sanitized.
Returns:
str: The sanitized session ID.
"""
sanitized = session_id[:8] + "".join("-" if char == "-" else "X" for char in session_id[8:])
return sanitized
@lru_cache(maxsize=42)
def _camel_to_snake_case(name):
"""
Convert a CamelCase string to snake_case.
This function implements an LRU cache to avoid doing the operation multiple times. As
it is used with a very limited set of inputs, the cache size is set to 42 as the usual
expected input is 21 distinct strings.
Args:
name (str): The CamelCase string to be converted.
Returns:
str: The converted snake_case string.
Example:
>>> _camel_to_snake_case("CamelCaseString")
'camel_case_string'
"""
# Handle the all-uppercase special case first
if name.isupper():
return name.lower()
# Convert camelCased portions to snake_case
name = re.sub("([a-z0-9])([A-Z])", r"\1_\2", name)
# Insert underscores between letters and digits
name = re.sub("([a-z])([0-9])", r"\1_\2", name)
name = re.sub("([0-9])([a-z])", r"\1_\2", name)
# Replace non-alphanumeric characters (excluding underscores) with underscores
name = re.sub(r"[^\w]", "_", name)
# Handle the remaining uppercase letters
name = name.lower()
return name