Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
61 lines (47 loc) · 1.55 KB
/
Copy pathutils.py
File metadata and controls
61 lines (47 loc) · 1.55 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
61
"""Utility functions for generating pup names."""
import uuid
from generator import generate
def generate_with_uuid(
words: int = 2, separator: str = "-", used: set[str] | None = None
) -> str:
"""Generate a unique pup name with a short UUID suffix.
Args:
words: Number of base words in name.
separator: Word separator.
used: Set of previously generated names.
Returns:
Unique name with UUID suffix.
Raises:
RuntimeError: If a unique name can't be generated.
"""
if used is None:
used = set()
for _ in range(1000):
base = generate(words, separator)
suffix = uuid.uuid4().hex[:4]
full = f"{base}{separator}{suffix}"
if full not in used:
used.add(full)
return full
raise RuntimeError("Unable to find unique name with UUID.")
def unique(used: set[str], words: int = 2, separator: str = "-") -> str:
"""Generate a unique pup name.
Args:
used: Set of previously generated names.
words: Number of base words in name.
separator: Word separator.
Returns:
A unique name.
Raises:
RuntimeError: If no unique name can be found.
"""
for _ in range(1000):
name = generate(words, separator)
if name not in used:
used.add(name)
return name
raise RuntimeError(
"Unable to find a unique name after 1000 attempts. "
"Try increasing the number of words, reducing the count, "
"or using '--uuid' for uniqueness."
)