import sys, time # standard system functions
import cmd # command-line processor library
import sqlite3 # database library
class LoggingProgram(cmd.Cmd):
""" The base Cmd class gives us a command line interpreter that will
read input and dispatch do_XXX() functions based on the first word
of the input. The rest of the input line is passed to the function.
"""
log_freq = ''
log_mode = ''
prompt = 'Log >> '
insertsql = """
INSERT INTO logdata(logtime, theircall, frequency, mode, sentrpt, rcvdrpt)
VALUES (?, ?, ?, ?, ?, ?)
"""
def preloop(self):
"Open DB file and create our data table if needed"
self.db = sqlite3.connect('log.db')
self.db.execute("""
CREATE TABLE IF NOT EXISTS logdata (
logtime DATETIME,
theircall TEXT,
frequency TEXT,
mode TEXT,
sentrpt TEXT,
rcvdrpt TEXT)
""")
def do_log(self, cmdline):
"Split up the input line and write it to the database"
theircall, sent, rcvd = cmdline.split()
timestr = time.strftime('%Y-%m-%d %H:%M:%S')
self.db.execute(self.insertsql,
(timestr, theircall, self.log_freq, self.log_mode, sent, rcvd) )
self.db.commit()
print( "Logged %s at %s (%s/%s on %s using %s)"
%(theircall, timestr, sent, rcvd, self.log_freq, self.log_mode) )
def do_setmode(self, cmdline):
self.log_mode = cmdline
def do_setfreq(self, cmdline):
self.log_freq = cmdline
def do_exit(self, cmdline):
sys.exit()
def do_EOF(self, cmdline):
self.db.close()
def do_report(self, cmdline):
for row in self.db.execute("SELECT * FROM logdata ORDER BY logtime"):
print( '\t'.join(row) )
if __name__ == "__main__":
"Python runs this only when the file is executed directly"
LoggingProgram().cmdloop()