def main(): database = file_management.load_data( ) # If json successfully loaded, use that. Else default to base template. # Update backup right after loading (to save state before user performs any actions) file_management.update(database, 'data_autobackup.json') console_display.print_display(database) while True: user_input = input().lower() # Lower for string comparisons if not user_input: # It's possible to enter nothing continue if not user_input.isascii(): print('Please only use ASCII characters') continue user_input = user_input.split( ) # Split into a list of space-separated terms commands.alias_format(user_input) command = user_input[0] print() # Newline to separate input from printing try: # Try to get corresponding function from commands.py # Command functions have '_command' appended to name command_function = getattr(commands, command + '_command') except AttributeError: # If function is not found print('Invalid command') print() # Newline to separate input from printing continue command_function(database, user_input)
def mode_route(database, user_input): input_length = len(user_input) if not input_length >= 2 and input_length < 4: # These commands always take 2-4 inputs print('Invalid number of parameters, expected 2-4', end='\n\n') return command = user_input[0] # To know which dict we're working with mode = user_input[1] valid_modes = documentation.get_modes( command) # Returns set of valid modes for given dict command if mode in valid_modes: mode_function = getattr(item_management, mode + '_mode') # ie 'add' goes to add_mode() else: print('Invalid mode', end='\n\n') return dictionary = database[command] input_info = { 'command': command, # Some containers need different routing within a mode function 'mode': mode, 'parameter_length': len( user_input[2:] ) # For these commands, 'parameters' = inputs following command/mode } if not mode_function( database, dictionary, input_info, * user_input[2:]): # Expand input parameters into arguments return # Save, sort, and print display dict_management.sort_dictionary(database, command) file_management.update(database) console_display.print_display(database) print_mode_success(mode)
def change_all_daily_dicts(database, mode): daily_dictionary_names = documentation.get_daily_dictionary_names() daily_dict_item_length = 0 for dict_name in daily_dictionary_names: daily_dict_item_length += len(name_to_container(database, dict_name)) if daily_dict_item_length == 0: print('There are no active daily objectives', end='\n\n') return # Get confirmation while True: if mode == 'complete': # If 'complete', print 'complete', else print '0%' for 'reset' print( 'Set all daily, optional, and active cycle objectives to complete? (y/n)', end='\n\n') elif mode == 'reset': print( 'Set all daily, optional, and active cycle objectives to 0%? (y/n)', end='\n\n') user_input = input().lower() if user_input == 'y': break elif user_input == 'n': return if mode == 'complete': for dict_name in daily_dictionary_names: # Function expects a dictionary with {database, command, dictionary, parameters} (command = dict name) items = { 'database': database, 'dictionary': name_to_container(database, dict_name), 'command': dict_name, 'parameters': ['complete'] } item_management.setall_mode(items) elif mode == 'reset': for dict_name in daily_dictionary_names: # Function expects a dictionary with {command, dictionary, parameters} (command = dict name) items = { 'database': database, 'dictionary': name_to_container(database, dict_name), 'command': dict_name, 'parameters': ['reset'] } item_management.setall_mode(items) for dict_name in daily_dictionary_names: sort_dictionary(database, dict_name) file_management.update(database) console_display.print_display(database) print('Dictionary successfully updated', end='\n\n')
def delete_dictionary(database, mode): def get_confirmation(): while True: if mode == 'all': print( f"Are you sure you'd like to delete ALL objectives" f" ({total_objectives_to_remove})? (y/n)", end='\n\n') else: print( f"Are you sure you'd like to delete ALL {mode} items" f" ({total_objectives_to_remove})? (y/n)", end='\n\n') user_response = input().lower() if user_response == 'y': return True elif user_response == 'n': return False if mode == 'all': dict_list = documentation.get_dictionary_list(database) total_objectives_to_remove = 0 for dict_name in dict_list: total_objectives_to_remove += len(dict_name) if not total_objectives_to_remove: # If there are none print('There are no objectives to delete', end='\n\n') return False if not get_confirmation(): console_display.print_display(database) return False for dictionary in dict_list: dictionary.clear() return True else: # Specified dictionary dictionary = name_to_container(database, mode) total_objectives_to_remove = len(dictionary) if not total_objectives_to_remove: # If there are none print('That container has no items', end='\n\n') return False if not get_confirmation(): console_display.print_display(database) return False dictionary.clear() return True
def delete_command(database, user_input): # ex input: delete daily # ex input: delete all if dict_management.wrong_parameter_count(len(user_input), 2): return delete_mode_input = user_input[1] if delete_mode_input not in documentation.get_dictionary_names( ) and delete_mode_input != 'all': print( "Invalid delete mode. Expected a dictionary name (ie daily) or 'all'", end='\n\n') return if not dict_management.delete_dictionary(database, delete_mode_input): return file_management.update(database) console_display.print_display(database) print('Successfully deleted the specified', end='\n\n')
def setdate_command(database, user_input): # ex input: setdate 5 26 if dict_management.wrong_parameter_count(len(user_input), 3): return input_month = user_input[1] input_day = user_input[2] if not input_month.isnumeric(): print('Invalid month. Use numbers in the form MM DD, ie 5 27', end='\n\n') return if not input_day.isnumeric(): print('Invalid day. Use numbers in the form MM DD, ie 5 27', end='\n\n') return input_month = int(input_month) input_day = int(input_day) calendar_date = database['settings']['calendar_date'] if input_month == calendar_date['month'] and input_day == calendar_date[ 'day']: print('That date is already set', end='\n\n') return if input_month > 12 or input_month < 1: print('Invalid input. Expected 1-12 range for month', end='\n\n') return if input_day > 31 or input_day < 1: print('Invalid input. Expected 1-31 range for day', end='\n\n') return if not date_logic.valid_date(input_month, input_day): print('Invalid day for that month', end='\n\n') return if not settings_management.force_date_change_prompt( database): # If they confirm; streak reset, cycles deleted console_display.print_display(database) return database['settings']['calendar_date']['month'] = input_month database['settings']['calendar_date']['day'] = input_day file_management.update(database) console_display.print_display(database) print('Date successfully changed', end='\n\n')
def note_command(database, user_input): if not len(user_input) > 1: print('Invalid number of parameters, expected at least 2', end='\n\n') return mode = user_input[1] valid_modes = documentation.get_modes('note') if mode in valid_modes: mode_function = getattr( item_management, mode + '_note_mode') # ie 'add' goes to add_note_mode() else: print('Invalid mode', end='\n\n') return if not mode_function(database, user_input): return # Save and print display file_management.update(database) console_display.print_display(database) print_mode_success(mode)
def setday_command(database, user_input): # ex input: setday monday if dict_management.wrong_parameter_count(len(user_input), 2): return input_week_day = user_input[1] if input_week_day not in date_logic.get_week_days(): print('Invalid day. Enter week day name (ie saturday)', end='\n\n') return if not settings_management.force_date_change_prompt( database): # If they confirm; streak reset, cycles deleted console_display.print_display(database) return week_day_number = date_logic.convert_day( input_week_day) # Convert to #, ie Sunday = 1 database['settings']['calendar_date']['week_day'] = week_day_number file_management.update(database) console_display.print_display(database) print('Week day successfully changed', end='\n\n')
def print_command(database, user_input): if dict_management.wrong_parameter_count(len(user_input), 1): return console_display.print_display(database)