-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
71 lines (59 loc) · 2.05 KB
/
utils.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
import os
import readline
from glob import glob
import dateparser
import yaml
# noinspection PyStatementEffect
readline # this does nothing but make sure the import readline is not removed accidently
def load_yaml(filename):
with open(filename, 'r') as stream:
return yaml.safe_load(stream)
def save_yaml(data, filename):
with open(filename, 'w') as outfile:
yaml.dump(data, outfile, default_flow_style=False)
def remove_tmp_files(base):
for ext in ["aux", "log"]:
os.remove(base + "." + ext)
def get_possible_recipents():
return [os.path.splitext(os.path.basename(file))[0] for file in glob("recipients/*.yaml")]
def ask(question, validator=None, default=None, set=None):
while True:
string = question + (" [{default}]".format(default=default) if default else "") + ": "
answer = input(string)
if answer == "":
if default:
answer = default
print("\033[F" + string + str(answer))
else:
continue
if validator == "float":
try:
answer = float(answer)
except ValueError:
continue
if validator == "money":
try:
answer = int(float(answer) * 100)
except ValueError:
continue
if validator == "int":
try:
answer = int(answer)
except ValueError:
continue
if validator == "date":
answer = dateparser.parse(answer).date()
if answer is None:
continue
if validator == "set":
if answer not in set:
print("only [{formats}] are allowed".format(formats=", ".join(set)))
continue
if validator == "boolean":
if answer.lower() in ['true', '1', 't', 'y', 'yes']:
return True
elif answer.lower() in ["false", "0", "f", "n", "no"]:
return False
else:
continue
return answer