def get_datetime(prompt, validate=None): print(prompt) in_dt = None while in_dt is None: in_str = input("> ").strip() try: in_dt = parse_datetime(in_str, tz=session.config['TIMEZONE']) except ValueError as error: print("Error: {err!s}".format(err=error)) return in_dt
def get_task(selection=None, ask_details=True): """ Asks the user for a task from a given selection or a new one. Keyword arguments: session -- the user session object selection -- a selection of tasks to choose from (default: anything) ask_details -- whether to ask the user for details, such as deadline and estimated time """ # Check that the selection is satisfiable. if selection is not None and not selection: raise ValueError("Cannot ask for a task from an empty selection.") task = None print("What is the task? ") if selection is not None: restricted = True else: selection = session.tasks restricted = False str2task = dict() int2task = dict() for task_index, a_task in enumerate(selection): # FIXME Distinguish between different tasks with the same string # representation. task_str = str(a_task) str2task[task_str] = a_task int2task[str(task_index)] = a_task if restricted: Cli.choosefrom(int2task) shown_selection = False taskname = input("> ") while not taskname or taskname == "?": if restricted: Cli.choosefrom(int2task) else: if selection: shown_selection = True Cli.choosefrom(int2task, msg="You can choose from the existing " "tasks:") else: print("There are currently no tasks defined.") taskname = input("> ") # Should the task be one of existing Task tasks, if restricted: # Find the Task task. while taskname not in str2task and taskname not in int2task: if not taskname or taskname == "?": Cli.choosefrom(int2task) else: print("Sorry, this task was not on the menu. Try again.") taskname = input("> ") if taskname in int2task: task = int2task[taskname] else: task = str2task[taskname] # If we are asking for a new task, else: if shown_selection: if taskname in int2task: task = int2task[taskname] elif taskname in str2task: task = str2task[taskname] if task is None: # Create a new task, asking for optional details. project = Cli.get_project( prompt="What project does it belong to?") task = Task(taskname, project) if ask_details: print("Estimated time?") time = input("> ").strip() print("Deadline?") deadline = input("> ").strip() if time: task.time = parse_timedelta(time) if deadline: task.deadline = parse_datetime( deadline, tz=session.config['TIMEZONE']) return task