"""
Low-level class for database access: insert, update, delete,
select operations
+ Basic operations on each table in P2P logging database: swarms,
+ btclients, client_sessions, status_messages, verbose_messages
+ Insert methods have dual options:
+ insert_swarms_row - inserts a row as an array
+ insert_swarms - row fields to be added are passed as separate
+ arguments
"""
def __init__ (self, dbname):
return self.conn
def get_status(self):
+ """
+ Select rows in all tables
+ """
tables = ['swarms', 'btclients', 'client_sessions', 'status_messages', 'verbose_messages']
for t in tables:
try:
print("[status_messages]An error ocurred: ", e.args[0])
-"""
-Test case
-"""
-
def main():
+
+ """
+ Test case
+ """
+
dba = DatabaseAccess("p2p-next.db")
dba.connect()
dba.disconnect()
- """
- for t in [('1', 'mumu', '1024', 'ceva', 'URL'),
- ('2', 'gugu', '1024', 'ceva', 'URL'),
- ('3', 'gaga', '1024', 'ceva', 'URL'),
- ]:
- insert_swarms(t)
-
- for t in [('1', 'tribler', 'python', 1, 0),
- ]:
- insert_btclients(t)
-
- for t in [('1', '1', '2', 'Linux', '2.6.30', '256', '1833', '0.0.0.0', '6969', '256', '96', '123131.1231')
- ]:
- insert_client_sessions(t)
-
- tables = ['swarms', 'btclients', 'swarms', 'client_sessions', 'status_messages', 'verbose_messages']
- for t in tables:
- curs.execute("select * from '%s'" %t)
- for row in curs:
- print row
- """
if __name__ == "__main__":
sys.exit(main())
from DatabaseAccess import DatabaseAccess
class DatabaseCommander:
+ """
+ swarms and client_sessions table handling methods
+ Wrappers around DatabaseAccess class methods
+ """
+
def __init__ (self, dbname):
self.dbname = dbname
self.dba = DatabaseAccess(dbname)
client_id = self.dba.select_btclient_id_by_name(client_name)
self.dba.delete_client_sessions_by_swarm(swarm_id, client_id)
+
def usage():
print "Usage: python DatabaseTorrentSessionHandler.py action target [-i|--id id] [options] database"
print "action:"
def main():
+ """
+ Command line interface for database handling. Allows insertion,
+ deletion and selection of rows in swarms and client_sessions tables.
+ If id == -1, all rows in target table are deleted/selected.
+ Uses getopt for command line parsing.
+ Last argument must be a database file.
+ Please check README for details and running examples.
+ """
+
try:
opts, args = getopt.getopt(sys.argv[1:], "ha:l:d:s:c:i:", ["help",
"add=", "list=", "delete=", "swarm=", "client=", "id="])
else:
assert False, "unhandled option"
+ # no database file passed as argument
if len(args) != 1:
print "Error: no database file passed as argument."
sys.exit(2)