View difference between Paste ID: aU5gs0d0 and
SHOW: | | - or go back to the newest paste.
1-
1+
import sys, time # standard system functions
2
import cmd       # command-line processor library
3
import sqlite3   # database library
4
5
class LoggingProgram(cmd.Cmd):
6
    """ The base Cmd class gives us a command line interpreter that will
7
        read input and dispatch do_XXX() functions based on the first word
8
        of the input. The rest of the input line is passed to the function.
9
    """
10
    log_freq = ''
11
    log_mode = ''
12
    prompt = 'Log >> '
13
    
14
    insertsql = """
15
        INSERT INTO logdata(logtime, theircall, frequency, mode, sentrpt, rcvdrpt)
16
        VALUES (?, ?, ?, ?, ?, ?)
17
        """
18
        
19
    def preloop(self):
20
        "Open DB file and create our data table if needed"
21
        self.db = sqlite3.connect('log.db')
22
        self.db.execute("""
23
            CREATE TABLE IF NOT EXISTS logdata (
24
               logtime    DATETIME,
25
               theircall  TEXT,
26
               frequency  TEXT,
27
               mode       TEXT,
28
               sentrpt    TEXT,
29
               rcvdrpt    TEXT)
30
            """)
31
    
32
    def do_log(self, cmdline):
33
        "Split up the input line and write it to the database"
34
        theircall, sent, rcvd = cmdline.split()
35
        timestr = time.strftime('%Y-%m-%d %H:%M:%S')
36
        self.db.execute(self.insertsql,
37
            (timestr, theircall, self.log_freq, self.log_mode, sent, rcvd) )
38
        self.db.commit()
39
        print( "Logged %s at %s (%s/%s on %s using %s)"
40
               %(theircall, timestr, sent, rcvd, self.log_freq, self.log_mode) )
41
        
42
    def do_setmode(self, cmdline):
43
        self.log_mode = cmdline
44
45
    def do_setfreq(self, cmdline):
46
        self.log_freq = cmdline
47
48
    def do_exit(self, cmdline):
49
        sys.exit()
50
       
51
    def do_EOF(self, cmdline):
52
        self.db.close()
53
        
54
    def do_report(self, cmdline):
55
        for row in self.db.execute("SELECT * FROM logdata ORDER BY logtime"):
56
            print( '\t'.join(row) )
57
58
59
if __name__ == "__main__":
60
    "Python runs this only when the file is executed directly"
61
    LoggingProgram().cmdloop()