Prompt if user wants to create an undefined label, instead of auto-creating

pull/15/head
Hoffelhas 2020-06-07 16:20:55 +02:00
parent fa3d7fa1dd
commit 50830eaaa8
1 changed files with 46 additions and 6 deletions

View File

@ -82,6 +82,38 @@ def main():
'Error trying to sync with Todoist API: %s' % str(e)) 'Error trying to sync with Todoist API: %s' % str(e))
quit() quit()
def query_yes_no(question, default="yes"):
# """Ask a yes/no question via raw_input() and return their answer.
# "question" is a string that is presented to the user.
# "default" is the presumed answer if the user just hits <Enter>.
# It must be "yes" (the default), "no" or None (meaning
# an answer is required of the user).
# The "answer" return value is True for "yes" or False for "no".
# """
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
def initialise(args): def initialise(args):
# Check we have a API key # Check we have a API key
@ -138,13 +170,21 @@ def main():
# Create a new label in Todoist # Create a new label in Todoist
#TODO: #TODO:
logging.info( logging.info(
"\n\nLabel '{}' doesn't exist in your Todoist. A new label is now created for you.\n".format(args.label)) "\n\nLabel '{}' doesn't exist in your Todoist\n".format(args.label))
# sys.exit(1) # sys.exit(1)
response = query_yes_no('Do you want to automatically create this label?')
if response:
api.labels.add(args.label) api.labels.add(args.label)
api.commit() api.commit()
api.sync() api.sync()
labels = api.labels.all(lambda x: x['name'] == args.label) labels = api.labels.all(lambda x: x['name'] == args.label)
label_id = labels[0]['id'] label_id = labels[0]['id']
logging.info('Label {} has been created!'.format(args.label))
else:
logging.info('Exiting Autodoist.')
exit(1)
else: else:
# Label functionality not needed # Label functionality not needed
label_id = None label_id = None