Exemplo n.º 1
0
def initialize_work_log():
    '''
    Checks to make sure work_log.txt exists and has the appropriate headers
    '''
    try:
        log = open("work_log.txt")
        log.close()

    # If no work_log.txt, create file with appropriate headers.
    except FileNotFoundError:
        with open("work_log.txt", "w+") as work_log:
            work_log.write('date,task_name,time_spent,note\n')
        return True

    # If pre_existing work_log.txt uses wrong (or no) headers, exit the program
    # while prompting the user to fix the headers.
    else:
        with open("work_log.txt", "r") as work_log:
            if work_log.readline() != 'date,task_name,time_spent,note\n':
                clear_screen()
                print("Oh no! It looks like the header in work_log.txt "
                      "is not formatted properly.\n\nMake sure the first "
                      "line of work_log.txt is 'date,title,duration,note', "
                      "then try opening the program again.")
                sys.exit()
            else:
                pass
Exemplo n.º 2
0
def work_logs_program():
    main_options = {
        '1': 'Log Work',
        '2': 'Search Logs',
        '3': 'Clear All Logs',
        '4': 'Quit'
    }
    # Build the work_log.txt file if non exists
    initialize_work_log()

    # Get user's choice from the main menu options
    while True:
        clear_screen()
        print("Main Menu")
        nav = menu(main_options)

        if nav == 'Log Work':
            new_log = Log()
            new_log.create_new_log()

        if nav == 'Search Logs':
            search_log_loop()

        if nav == 'Clear All Logs':
            clear_all_logs()

        if nav == 'Quit':
            break
Exemplo n.º 3
0
 def get_all_log_fields(self):
     '''Get all log attributes'''
     clear_screen()
     self.get_log_date()
     self.get_log_task_name()
     self.get_log_time_spent()
     self.get_log_note()
Exemplo n.º 4
0
 def create_new_log(self):
     '''Get a new log from the user and write it to work_log.txt'''
     self.get_all_log_fields()
     self.add_log()
     clear_screen()
     print("New work log created:")
     self.display_log()
     input("\nHit 'Enter' to return to the Main Menu.")
Exemplo n.º 5
0
def search_log_loop():
    search_options = {
        '1': 'Search by Date',
        '2': 'Search by Date Range',
        '3': 'Search by Work Time Spent',
        '4': 'Search by Word or Phrase',
        '5': 'Search by Pattern (Regex)',
        '6': 'Return to Main Menu'
    }
    while True:

        # Fetch a list of the logs from work_log.txt.
        clear_screen()

        # Present the search menu and get the user's choice.
        print("Search Menu")
        nav = menu(search_options)
        clear_screen()

        # Break search loop if return to main menu is chosen.
        if nav == 'Return to Main Menu':
            break

        # Otherwise execute the chosen search option.
        else:
            search = Search()
            if nav == 'Search by Date':
                search.search_by_date()

            if nav == 'Search by Date Range':
                search.search_by_date_range()

            if nav == 'Search by Work Time Spent':
                search.search_by_time_spent()

            if nav == 'Search by Word or Phrase':
                search.search_by_string()

            if nav == 'Search by Pattern (Regex)':
                search.search_by_pattern()

            search.detail_view()
Exemplo n.º 6
0
def greet_user():
    '''Greet the user'''
    clear_screen()
    input("Hello, welcome to the work log program. Press any key to continue")
Exemplo n.º 7
0
def goodbye():
    '''Say goodbye when the program ends'''
    clear_screen()
    print("Thank you for using the work log program!")
Exemplo n.º 8
0
    def detail_view(self):
        '''
        Displays logs in self.search_results
        
        Displays logs one at a time, allowing the user to page through all
        results, and potentially edit or delete a certain result.
        '''
        clear_screen()
        # If the search yielded results, display the first result and set an
        # index to 0, which will be used for paging through the results.
        if self.search_results:
            index = 0
            while True:
                print(
                    "Displaying result "
                    "{} of {}".format(index + 1, len(self.search_results))
                )
                try:
                    current_result = Log(**self.search_results[index])
                    current_result.display_log()
                except TypeError:
                    input(
                        "Oh no! It looks like the data in work_logs.txt is "
                        "not formatted correctly!\nPress any key to return to "
                        "the Search Menu."
                    )
                else:
                    # Display navigation options for the search result detail
                    # view.
                    nav = input(
                        "[N]ext, [P]revious, [E]dit, [D]elete, "
                        "[R]eturn to Search Menu: "
                    ).lower()

                    # Page to next result (if any)
                    if nav == 'n' and index != len(self.search_results) - 1:
                        index += 1
                
                    # Page to previous result (if any)
                    if nav == 'p' and index != 0:
                        index -= 1
            
                    # Enter edit log dialogue.
                    if nav == 'e':
                        current_result.edit_log()
                        break
            
                    # Enter delete log dialogue.
                    if nav == 'd':
                        if confirm_user_action():
                            current_result.delete_log()
                        break
            
                    # Break search result detail view loop if [R]eturn is
                    # selected.
                    if nav == 'r':
                        break
                
                    # Keep loop going if navigation input is invalid.
                    else:
                        clear_screen()
                        continue
    
        # If no search results, announce that and leave search result detail
        # view.
        else:
            nav = input(
                "---Sorry, no results matched your search.---"
                "\nPress any key to return to the Search Menu."
            )
            pass
Exemplo n.º 9
0
    def edit_log(self):
        '''
        Prompts a user to edit chosen attributes of a log
    
        Using a dictionary of options to generate a menu, this asks whether the
        user would like to edit the date, title, duration, and/or note for a
        work log. The desired fields are retrieved, then a new log with the
        editted and original fields (if any) is appended to work_log.txt --
        while the original log is deleted from work_log.txt
        '''
        edit_options = {
            '1': 'Date',
            '2': 'Task Name',
            '3': 'Time Spent',
            '4': 'Note',
            '5': 'All Fields',
            '6': 'No Fields (Return to Search Menu)'
        }
        # Display the log the user desires to edit and present them with the
        # edit menu, getting their chosen action.
        clear_screen()
        print("Original Log:")
        self.display_log()
        print("\nWhat field(s) would you like to edit?")
        edit_field = menu(edit_options)

        # Return the user to the search menu if no edits are desired.
        if edit_field == 'No Fields (Return to Search Menu)':
            pass
        
        # User desires to edit fields in the log.
        else:
            # If edits are desired, create a copy of the original log for
            # editing. Keeping the original log instance to display to the user
            # when evaluating edits, and also to be used for deletion if the
            # edits are later confirmed.
            edited_log = Log()
            edited_log.copy_log(self)
            
            # Execute edits for chosen fields.
            if edit_field == 'Date':
                edited_log.get_log_date()

            if edit_field == 'Task Name':
                edited_log.get_log_task_name()

            if edit_field == 'Time Spent':
                edited_log.get_log_time_spent()

            if edit_field == 'Note':
                edited_log.get_log_note()

            if edit_field == 'All Fields':
                edited_log.get_all_log_fields()
                
            # Display the original and editted logs to the user.
            clear_screen()
            self.display_log()
            print("\n--- Will Be Replaced With---\n")
            edited_log.display_log()

            # Update work_log.txt if the user confirms their edits by adding
            # the editted log, and deleting the original log.
            if confirm_user_action():
                edited_log.add_log()
                self.delete_log()