From: Razvan Deaconescu Date: Fri, 23 Apr 2010 16:03:51 +0000 (+0300) Subject: log-parser: reworked version of TriblerStatusParser; uses parse() function call in... X-Git-Tag: getopt_long~51 X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=commitdiff_plain;h=6bcc28c40001eb775c476f30b3a7efae3c6d1378;p=cs-p2p-next.git log-parser: reworked version of TriblerStatusParser; uses parse() function call in GenericStatusParser --- diff --git a/ppf/log-parser/generic/GenericStatusParser.py b/ppf/log-parser/generic/GenericStatusParser.py index b89bd60..5100f7e 100644 --- a/ppf/log-parser/generic/GenericStatusParser.py +++ b/ppf/log-parser/generic/GenericStatusParser.py @@ -1,11 +1,8 @@ #!/usr/bin/env python import sys -import getopt import re -import julian import datetime - import logging # configure logging (change to logging.ERROR when no DEBUG required) diff --git a/ppf/log-parser/generic/TriblerStatusParser.py b/ppf/log-parser/generic/TriblerStatusParser.py index 932c52d..be8732c 100644 --- a/ppf/log-parser/generic/TriblerStatusParser.py +++ b/ppf/log-parser/generic/TriblerStatusParser.py @@ -1,7 +1,6 @@ #!/usr/bin/env python import sys -import getopt import re import datetime import logging @@ -16,6 +15,7 @@ class TriblerStatusParser(GenericStatusParser): def __init__(self, filename): GenericStatusParser.__init__(self, filename) + self.filesize = self.get_file_size() def is_status_line(self, line): """ Check if status line. All status messages contain a @@ -35,16 +35,30 @@ class TriblerStatusParser(GenericStatusParser): return False return True - # def get_file_size(self, line): - # """ Parse a line with this format: - # SingleDownload: save_as( u'' '' ) - # 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): + # """ Parse a line with this format: + # SingleDownload: save_as( u'' '' ) + # Saves the file name and size. If the line does not correspond to this format, it does nothing. + + try: + fin = open(self.filename, "r") + while 1: + line = fin.readline() + if not line: + break + + line = line.strip() + if self.is_single_download_line(line) == True: + if line.find("save_as") != -1: + parts = line.split("'") + return int(parts[2]) + break + fin.close() + except IOError: + logger.error("Error processing file %s." % (self.filename)) + return -1 + + return 0 def canon_num_peers(self, non_canon_value): """ @return integer """ @@ -64,8 +78,7 @@ class TriblerStatusParser(GenericStatusParser): 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 0 + return int(float(non_canon_value.strip("%")) * self.filesize / 100) def canon_upload_size(self, non_canon_value): return 0 @@ -104,7 +117,6 @@ class TriblerStatusParser(GenericStatusParser): download_size = 0 upload_size = 0 eta = 0 - filename = "" timestamp = None string_array = re.split("\ *", line) @@ -116,8 +128,6 @@ 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 while i < len(string_array): #string_array: if string_array[i] == "peers": @@ -143,40 +153,6 @@ class TriblerStatusParser(GenericStatusParser): i = i + 1 return (num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta) - def parse_status_file2(self, callback_func, callback_arg = None): - try: - fin = open(self.filename, "r") - while 1: - line = fin.readline() - if not line: - break - - line = line.strip() - if self.is_single_download_line(line) == True: - self.get_file_size(line) - - if self.is_status_line(line) == False: - continue - - (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(num_peers, dht, - download_speed, upload_speed, - download_size, upload_size, - eta_seconds) - pass - else: - callback_func(num_peers, dht, - download_speed, upload_speed, - download_size, upload_size, - eta_seconds) - - except IOError: - logger.error("Error processing file %s." % (self.filename)) - def main(): if len(sys.argv) != 2: