-
Notifications
You must be signed in to change notification settings - Fork 22
/
__init__.py
56 lines (46 loc) · 1.5 KB
/
__init__.py
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
"""
Utility functions.
..
PYTEST_DONT_REWRITE
"""
import pathlib
import random
import string
from functools import lru_cache
import salt.utils.user
def random_string(prefix, size=6, uppercase=True, lowercase=True, digits=True):
"""
Generates a random string.
:keyword str prefix: The prefix for the random string
:keyword int size: The size of the random string
:keyword bool uppercase: If true, include upper-cased ascii chars in choice sample
:keyword bool lowercase: If true, include lower-cased ascii chars in choice sample
:keyword bool digits: If true, include digits in choice sample
:return str: The random string
"""
if not any([uppercase, lowercase, digits]):
raise RuntimeError("At least one of 'uppercase', 'lowercase' or 'digits' needs to be true")
choices = []
if uppercase:
choices.extend(string.ascii_uppercase)
if lowercase:
choices.extend(string.ascii_lowercase)
if digits:
choices.extend(string.digits)
return prefix + "".join(random.choice(choices) for _ in range(size))
@lru_cache(maxsize=1)
def running_username():
"""
Return the username that is running the code.
"""
return salt.utils.user.get_user()
def cast_to_pathlib_path(value):
"""
Cast the passed value to an instance of ``pathlib.Path``.
"""
if isinstance(value, pathlib.Path):
return value
try:
return pathlib.Path(value.strpath)
except AttributeError:
return pathlib.path(str(value))