parameters: (self)
return_statement: return
docstring: Check and order (if necessary) record file
          by value of specified log by title
docstring_summary: Check and order (if necessary) record file
          by value of specified log by title
docstring_tokens: Check and order ( if necessary ) record file by value of specified log by title
function: def _orderRecordFile(self):
          """ Check and order (if necessary) record file
          by value of specified log by title
          """
          self.log().debug("Order Record File!")
  
          # Read line
          lfile = open(self._logfilename, "r")
          lines = lfile.readlines()
          lfile.close()
  
          # Create dictionary for the log value
          titlelines = []
          linedict = {}
          ilog = self._headerTitles.index(self.titleToOrder)
  
          totnumlines = 0
          for line in lines:
              cline = line.strip()
              if len(cline) == 0:
                  continue
  
              if cline.startswith(self._headerTitles[0]) is True or cline.startswith('#'):
                  # title line or comment line
                  titlelines.append(line)
              else:
                  # value line
                  try:
                      keyvalue = line.split(self._valuesep)[ilog].strip()
                  except IndexError:
                      self.log().error("Order record failed.")
                      return
                  if (keyvalue in linedict) is False:
                      linedict[keyvalue] = []
                  linedict[keyvalue].append(line)
                  totnumlines += 1
              # ENDIFELSE
          # ENDFOR
  
          # Check needs to re-order
          # This test does not work with python 3, you can not assume the order of a dictionary
          # if list(linedict.keys()) != sorted(linedict.keys()):
          if True: # temporary hack to get it working with python 3, always write a new file!
              # Re-write file
              wbuf = ""
  
              # title line
              for line in titlelines:
                  wbuf += line
  
              # log entry line
              numlines = 0
  
              # consider the mode to remove duplicate
              if self._removeDupRecord is True:
                  totnumlines = len(linedict.keys())
  
              for ivalue in sorted(linedict.keys()):
                  if self._removeDupRecord is True:
                      # If duplicated records is to be removed, only consider the last record
                      line = linedict[ivalue][-1]
                      wbuf += line
                      if numlines != totnumlines-1 and wbuf[-1] != '\n':
                          wbuf += '\n'
                      numlines += 1
  
                  else:
                      # Consider all!
                      for line in linedict[ivalue]:
                          wbuf += line
                          # Add extra \n in case reordered
                          if numlines != totnumlines-1 and wbuf[-1] != '\n':
                              wbuf += '\n'
                          numlines += 1
                      # ENDFOR
                  # ENDFOR
              # ENDFOR
  
              # Last line should not ends with \n
              if wbuf[-1] == '\n':
                  wbuf = wbuf[0:-1]
  
              # Remove unsupported character which may cause importing error of GNUMERIC
              wbuf = wbuf.replace(chr(0),"")
  
              # Re-write file
              ofile = open(self._logfilename, "w")
              ofile.write(wbuf)
              ofile.close()
  
          # ENDIF
  
          return
function_tokens: def _orderRecordFile ( self ) : self . log ( ) . debug ( "Order Record File!" ) # Read line lfile = open ( self . _logfilename , "r" ) lines = lfile . readlines ( ) lfile . close ( ) # Create dictionary for the log value titlelines = [ ] linedict = { } ilog = self . _headerTitles . index ( self . titleToOrder ) totnumlines = 0 for line in lines : cline = line . strip ( ) if len ( cline ) == 0 : continue if cline . startswith ( self . _headerTitles [ 0 ] ) is True or cline . startswith ( '#' ) : # title line or comment line titlelines . append ( line ) else : # value line try : keyvalue = line . split ( self . _valuesep ) [ ilog ] . strip ( ) except IndexError : self . log ( ) . error ( "Order record failed." ) return if ( keyvalue in linedict ) is False : linedict [ keyvalue ] = [ ] linedict [ keyvalue ] . append ( line ) totnumlines += 1 # ENDIFELSE # ENDFOR # Check needs to re-order # This test does not work with python 3, you can not assume the order of a dictionary # if list(linedict.keys()) != sorted(linedict.keys()): if True : # temporary hack to get it working with python 3, always write a new file! # Re-write file wbuf = "" # title line for line in titlelines : wbuf += line # log entry line numlines = 0 # consider the mode to remove duplicate if self . _removeDupRecord is True : totnumlines = len ( linedict . keys ( ) ) for ivalue in sorted ( linedict . keys ( ) ) : if self . _removeDupRecord is True : # If duplicated records is to be removed, only consider the last record line = linedict [ ivalue ] [ - 1 ] wbuf += line if numlines != totnumlines - 1 and wbuf [ - 1 ] != '\n' : wbuf += '\n' numlines += 1 else : # Consider all! for line in linedict [ ivalue ] : wbuf += line # Add extra \n in case reordered if numlines != totnumlines - 1 and wbuf [ - 1 ] != '\n' : wbuf += '\n' numlines += 1 # ENDFOR # ENDFOR # ENDFOR # Last line should not ends with \n if wbuf [ - 1 ] == '\n' : wbuf = wbuf [ 0 : - 1 ] # Remove unsupported character which may cause importing error of GNUMERIC wbuf = wbuf . replace ( chr ( 0 ) , "" ) # Re-write file ofile = open ( self . _logfilename , "w" ) ofile . write ( wbuf ) ofile . close ( ) # ENDIF return