-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.py
96 lines (75 loc) · 2.58 KB
/
common.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright © 2020 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from random import randint
VERBOSE=False
class Colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class Console:
def verbose(msg):
if VERBOSE:
print(f"{Colors.OKBLUE}{msg}{Colors.ENDC}".format(msg=str(msg)))
def print(msg=''):
print(msg)
def println(no=1):
for i in range(no):
print()
def ok(msg):
print(f"{Colors.OKGREEN}{msg}{Colors.ENDC}".format(msg=str(msg)))
def error(msg):
Console.fail(msg)
def fail(msg):
print(f"{Colors.FAIL}Error: {msg}{Colors.ENDC}".format(msg=str(msg)))
def warn(msg):
print(f"{Colors.WARNING}Warning: {msg}{Colors.ENDC}".format(msg=str(msg)))
def progress(count, total, status=''):
bar_len = 60
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))
sys.stdout.flush()
class RateLimitData:
DEFAULT_RATE_LIMIT_MAX = 100
DEFAULT_RATE_LIMIT_SLEEP = 30*60
def __init__(self, max_calls, sleep, enabled=False, random=False):
self.__enabled = enabled
self.__max_calls = max_calls
self.__sleep = sleep
self.__random = random
def enabled(self):
return self.__enabled
def set_enabled(self, aBool):
self.__enabled = aBool
def random(self):
return self.__random
def max_calls(self):
if self.__random:
return randint(1, self.__max_calls)
return self.__max_calls
def sleep(self):
if self.__random:
return randint(1, self.__sleep)
return self.__sleep
def set_max_calls(self, int_value):
self.__max_calls = int_value
def set_sleep(self, int_value):
self.__sleep = int_value