Next / Previous / Contents / TCC Help System / NM Tech homepage

14.7. PickList.__readColorsFile(): Process the rgb.txt file

The purpose of this method is to attempt to read the rgb.txt file and use it to set up the list of standard colors.

huey
# - - -   P i c k L i s t . _ _ r e a d C o l o r s F i l e

    def __readColorsFile ( self, inFile ):
        """Try to read the file of standard colors.

          [ inFile is a readable file ->
              if inFile is a valid colors file ->
                self.__colorMap  :=  as invariant from that file
              else ->
                self.__colorMap  :=  an dictionary ]
        """

The format of the colors file is fairly simple:

The color files aren't huge, so we use .readlines() to return the contents of the file as a list of strings. This makes it easier to number the lines in the next step.

huey
        #-- 1 --
        # [ lineList  :=  the lines from inFile as a list of strings ]
        lineList  =  inFile.readlines()

Later steps need to know the original order of the colors, so we use the index in lineList as the induction variable of the loop to process the lines. If even so much as one line is not valid, we clear self.__colorMap and return. See Section 14.8, “PickList.__readColorLine(): Process one line from rgb.txt.

huey
        #-- 2 --
        # [ if all the non-comment lines in inFile are valid ->
        #     self.__colorMap  :=  as invariant from those lines
        #   else ->
        #     self.__colorMap  :=  an empty list ]
        try:
            for  index in range ( len ( lineList ) ):
                #-- 2 body --
                # [ if lineList[index] is a comment -> I
                #   else if lineList[index] is a valid color line ->
                #     self.__colorMap  +:=  an entry mapping the
                #         uppercased color name |-> a tuple (index,
                #         the color from that line as a Color instance),
                #         the name from that line)
                #   else ->
                #     self.__colorMap  :=  {}
                #     return ]
                self.__readColorLine ( index, lineList[index] )
        except IOError:
            self.__colorMap  =  {}