2018-03-04 21:04:54 +01:00
|
|
|
import os
|
2018-03-13 22:03:13 +01:00
|
|
|
from glob import glob
|
2018-03-04 21:04:54 +01:00
|
|
|
|
2018-03-13 22:03:13 +01:00
|
|
|
import dateparser
|
2018-03-04 21:04:54 +01:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
def load_yaml(filename):
|
|
|
|
with open(filename, 'r') as stream:
|
|
|
|
return yaml.load(stream)
|
|
|
|
|
|
|
|
|
2018-03-13 22:03:13 +01:00
|
|
|
def save_yaml(data, filename):
|
|
|
|
with open(filename, 'w') as outfile:
|
|
|
|
yaml.dump(data, outfile, default_flow_style=False)
|
|
|
|
|
|
|
|
|
2018-03-04 21:04:54 +01:00
|
|
|
def remove_tmp_files(base):
|
|
|
|
for ext in ["aux", "log"]:
|
|
|
|
os.remove(base + "." + ext)
|
2018-03-13 22:03:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
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(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
|
|
|
|
return answer
|