ppf/new: Refactor gget_*_message methods for LibtorrentLogParser.
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sat, 27 Aug 2011 15:49:06 +0000 (18:49 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sat, 27 Aug 2011 15:55:14 +0000 (18:55 +0300)
ppf/new/parsing.py

index 50590f3..6b5c57e 100644 (file)
@@ -142,27 +142,72 @@ class LibtorrentLogParser(SessionLogParser):
 
         self.parsing = self.to_parse.pop()
         self.f = open(self.parsing, 'r')
+        self.line = ""
+        self.message_list = []
 
         # TODO: Log this information somewhere for snapshotting purpose.
         # In case an error occurs parsing would resume from that point.
 
-    def is_status_log_line(self, line):
-        return False
+    def pop_message(self):
+        return self.message_list.pop()
 
-    def is_peer_status_log_line(self, line):
-        return False
+    def read_next_line(self):
+        self.f.readline()
+        self.parse_log_line(self.line)
 
-    def is_verbose_log_line(self, line):
-        return False
+    def is_line_empty(self):
+        return self.line == ""
+
+    def get_next_message_from_line(self):
+        try:
+            msg = self.pop_message()
+        except IndexError, e:
+            msg = None
+
+    def get_next_message_from_file(self):
+        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_next_message(self):
+        """Go through all files in libtorrent log folder and parse them.
+        Return the next message available or None when all log files have
+        been parsed.
+        """
+        while True:
+            try:
+                msg = self.get_next_message_from_file()
+                if msg == None:
+                    self.open_next_file()
+                    continue
+                else:
+                    break
+            except (ValueError, IOError, IndexError), e:
+                # In case of no more files, return None.
+                return None
+
+        return msg
+
+    def get_log_line_type(self, line):
+        return (None, None)
 
     def parse_status_log_line(self, line):
-        return None
+        pass
 
     def parse_peer_status_log_line(self, line):
-        return None
+        pass
 
-    def parse_verbose_log_line(self, line):
-        return None
+    def parse_verbose_log_line(self, line, verbose_line_type):
+        pass
 
     def parse_log_line(self, line):
         """Parse a log line and establish its type.
@@ -172,48 +217,13 @@ class LibtorrentLogParser(SessionLogParser):
         of no message line."""
 
         # Check log line type and call appropriate method.
-        if self.is_status_log_line(line):
-            return self.parse_status_log_line(line)
-        elif self.is_peer_status_log_line(line):
-            return self.parse_peer_status_log_line(line)
-        elif self.is_verbose_log_line(line):
-            return self.parse_verbose_log_line(line)
-
-        # Return None in case of unknown/non-existent message type line.
-        return None
-
-    def get_next_message(self):
-        """
-        Go through all files in libtorrent log folder and parse them.
-        Return the next message available or None when all log files have
-        been parsed.
-        """
-
-        while True:             # Find first message.
-            while True:             # Find first available file.
-                try:
-                    line = self.f.readline()
-                    if line != "":
-                        break
-
-                    logger.debug("Parse file %s.\n" %(self.parsing))
-
-                    # End of file reached, open new file.
-                    self.open_next_file()
-                except (ValueError, IOError, IndexError), e:
-                    # In case of no more files, return None.
-                    return None
-                else:
-                    continue
-
-            msg = self.parse_log_line(line)
-
-            # Go around in case line is bogus (msg is None).
-            if msg is not None:
-                break
-
-        # Caller has to distinguish between message types.
-        return msg
+        (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)
 
 
 class TriblerLogParser(SessionLogParser):