Next / Previous / Contents / Shipman's homepage

26. Scan.nextLine(): Advance to the next line

logscan.py
# - - -   S c a n . n e x t L i n e

    def nextLine(self):
        '''Advance to the next line, if there is one.
        '''
        #-- 1 --
        if self.atEndFile:
            return False
        else:
            self.pos = 0

Next we use the file's .readline() method to try to read the next line. This method returns zero at the end of the stream.

logscan.py
        #-- 2 --
        # [ if self.file has at least one line remaining ->
        #     self.file  :=  self.file advanced past the next line
        #     self.rawLine  :=  next line from self.file
        #   else ->
        #     self.rawLine  :=  '' ]
        self.rawLine = self.file.readline()

        #-- 3 --
        if len(self.rawLine) == 0:
            self.atEndFile = 1
            return False
        else:
            self.lineNo += 1
            self.__echoed = False

If there is a trailing newline, we remove it. Then, we set self.line to self.rawLine, with the comment removed if there is one.

logscan.py
        #-- 4 --
        # [ if self.rawLine ends with a newline ->
        #     self.rawLine  :=  self.rawLine without that newline
        #   else -> I ]
        if(( len(self.rawLine) > 0) and
            (self.rawLine[-1] == '\n')):
            self.rawLine = self.rawLine[:-1]

        #-- 5 --
        # [ if (self.commentPrefix is not None) and
        #   (self.commentPrefix matches anywhere on self.line) ->
        #     self.line  :=  self.line truncated at the start of
        #                    the first match
        #   else ->
        #     self.line  :=  self.rawLine ]
        self.line = self.rawLine
        if self.commentPrefix:
            commentPos = self.rawLine.find(self.commentPrefix)
            if commentPos >= 0:
                self.line = self.rawLine[:commentPos]

        #-- 6 --
        return True