<s> import sqlite3 <EOL> import sys <EOL> import base64 <EOL> from random import sample <EOL> from string import digits , ascii_letters <EOL> from utils import Utils <EOL> class MyDB ( ) : <EOL> def __init__ ( self , sqlite_file ) : <EOL> self . sqlite_file = sqlite_file + "<STR_LIT>" <EOL> self . conn = None <EOL> if ( not self . checkDB ( ) ) : <EOL> self . initDB ( ) <EOL> def getCursor ( self ) : <EOL> if ( self . conn is None ) : <EOL> try : <EOL> self . conn = sqlite3 . connect ( self . sqlite_file ) <EOL> except sqlite3 . OperationalError as e : <EOL> print e <EOL> except : <EOL> print sys . exc_info ( ) [ <NUM_LIT:0> ] <EOL> return self . conn . cursor ( ) <EOL> def checkDB ( self ) : <EOL> try : <EOL> cursor = self . getCursor ( ) <EOL> except : <EOL> print sys . exc_info ( ) [ <NUM_LIT:0> ] <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> if cursor . fetchone ( ) is None : <EOL> return False <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> if cursor . fetchone ( ) is None : <EOL> return False <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> if cursor . fetchone ( ) is None : <EOL> return False <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> if cursor . fetchone ( ) is None : <EOL> return False <EOL> return True <EOL> def initDB ( self ) : <EOL> cursor = self . getCursor ( ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> self . conn . commit ( ) <EOL> return <EOL> def addUser ( self , user ) : <EOL> cursor = self . getCursor ( ) <EOL> cursor . execute ( '<STR_LIT>' , ( user , "<STR_LIT>" . join ( sample ( digits + ascii_letters , <NUM_LIT:8> ) ) ) ) <EOL> self . conn . commit ( ) <EOL> return <EOL> def addUsers ( self , users ) : <EOL> for user in users : <EOL> self . addUser ( user ) <EOL> return <EOL> def addHost ( self , name , ip = "<STR_LIT>" ) : <EOL> cursor = self . getCursor ( ) <EOL> cursor . execute ( '<STR_LIT>' , ( name , ip , ) ) <EOL> self . conn . commit ( ) <EOL> return <EOL> def addHosts ( self , hosts ) : <EOL> for host in hosts : <EOL> self . addHost ( host ) <EOL> return <EOL> def addPort ( self , port , host ) : <EOL> cursor = self . getCursor ( ) <EOL> cursor . execute ( '<STR_LIT>' , ( port , host , ) ) <EOL> self . conn . commit ( ) <EOL> return <EOL> def addWebTemplate ( self , ttype , src_url , tdir ) : <EOL> cursor = self . getCursor ( ) <EOL> cursor . execute ( '<STR_LIT>' , ( ttype , src_url , tdir , ) ) <EOL> self . conn . commit ( ) <EOL> return <EOL> def clearWebTemplates ( self ) : <EOL> cursor = self . getCursor ( ) <EOL> cursor . execute ( '<STR_LIT>' ) <EOL> self . conn . commit ( ) <EOL> return <EOL> def getUsers ( self ) : <EOL> users = [ ] <EOL> cursor = self . getCursor ( ) <EOL> cursor . execute ( '<STR_LIT>' ) <EOL> for row in cursor . fetchall ( ) : <EOL> users . append ( row [ <NUM_LIT:0> ] ) <EOL> return Utils . unique_list ( users ) <EOL> def findUser ( self , trackid ) : <EOL> user = "<STR_LIT>" <EOL> cursor = self . getCursor ( ) <EOL> cursor . execute ( '<STR_LIT>' , ( trackid , ) ) <EOL> for row in cursor . fetchall ( ) : <EOL> user = row [ <NUM_LIT:0> ] <EOL> return user <EOL> def getUserTrackId ( self , user ) : <EOL> trackid = "<STR_LIT>" <EOL> cursor = self . getCursor ( ) <EOL> print user <EOL> cursor . execute ( '<STR_LIT>' , ( user , ) ) <EOL> for row in cursor . fetchall ( ) : <EOL> trackid = row [ <NUM_LIT:0> ] <EOL> return trackid <EOL> def getWebTemplates ( self , ttype = "<STR_LIT>" ) : <EOL> templates = [ ] <EOL> cursor = self . getCursor ( ) <EOL> cursor . execute ( '<STR_LIT>' , ( ttype , ) ) <EOL> for row in cursor . fetchall ( ) : <EOL> templates . append ( str ( row [ <NUM_LIT:1> ] ) + "<STR_LIT>" + str ( row [ <NUM_LIT:0> ] ) ) <EOL> return Utils . unique_list ( templates ) </s>
