#!/usr/bin/env python
import sys
-import getopt
import re
import datetime
import logging
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
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):
+ # """ 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.
+
+ 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 """
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
download_size = 0
upload_size = 0
eta = 0
- filename = ""
timestamp = None
string_array = re.split("\ *", line)
# 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":
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: