log-parser: send filename as argument to class contructor
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 23 Apr 2010 15:37:15 +0000 (18:37 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 23 Apr 2010 15:37:15 +0000 (18:37 +0300)
ppf/log-parser/generic/GenericStatusParser.py
ppf/log-parser/generic/LibtorrentStatusParser.py
ppf/log-parser/generic/TriblerStatusParser.py

index 4037a97..b89bd60 100644 (file)
@@ -17,8 +17,8 @@ class GenericStatusParser:
     Inherited by client-specific classes
     """
 
-    def __init__(self):
-        pass
+    def __init__(self, filename):
+        self.filename = filename
 
     # return boolean
     #
@@ -81,9 +81,9 @@ class GenericStatusParser:
 
         return (num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta)
 
-    def parse_last_status_line(self, filename,line):
+    def parse_last_status_line(self, line):
         # read last line from status log file
-        f = open(filename, "r")
+        f = open(self.filename, "r")
         f.seek(-1, os.SEEK_END)
 
         # seek before the beginning of the last line
@@ -100,10 +100,9 @@ class GenericStatusParser:
             eta_seconds):
         print "ps = %d, dht = %d, ds = %d kb/s, us = %d kb/s, dsize = %d bytes, usize = %d bytes" % (num_peers, dht, download_speed, upload_speed, download_size, upload_size)
 
-    def parse_status_file(self, filename, callback_func, callback_arg = None):
-
+    def parse(self, callback_func, callback_arg = None):
         try:
-            fin = open(filename, "r")
+            fin = open(self.filename, "r")
             while 1:
                 line = fin.readline()
                 if not line:
@@ -130,4 +129,4 @@ class GenericStatusParser:
                             eta_seconds)
 
         except IOError:
-            logging.error("Error processing file %s." % filename)
+            logging.error("Error processing file %s." % self.filename)
index b97fbdd..bc84044 100644 (file)
@@ -1,16 +1,12 @@
 #!/usr/bin/env python
 
 import sys
-import getopt
 import re
-from DatabaseWriter import DatabaseWriter
-from DatabaseCommander import DatabaseCommander
-from GenericStatusParser import GenericStatusParser
-import julian
 import datetime
-
 import logging
 
+from GenericStatusParser import GenericStatusParser
+
 # configure logging (change to logging.ERROR when no DEBUG required)
 logging.basicConfig(level=logging.ERROR)
 
@@ -20,8 +16,8 @@ class LibtorrentStatusParser(GenericStatusParser):
     Inherited by client-specific classes
     """
 
-    def __init__(self):
-        pass
+    def __init__(self, filename):
+        GenericStatusParser.__init__(self, filename)
 
     # return boolean
     #
@@ -79,7 +75,7 @@ class LibtorrentStatusParser(GenericStatusParser):
 
         return datetime.timedelta(eta[0], eta[3], 0, 0, eta[2], eta[1], 0)
 
-    # return list of required 
+    # return list of required information
     def parse_status_line(self, line):
         num_peers = 0
         dht = 0
@@ -113,114 +109,14 @@ class LibtorrentStatusParser(GenericStatusParser):
 
         return (num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta_seconds)
 
-def db_write(dbw, client_session_id, message_time,
-                            num_peers, dht,
-                            download_speed, upload_speed,
-                            download_size, upload_size,
-                            eta_seconds):
-    pass
-
-def usage():
-    print "Usage: python StatusParser.py -i|--id id status_file"
-    print "id:"
-    print "\t--id"
-    print "\t-i\t\tclient_session_id"
-    print "\tstatus_file:"
-    print "\t--file"
-    print "\t-f\t\tstatus_file for libtorrent"
-    print "\tdatabase\t\tSQLite database file"
-    print "\t--help"
-    print "\t-h\t\t\tprint this help screen"
 
 def main():
-    try:
-        opts, args = getopt.getopt(sys.argv[1:], "hi:f:", ["help",
-            "id=", "file="])
-    except getopt.GetoptError, err:
-        print str(err)
-        usage()
-        sys.exit(2)
-
-    client_session_id = None
-    filename = None
-    database = None
-
-    for o, a in opts:
-        if o in ("-h", "--help"):
-            usage()
-            sys.exit(0)
-        elif o in ("-i", "--id"):
-            client_session_id = int(a)
-        elif o in ("-f", "--file"):
-            filename = a
-        else:
-            assert False, "unhandled option"
-
-    if client_session_id == None:
-        print "Error: no client session id."
-        sys.exit(2)
-
-    if filename == None:
-        print "Error: no status file."
-        sys.exit(2)
-
-    # no database passed as argument
-    if len(args) != 1:
-        print "Error: no database file passed as argument."
-        sys.exit(2)
-    database = args[0]
-
-    dbc = DatabaseCommander(database)
-
-    # check for client_session_id, swarm_id, btclient_id
-    cursor = dbc.select_client_sessions_by_id(client_session_id)
-    if cursor == None:
-        print "Error: no client session id (%d) in database." % client_session_id
-        sys.exit(2)
-    for session_row in cursor:
-        pass
-
-    swarm_id = session_row[1]
-    btclient_id = session_row[2]
-
-    cursor = dbc.select_swarms(swarm_id)
-    if cursor == None:
-        print "Error: no swarm id (%d) in database." % swarm_id
-        sys.exit(2)
-    for swarm_row in cursor:
-        pass
-
-    cursor = dbc.select_btclients(btclient_id)
-    if cursor == None:
-        print "Error: no client id (%d) in database." % btclient_id
-        sys.exit(2)
-    for btclient_row in cursor:
-        pass
-
-    print "Client session row is: "
-    print "    ", session_row
-    print "Swarm row is: "
-    print "    ", swarm_row
-    print "Client row is: "
-    print "    ", btclient_row
-
-    print "\nContinue parsing on file %s? (y/n) " % filename,
-    try:
-        ans = sys.stdin.readline().strip()
-        if ans != "y":
-            sys.exit(0)
-    except IOError:
-        print "Error reading standard input."
-        sys.exit(2)
-    print ""
-
-    session_start = julian.julianToDatetime(session_row[11])
-
-    # parse status file
-    dbw = DatabaseWriter(database)
-    sp = LibtorrentStatusParser()
-    sp.parse_status_file(client_session_id, session_start, filename, db_write, dbw)
+    if len(sys.argv) != 2:
+        print "Usage: %s filename" % (sys.argv[0])
+        sys.exit(1)
 
+    sp = LibtorrentStatusParser(sys.argv[1])
+    sp.parse(sp.cb_print)
 
 if __name__ == "__main__":
     sys.exit(main())
index 3e1749d..932c52d 100644 (file)
@@ -3,14 +3,10 @@
 import sys
 import getopt
 import re
-#from DatabaseWriter import DatabaseWriter
-#from DatabaseCommander import DatabaseCommander
-from GenericStatusParser import GenericStatusParser
-import julian
 import datetime
-
 import logging
 
+from GenericStatusParser import GenericStatusParser
 
 class TriblerStatusParser(GenericStatusParser):
     """
@@ -18,11 +14,8 @@ class TriblerStatusParser(GenericStatusParser):
     @author Adriana Draghici <adriana.draghici@cti.pub.ro>
     """
 
-    files_sizes = {} # dictionary: key - filename, value - filesize
-    # configure logging (change to logging.ERROR when no DEBUG required)
-    logging.basicConfig(level=logging.DEBUG)
-    def __init__(self):
-        pass
+    def __init__(self, filename):
+        GenericStatusParser.__init__(self, filename)
 
     def is_status_line(self, line):
         """  Check if status line. All status messages contain a 
@@ -41,18 +34,18 @@ class TriblerStatusParser(GenericStatusParser):
         if line.find("SingleDownload") == -1:
             return False
         return True
-    
-    def get_file_size(self, line):
-        """ Parse a line with this format: 
-            SingleDownload: save_as( u'<filename>' <size_in_bytes> '<download_folder>' <is_dir> )
-            Saves the file name and size. If the line does not correspond to this format, it does nothing.
-        """
-        index = -1
-        parts = []
-        if line.find("save_as") != -1:
-            parts = line.split("'")
-            self.files_sizes[parts[1]] =  int(parts[2]) # saves the filename and its size in bytes
-    
+
+   # def get_file_size(self, line):
+   #     """ Parse a line with this format: 
+   #         SingleDownload: save_as( u'<filename>' <size_in_bytes> '<download_folder>' <is_dir> )
+   #         Saves the file name and size. If the line does not correspond to this format, it does nothing.
+   #     """
+   #     index = -1
+   #     parts = []
+   #     if line.find("save_as") != -1:
+   #         parts = line.split("'")
+   #         self.files_sizes[parts[1]] =  int(parts[2]) # saves the filename and its size in bytes
+
     def canon_num_peers(self, non_canon_value):
         """  @return integer """
         return int(non_canon_value)
@@ -69,13 +62,14 @@ class TriblerStatusParser(GenericStatusParser):
         """@return integer, eg. 12119.51kb/s -> 12119 """
         return int(float(non_canon_value.strip("KB/s")))
 
-    def canon_download_size(self, non_canon_value, filename):
+    def canon_download_size(self, non_canon_value):
         """@return integer, eg.  25% -> 25*file_size/100"""
-        return int(float(non_canon_value.strip("%")) * self.files_sizes[filename] / 100)
+#        return int(float(non_canon_value.strip("%")) * self.files_sizes[filename] / 100)
+        return 0
 
     def canon_upload_size(self, non_canon_value):
-        pass
-   
+        return 0
+
     def canon_eta(self, non_canon_value):
         """@return integer, eg. 26.456787 -> 26 (seconds)"""
         if non_canon_value != 'None':
@@ -87,14 +81,14 @@ class TriblerStatusParser(GenericStatusParser):
             Format: dd-mm-yyyy hh:mm:ss
             @return datetime object
         """
-        
+
         date_array = date.split("-");
         time_array = time.split(":");
         if len(date_array) != 3 or len(time_array) != 3:
             return None
-        
+
         timestamp = datetime.datetime(int(date_array[2]), int(date_array[1]), int(date_array[0]), #year, month, day
-                                        int(time_array[0]), int(time_array[1]), int(time_array[2])) #hour, min, sec
+                int(time_array[0]), int(time_array[1]), int(time_array[2])) #hour, min, sec
         return timestamp
 
     # return list of required 
@@ -113,7 +107,7 @@ class TriblerStatusParser(GenericStatusParser):
         filename = ""
         timestamp = None
         string_array = re.split("\ *", line)
-        
+
         logging.debug("string_array is: " + str(string_array))
         if len(string_array) != 14:
             logging.error("Invalid line format!")
@@ -121,10 +115,10 @@ class TriblerStatusParser(GenericStatusParser):
 
         # get timestamp and transform it in datetime format
         timestamp= self.parse_timestamp(string_array[0], string_array[1])
-        
+
         filename = string_array[2]
-        
-        i = 3 
+
+        i = 3
         while i < len(string_array): #string_array:
             if string_array[i] == "peers":
                 num_peers = self.canon_num_peers(string_array[i+1])
@@ -139,7 +133,7 @@ class TriblerStatusParser(GenericStatusParser):
                 i = i + 2
                 continue
             if string_array[i] == "DLSTATUS_DOWNLOADING" or string_array[i] == "DLSTATUS_SEEDING":
-                download_size = self.canon_download_size(string_array[i+1], filename)
+                download_size = self.canon_download_size(string_array[i+1])
                 i = i + 2
                 continue
             if string_array[i] == "eta":
@@ -147,11 +141,11 @@ class TriblerStatusParser(GenericStatusParser):
                 i = i + 2
                 continue
             i = i + 1
-        return (timestamp, num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta)
+        return (num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta)
 
-    def parse_status_file(self, client_session_id, session_start, filename, callback_func, callback_arg = None):
+    def parse_status_file2(self, callback_func, callback_arg = None):
         try:
-            fin = open(filename, "r")
+            fin = open(self.filename, "r")
             while 1:
                 line = fin.readline()
                 if not line:
@@ -164,143 +158,33 @@ class TriblerStatusParser(GenericStatusParser):
                 if self.is_status_line(line) == False:
                     continue
 
-                (message_time, num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta_seconds) = self.parse_status_line(line)
-                print "lista", (message_time, num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta_seconds)
-                logging.debug("(%s, %d, %d,%d kb/s, %d kb/s, %d bytes, %d bytes)" % (message_time, num_peers, eta_seconds, 
-                                                                                    download_speed, upload_speed, 
-                                                                                    download_size, upload_size))
+                (num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta_seconds) = self.parse_status_line(line)
+                logging.debug("(%d, %d, %d kb/s, %d kb/s, %d bytes, %d bytes)" % (num_peers, eta_seconds,
+                    download_speed, upload_speed,
+                    download_size, upload_size))
                 if callback_arg == None:
-                    """callback_func(client_session_id, message_time,
-                            num_peers, dht,
+                    callback_func(num_peers, dht,
                             download_speed, upload_speed,
                             download_size, upload_size,
                             eta_seconds)
-                    """
                     pass
                 else:
-                    callback_func(callback_arg, client_session_id, message_time,
-                            num_peers, dht,
+                    callback_func(num_peers, dht,
                             download_speed, upload_speed,
                             download_size, upload_size,
                             eta_seconds)
 
         except IOError:
-            logger.error("Error processing file %s." %filename)
-
-def db_write(dbw, client_session_id, message_time,
-                            num_peers, dht,
-                            download_speed, upload_speed,
-                            download_size, upload_size,
-                            eta_seconds):
-    pass
+            logger.error("Error processing file %s." % (self.filename))
 
-def usage():
-    print "Usage: python TriblerStatusParser.py -i|--id id status_file"
-    print "id:"
-    print "\t--id"
-    print "\t-i\t\tclient_session_id"
-    print "\tstatus_file:"
-    print "\t--file"
-    print "\t-f\t\tstatus_file for tribler"
-    print "\tdatabase\t\tSQLite database file"
-    print "\t--help"
-    print "\t-h\t\t\tprint this help screen"
 
 def main():
-    try:
-        opts, args = getopt.getopt(sys.argv[1:], "hi:f:", ["help",
-            "id=", "file="])
-    except getopt.GetoptError, err:
-        print str(err)
-        usage()
-        sys.exit(2)
-
-    client_session_id = None
-    filename = None
-    database = None
-
-    for o, a in opts:
-        if o in ("-h", "--help"):
-            usage()
-            sys.exit(0)
-        elif o in ("-i", "--id"):
-            client_session_id = int(a)
-        elif o in ("-f", "--file"):
-            filename = a
-        else:
-            assert False, "unhandled option"
-
-    if client_session_id == None:
-        print "Error: no client session id."
-        sys.exit(2)
-
-    if filename == None:
-        print "Error: no status file."
-        sys.exit(2)
-
-    # no database passed as argument
-    if len(args) != 1:
-        print "Error: no database file passed as argument."
-        sys.exit(2)
-    database = args[0]
-
-    dbc = DatabaseCommander(database)
-
-    # check for client_session_id, swarm_id, btclient_id
-    cursor = dbc.select_client_sessions_by_id(client_session_id)
-    if cursor == None:
-        print "Error: no client session id (%d) in database." % client_session_id
-        sys.exit(2)
-    for session_row in cursor:
-        pass
-
-    swarm_id = session_row[1]
-    btclient_id = session_row[2]
-
-    cursor = dbc.select_swarms(swarm_id)
-    if cursor == None:
-        print "Error: no swarm id (%d) in database." % swarm_id
-        sys.exit(2)
-    for swarm_row in cursor:
-        pass
-
-    cursor = dbc.select_btclients(btclient_id)
-    if cursor == None:
-        print "Error: no client id (%d) in database." % btclient_id
-        sys.exit(2)
-    for btclient_row in cursor:
-        pass
-
-    print "Client session row is: "
-    print "    ", session_row
-    print "Swarm row is: "
-    print "    ", swarm_row
-    print "Client row is: "
-    print "    ", btclient_row
-
-    print "\nContinue parsing on file %s? (y/n) " % filename,
-    try:
-        ans = sys.stdin.readline().strip()
-        if ans != "y":
-            sys.exit(0)
-    except IOError:
-        print "Error reading standard input."
-        sys.exit(2)
-    print ""
-
-    session_start = julian.julianToDatetime(session_row[11])
-
-    # parse status file
-    dbw = DatabaseWriter(database)
-    sp = TriblerStatusParser()
-    sp.parse_status_file(client_session_id, session_start, filename, db_write, dbw)
-
+    if len(sys.argv) != 2:
+        print "Usage: %s filename" % (sys.argv[0])
+        sys.exit(1)
 
-def main_just_parse():
-    filename = sys.argv[1]
-    client_session_id = 1
-    sp = TriblerStatusParser()
-    sp.parse_status_file(client_session_id, None, filename, None, None)
+    sp = TriblerStatusParser(sys.argv[1])
+    sp.parse(sp.cb_print)
 
 if __name__ == "__main__":
-    sys.exit(main_just_parse())
+    sys.exit(main())