ppf/new: Add draft implementation of TriblerLogParser.
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Thu, 1 Sep 2011 14:57:17 +0000 (17:57 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Thu, 1 Sep 2011 18:00:51 +0000 (21:00 +0300)
ppf/new/parsing.py

index 654e129..65eff8c 100644 (file)
@@ -480,14 +480,84 @@ class TriblerLogParser(SessionLogParser):
     Tribler log file parser.
     """
 
     Tribler log file parser.
     """
 
-    def __init__(self, path):
+    def __init__(self, path, start_time_string):
         super(TriblerLogParser, self).__init__(path)
 
         super(TriblerLogParser, self).__init__(path)
 
+        self.start_time = datetime.datetime.strptime(start_time_string,
+                "%Y-%m-%d %H:%M:%S")
+        self.f = open(path, 'rt')
+        self.statmsg_count = 0
+        self.message_list = []
+
+    def pop_message(self):
+        return self.message_list.pop()
+
+    def read_next_line(self):
+        self.line = self.f.readline()
+        # Remove newline character if existent.
+        self.line = re.sub(r'\n$', '', self.line)
+        self.parse_log_line(self.line)
+
+    def is_line_empty(self):
+        return self.line == ""
+
+    def get_next_message_from_line(self):
+        try:
+            msg = self.pop_message()
+            logger.debug("Popped message type %s." %(msg.__class__.__name__))
+        except IndexError, e:
+            msg = None
+
+        return msg
+
     def get_next_message(self):
     def get_next_message(self):
-        """
-        Go through Tribler log file. All log messages (verbose, status)
+        """Go through Tribler log file. All log messages (verbose, status)
         are stored in the same file.
         Return the next message available or None when all messages have
         been parsed.
         """
         are stored in the same file.
         Return the next message available or None when all messages have
         been parsed.
         """
-        return None
+        self.read_next_line()
+        while True:
+            msg = self.get_next_message_from_line()
+            if msg == None:
+                self.read_next_line()
+                if self.is_line_empty():
+                    return None
+                else:
+                    continue
+            else:
+                break
+
+        return msg
+
+    def get_log_line_type(self, line):
+        logger.debug("line is: %s" %(line))
+        return (None, None)
+
+    def parse_log_line(self, line):
+        """Parse a log line and establish its type.
+
+        Type may be status, verbose or peer status.
+        Return message in line, in case of message line, or None in case
+        of no message line."""
+
+        # Check log line type and call appropriate method.
+        (major_line_type, minor_line_type) = self.get_log_line_type(line)
+        if major_line_type == "status":
+            self.parse_status_log_line(line)
+        elif major_line_type == "peer-status":
+            self.parse_peer_status_log_line(line)
+        elif major_line_type == "verbose":
+            self.parse_verbose_log_line(line, minor_line_type)
+
+    def parse_status_log_line(self, line):
+        """Parse Tribler status line."""
+        pass
+
+    def parse_peer_status_log_line(self, line):
+        """Parse Tribler peer status line."""
+        pass
+
+    def parse_verbose_log_line(self, line, message_type):
+        """Parse Tribler verbose line."""
+        pass