ppf/new: Updates and new tests for parsing module.
[cs-p2p-next.git] / ppf / new / parsing.py
index 3177b5c..b303129 100644 (file)
@@ -12,6 +12,7 @@ import os.path
 import re
 import datetime
 import logging
+import socket
 
 import storage      # Use *Message classes.
 
@@ -55,7 +56,7 @@ class SessionLogParser(object):
         """
         return None
 
-    def get_current_parsing_filename():
+    def get_current_parsing_filename(self):
         """Return the name of the log file being currently parsed."""
         return self.parsing
 
@@ -82,21 +83,52 @@ class LibtorrentLogParser(SessionLogParser):
         for entry in os.listdir(self.path):
             entry_path = os.path.join(self.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:
+                # If entry is file and name is IP_PORT.log add it to list.
+                if self.is_verbose_log_filename(entry):
+                    logger.debug("Add entry path %s." %(entry_path))
                     self.to_parse.append(entry_path)
 
         status_file_path = os.path.join(self.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":
-            self.to_parse.insert(0, status_file_path)
-        else:
-            self.to_parse.append(status_file_path)
+        # In case status file doesn't exist, skip it.
+        if os.access(status_file_path, os.F_OK) and \
+                os.access(status_file_path, os.R_OK):
+            logger.debug("Status file exists: %s." %(status_file_path))
+            # List functions as a stack. First files go to the end.
+            if priority == "verbose":
+                self.to_parse.insert(0, status_file_path)
+            else:
+                self.to_parse.append(status_file_path)
 
         self.open_next_file()
 
+    def get_to_parse_list(self):
+        return self.to_parse
+
+    def get_have_parsed_list(self):
+        return self.have_parsed
+
+    def is_verbose_log_filename(self, filename):
+        r = re.compile(r'^[0-9]+(?:\.[0-9]+){3}_[0-9]+\.log$')
+        if not r.match(filename):
+            return False
+
+        # Check for valid IP address and port.
+        a = re.split('_', filename)
+        ip = a[0]
+        port = int(a[1].split('.')[0])
+
+        # Check valid port.
+        if port <= 0 or port > 65535:
+            return False
+
+        # Check valid IP address.
+        try:
+            socket.inet_aton(ip)
+        except socket.error, e:
+            return False
+
+        return True
+
     def open_next_file(self):
         """
         Open next log file from to_parse list.
@@ -108,8 +140,8 @@ class LibtorrentLogParser(SessionLogParser):
             self.f.close()
             self.have_parsed.append(parsing)
 
-        parsing = self.to_parse.pop()
-        self.f = open(parsing, 'r')
+        self.parsing = self.to_parse.pop()
+        self.f = open(self.parsing, 'r')
 
         # TODO: Log this information somewhere for snapshotting purpose.
         # In case an error occurs parsing would resume from that point.