def readChartFile(self, file_handle): ''' Here we do the heavy lifting of parsing the chart file. Should only be called from the constructor, and takes a file handle as argument. ''' # Read the file into memory. all_lines = file_handle.readlines() # Now create an itteratible array so we know which line we're on. line_numbers = range(len(all_lines)) # Create a line. current_line = Line() # Variable for tracking data lines. data_line = 0 # Now read the lines. for i in line_numbers: # Ignore comments. if all_lines[i].startswith('#'): continue # Ignore blank lines. elif all_lines[i].isspace(): continue # When we find ! set that line in the pages array. elif all_lines[i].startswith('!'): self.addPage(data_line) continue # If we find a Font, set the line font. elif all_lines[i].upper().startswith('FONT'): line = all_lines[i] key, value = line.split('=') current_line.setFont(value.strip()) continue # If we find a Linesize spec., set line spacing. elif all_lines[i].upper().startswith('LINESIZE'): line = all_lines[i] key, value = line.split('=') current_line.setLineSpacing(value.strip()) continue # If we find a ColumnSize spec., set the column spacing. elif all_lines[i].upper().startswith('COLUMNSIZES'): line = all_lines[i] key, value = line.split('=') current_line.setColumnSizes(value) continue # If we have a data line... elif all_lines[i].upper().startswith('20'): line = all_lines[i] # Increment the number of data lines. data_line += 1 # Set the default character to 0. default_chr_pos = 0 current_line.setDefaultCharacter(default_chr_pos) # Check for multiple columns. line_sections = line.split('|') for each_section in line_sections: ratio, text = each_section.split(':') ratio = ratio.strip() text = text.strip() # Check for default character markers. for chr in text: if chr == '~': text = text.replace('~', '', 1) # Sanity check for position. if default_chr_pos >= len(line): default_chr_pos = len(line) - 1 current_line.setDefaultCharacter(default_chr_pos) break else: default_chr_pos += 1 # Strip out any quote marks. if text.startswith('"') or text.startswith("'"): text = text.strip('"\'') # Check for blank lines - insert a space in that case. if text == '': text = ' ' # Add a section to the current line. a_section = Section(ratio, text) current_line.addSection(a_section) # Done parsing line. Now add it to the chart. # Note we do NOT creat a new line, as this would clear the spacing # and font and those are only cleared if specified again or a new # chart file is loaded. BUT we DO clear the line sections - the # text and ratios or else we continue appending and get all sections # is all the lines. self.addLine(current_line) current_line.clearSections() continue else: print "I don't know how to handle this line: \n#%i, %s" %(i, all_lines[i]) continue return True