forked from YongxueHong/ipa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_misc.py
79 lines (69 loc) · 2.13 KB
/
utils_misc.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import time
import random
import string
import re
def wait_for_output(func, timeout, first=0.0, step=1.0):
end_time = time.time() + float(timeout)
time.sleep(first)
while time.time() < end_time:
output = func()
if output:
return output
time.sleep(step)
return None
def wait_for_keyword(func, keyword, timeout, first=0.0, step=1.0):
end_time = time.time() + float(timeout)
time.sleep(first)
while time.time() < end_time:
output = func()
if re.search(keyword, output):
return output
time.sleep(step)
return None
def py3_to_str(bytes_or_str):
if isinstance(bytes_or_str, bytes):
value = bytes_or_str.decode('utf-8')
else:
value = bytes_or_str
return value
def py3_to_bytes(bytes_or_str):
if isinstance(bytes_or_str, str):
value = bytes_or_str.encode('utf-8')
else:
value = bytes_or_str
return value
def py2_to_unicode(unicode_or_str):
if isinstance(unicode_or_str, str):
value = unicode(unicode_or_str, 'utf-8')
else:
value = unicode_or_str
return value
def py2_to_srt(unicode_or_str):
if isinstance(unicode_or_str, unicode):
value = unicode_or_str.encode('utf-8')
else:
value = unicode_or_str
return value
def generate_random_string(length, ignore_str=string.punctuation,
convert_str=""):
"""
Return a random string using alphanumeric characters.
:param length: Length of the string that will be generated.
:param ignore_str: Characters that will not include in generated string.
:param convert_str: Characters that need to be escaped (prepend "\\").
:return: The generated random string.
"""
r = random.SystemRandom()
sr = ""
chars = string.ascii_letters + string.digits + string.punctuation
if not ignore_str:
ignore_str = ""
for i in ignore_str:
chars = chars.replace(i, "")
while length > 0:
tmp = r.choice(chars)
if convert_str and (tmp in convert_str):
tmp = "\\%s" % tmp
sr += tmp
length -= 1
return sr