ppf/new: Add skeleton for LibtorrentLogParser.
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Mon, 22 Aug 2011 12:23:56 +0000 (15:23 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Mon, 22 Aug 2011 12:23:56 +0000 (15:23 +0300)
ppf/new/parser.py

index 9e5ae1e..17b1e28 100644 (file)
@@ -53,21 +53,94 @@ class SessionLogParser(object):
         """
         return None
 
+
 class LibtorrentLogParser(object):
     """
     libtorrent-rasterbar log folder parser.
     """
 
-    def __init__(self, path):
+    def __init__(self, path, priority=None):
+        """
+        If priority == "verbose" parse verbose log files first. Else, parse
+        status file first."
+        """
         super(LibtorrrentLogParser, self).__init__(path)
 
+        # to_parse: list of files to be parsed
+        # have_parsed: list of files that have been parsed
+        # parsing: file currently being parsed
+        self.to_parse = []
+        self.have_parsed = []
+        self.parsing = None
+
+        for entry in os.listdir(path):
+            entry_path = os.path.join(path, entry)
+            if os.path.isfile(entry_path):
+                # TODO: If entry is file and name is IP_PORT.log add it to list.
+                if True:
+                    to_parse.append(entry_path)
+
+        status_file_path = os.path.join(path, "status.log")
+        # TODO: Check if status file exists and is a file.
+
+        # List functions as a stack. First files go to the end.
+        if priority == "verbose":
+            to_parse.insert(0, status_file_path)
+        else:
+            to_parse.append(status_file_path)
+
+        open_next_file()
+
+    def open_next_file(self):
+        """
+        Open next log file from to_parse list.
+        Update have_parsed and parsing accordingly.
+        """
+        if self.parsing is None:     # first call
+            pass
+        else:
+            f.close()
+            have_parsed.append(parsing)
+
+        parsing = to_parse.pop()
+        f = open(parsing, 'r')
+
+        # TODO: Log this information somewhere for snapshotting purpose.
+        # In case an error occurs parsing would resume from that point.
+
+    def parse_status_log_line(self):
+        return None
+
+    def parse_verbose_log_line(self):
+        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 logs have
+        Return the next message available or None when all log files have
         been parsed.
         """
-        return None
+
+        while True:             # Find first message.
+            while True:             # Find first available file.
+                line = f.readline()
+                if line is not None:
+                    break
+
+                # end of file reached
+                open_next_file
+                continue
+
+            # TODO: Use a flag to distinguish between status and verbose logs.
+            msg = parse_status_log_line(line)
+            #msg = parse_verbose_log_line(line)
+
+            # Go around in case line is bogus (msg is None).
+            if msg is not None:
+                break
+
+        return msg
+
 
 class TriblerLogParser(object):
     """
@@ -75,12 +148,13 @@ class TriblerLogParser(object):
     """
 
     def __init__(self, path):
-        super(LibtorrrentLogParser, self).__init__(path)
+        super(TriblerLogParser, self).__init__(path)
 
     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 logs have
+        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.
         """
         return None