#!/usr/bin/env python
import sys
-from DatabaseWriter import DatabaseWriter
+import re
+from auto.db.DatabaseWriter import DatabaseWriter
-# TODO
+
+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 libtorrent_is_status_line(line):
+ if re.match("^ps", line) == None:
+ return False
+ return True
+
+def libtorrent_parse_status_line(line):
+
+def libtorrent_parse_status_file(database, client_sesion_id, filename):
+ dbw = DatabaseWriter(database)
+
+ try:
+ fin = open(filename, "r")
+ while 1:
+ line = fin.readline.strip()
+
+ if libtorrent_is_status_line(line) == False:
+ continue
+
+ (date, time, peer_nume, dht, download_speed, upload_speed, download_size, upload_size, eta_time) = libtorrent_parse_status_line(line)
+
+ dbw.add_status_message(client_session_id, date, time, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta_time):
+
+ except IOError:
+ print "Error processing file %s." %filename
+
+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 = 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]
+
+ libtorrent_parse_status_file(database, client_session_id, filename)
+
+if __name__ == "__main__":
+ sys.exit(main())