If some of this code could be made to work it could be of some use:

ParserEngine is the least important...

MessageParser is interesting.

class AbstractSectionsParser(AbstractParser):
# Provides a method to return a list of sections
def GetResult(self):
 # Returns the list with the found resources.
 pass

class AbstractParser(object):
# Must be implemented by objects that want to use the DemoFileParser.
# Provides an empty implementation for conveniance
def StartParsing(self, filename):
 # Is called before the first line is parsed.
 # "filename" Absolute path to the file being parsed
 pass

def NewLine(self, line, lineNumber):
 # Is called by the parser when it has fetched a new line
 # "line" Content of the line
 # "lineNumber" Current line number (0 index)
 pass

def HasAllInformation(self):
 # Is called before each line is read. When you return true, parsing
 # will stop prematurely.
 # returns true if this plugin does not want to  process any more lines
 return False

def StopParsing(self, lineCount):
 # Is called, when all lines have been read by the parser.
 # "lineCount" Total number of lines in the demo file.
 pass

class MessagesParser(AbstractSectionsParser):
# Parses a demo file for "Notifications"
# The list in which the parser stores the pprocessed messages
# The string we are looking for in parsed messages
# Number of open braces ("{" increments, "}" decrements)
# The number of open braces at the last encounter of "Messages"
# The line at which the last "Messages" section started
# Flag that indicates if the parser has currently entered a
# "Messages" section
# Flag that indicates if we are currently processing a Messages
# section that contains the messageSearchString
# The last visited message test
# The command identifier
# The time index of the message in seconds
# The entity ref for this message
def __init__(self, messageSearchString):
  # Creates a new MessageParser object
  # <param name="messageSearchString">The string to look for in parsed
 self._result = List[DemoSection]()
 self._braceLevel = 0
 self._lastMessagesBraceLevel = 0
 self._lastMessagesSectionStart = 0
 self._inMessagesSection = False
 self._inSearchSection = False
 self._messageText = ""
 self._command = -1
 self._time = -1
 self._entityRef = -1
 self._messageSearchString = messageSearchString

def NewLine(self, line, lineNumber):
  # Is called by the parser when it has fetched a new line
  # <param name="line">Content of the line</param>
  # <param name="lineNumber">Current line number (0 index)</param>
 if line.Trim().StartsWith("Messages"):
  self._lastMessagesSectionStart = lineNumber
  self._lastMessagesBraceLevel = self._braceLevel
  self._inMessagesSection = True
 if self._inMessagesSection and line.Trim().StartsWith("Time"):
  timeString = line.Substring(line.IndexOf("Time ") + 5)
self._time = Double.Parse(timeString, System.Globalization.CultureInfo.GetCultureInfo("en-US").NumberFormat) if self._inMessagesSection and line.Trim().StartsWith("Message ") and line.Contains(self._messageSearchString):
  self._inSearchSection = True
  self._messageText = line.Substring(line.IndexOf("Message ") + 8)
 if self._inMessagesSection and line.Trim().StartsWith("Command"):
self._command = Convert.ToInt32(line.Substring(line.IndexOf("Command ") + 8))
 if self._inMessagesSection and line.Trim().StartsWith("EntityRef"):
self._entityRef = Convert.ToInt32(line.Substring(line.IndexOf("EntityRef ") + 10)) if self._inSearchSection and line.Trim().StartsWith("}") and self._braceLevel - 1 == self._lastMessagesBraceLevel:
  self._inMessagesSection = False
  self._inSearchSection = False
  message = DemoMessage()
  message.StartLine = self._lastMessagesSectionStart
  message.EndLine = lineNumber + 1
  message.Message = self._messageText
  message.Time = self._time
  message.Command = self._command
  message.EntityRef = self._entityRef
  self._result.Add(message)
 if line.Trim().StartsWith("{"):
  self._braceLevel += 1
 if line.Trim().StartsWith("}"):
  self._braceLevel -= 1

def GetResult(self):
 # Returns the list of parsed messages
 # <returns>Returns a list of parsed messages</returns>
 return self._result


class ParserEngine(object):
def Parse(filename, plugin):
 """ <summary>
  Parses a demo file
  </summary>
  <param name="filename">The file to be parsed</param>
  <param name="plugin">The ParserPlugin to use</param>
  <returns>true if parsing was successful</returns>
 """
 # here we read the contents of the demo file line-by-line
 line = ""
 lineNumber = 0
 # only run if the file actually exists
 if File.Exists(filename):
  plugin.StartParsing(filename)
  # start reading file line by line
  file = None
  try:
   file = StreamReader(filename)
# parse as long as we don't have all information and there are still lines to read while not plugin.HasAllInformation() and (line = file.ReadLine()) != None:
    plugin.NewLine(line, lineNumber)
    lineNumber += 1
   plugin.StopParsing(lineNumber)
  except Exception, e:
   # report exception
MessageBox.Show("The demo file " + filename + " could not be parsed for meta information.\n" + "Please report the following error under \"Updates & Help\"\n" + e.Message, "Error Parsing Demo Directory", MessageBoxButtons.OK, MessageBoxIcon.Error)
  finally:
   # close file, no matter what
   if file != None:
    file.Close()
  # abort parsing, if exception occured
  if file == None:
   return False
 else:
  # report error, if file does not exist
MessageBox.Show("The demo file " + filename + " does not exist.\n" + "Please report the this error under \"Updates & Help\"\n", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
  return False
 return True

Parse = staticmethod(Parse)

def Parse(fileContents, plugin):
 """ <summary>
  Parses a demo file in memory
  </summary>
<param name="fileContents">The contents of a demo file to be parsed</param>
  <param name="plugin">The ParserPlugin to use</param>
  <returns>true if parsing was successful</returns>
 """ #, ToolStripProgressBar progressBar)
 if fileContents != None:
  # here we read the contents of the demo file line-by-line
  lineNumber = 0
  #progressBar.Maximum = fileContents.Count();
  #progressBar.Step = 1;
  enumerator = fileContents.GetEnumerator()
  plugin.StartParsing("no file")
# parse as long as we don't have all information and there are still lines to read
  while not plugin.HasAllInformation() and enumerator.MoveNext():
   plugin.NewLine(enumerator.Current, lineNumber)
   lineNumber += 1
  #progressBar.PerformStep();
  plugin.StopParsing(lineNumber)
 else:
  # report error, if file does not exist
MessageBox.Show("The demo file buffer was null.\n" + "Please report the this error under \"Updates & Help\"\n", "Demo File Buffer Null", MessageBoxButtons.OK, MessageBoxIcon.Error)
  return False
 return True

Parse = staticmethod(Parse)

Bye,
 Skybuck.

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to