Welcome to (*queue audience) "Comment...that...code" with (*queue audience) "Think...Pair...Share"

This assignment is meant to give you some more practice with digesting code and finidng your own ways to explain said code

Being able to drop into code and figure out functionality is a very useful skill (it's really a set of skills centered not only on declarative memory of syntax, but also debugging code to understand what's going on)

(game shows were this crazy pre-smarphone dominated thing that families gathered together on evenings to watch on this other crazy thing called cable television)

First Fill out the comments on your own.

You only need to fill in comments on those lines with a #, but you are free to comment where you want if you want to add more for your own learning/understanding

Second Discuss the comments with a partner. (I will let you know when it's time to discuss with a partner)

class FileEditor:
    #constants used for flags for some methods
    UNDO = 1
    REDO = 2

    def __init__(self, file_name=None):
        if( file_name != None ):
            try:
                self._curr_file = open(file_name, 'r')
            except:
                print('File name invalid!')
                return

        self._file_name = file_name
        self.__undo_ops = Stack()
        self.__redo_ops = Stack()

    def set_file(self, file_name):
        try:
            self._curr_file = open(file_name, 'r')
        except:
            print('File name invalid!')

    def close_file(self):
        try:
            self._curr_file.close()
        except:
            print('Problem closing file!')

    def _write_temp_file(self, to_line):
        '''
        Internal function that writes a temporary file up to a certain line number
         of the internal file (as specified by to_line)
        '''
        temp_file_beg = open('tempFile', 'w') # Open Temporary file to write to
        # Make sure we are at beginning of current file being edited
        self._curr_file.peek(0)

        i = 0
        while i < to_line:
            tempFile_beg.write(self._curr_file.readline())
            i += 1

        return temp_file_beg

    def edit_line(self, line_num, new_line=None, flag=None, old_line=None):


        self._curr_file.seek(0)
      #
        file_lines = self._curr_file.readlines()
      #
        if( line_num > len(file_lines) ):
            print('Line number greater than file length!')
            return
      #
        if(new_line is None):
            new_line = input('Enter new line: ')
            confirm = input('Are you sure? Y/N/Q (Yes/No/Quit)')
            #
            while( confirm != 'Y' and confirm != 'y' ):
                if( confirm == 'Q' or confirm == 'q'):
                    return
                elif( confirm == 'N' or confirm == 'n' ):
                    new_line = input('Enter new line: ')
                confirm = input('Are you sure? Y/N/Q (Yes/No/Quit)')

            new_line = new_line + "\n"

        #
        if( flag is None or flag == FileEditor.REDO):
            self.__undo_ops.push([self.edit_line, [line_num, file_lines[line_num], FileEditor.UNDO, new_line]])
        #
        elif( flag == FileEditor.UNDO ):
            self.__redo_ops.push([self.edit_line, [line_num, new_line, FileEditor.REDO, file_lines[line_num]]])

        #
        file_lines[line_num] = new_line

      #
        try:
            self._write_file(self._file_name, file_lines)
        except OSError:
            print("Problem writing file!")

        #
        while( (not (flag == FileEditor.REDO or flag == FileEditor.UNDO)) and (not self.__redo_ops.is_empty()) ):
            self.__redo_ops.pop()

    def undo(self):
        #
        if( self.__undo_ops.is_empty ):
            return

        #
        undo_op = self.__undo_ops.pop()

        #
        undo_op[0](*undo_op[1])

    def redo(self):
        if( self.__redo_ops.is_empty ):
            return

        redo_op = self.__redo_ops.pop()

        redo_op[0](*redo_op[1])

    def _write_file(self, file_name, line_list):
        #If the file we are writing is our internal file, we close the file
        # before we open it for writing
        if( file_name == self._file_name ):
            self.close_file()
        new_file = open(file_name, 'w')

        try:
            #write lines to file
            for line in line_list:
                new_file.write(line)
            print("File successfully written!")
            return( True )
        except:
            print('Problem writing lines to file!')
            return( False )
        finally:
            new_file.close()
            #reset our file to reading mode
            if( file_name == self._file_name ):
                self.set_file(file_name)