summaryrefslogtreecommitdiff
path: root/jacsr
blob: 9b960b99985c1257d0924fb5e64a385d101fb7bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python3
import csv
import sqlite3
import sys

# TODO: Parameterize with getopt. These should be fine defaults.
RESULT_FILE = './cstimer.csv'
DATABASE_FILE = 'solves.db'


def create_solve_table(connection):
    cursor = connection.cursor()
    # TODO: Not sure what to do about DNF (Did Not Finish) solves
    statement = """
    CREATE TABLE solves (
        number integer primary key autoincrement,
        time datetime,
        scramble text
    )
    """
    cursor.execute(statement)
    cursor.close()


def insert_times_into_db(connection, result_file):
    cursor = connection.cursor()

    with open(result_file) as f:
        csv_file = csv.DictReader(f, delimiter=';')

        for line in csv_file:
            # TODO: Add date, comment, etc. fields from CsTimer csv
            cursor.execute("INSERT into solves (time, scramble) VALUES (?, ?)",
                           (line['Time'], line['Scramble']))

    cursor.close()


def read_times_from_database(connection):
    cursor = connection.cursor()

    rows = cursor.execute('SELECT * from solves').fetchall()
    print(rows)

    cursor.close()


def usage():
    print("""
jacsr - Jackson's Awesome Cube Statistics Recorder
-h - show this help message
-s - Setup database with the tables
    """)


if __name__ == "__main__":
    setup_db = False

    # TODO: Use getopt instead
    if len(sys.argv) > 1:
        if sys.argv[1] == '-s':
            setup_db = True
        else:
            usage()
            sys.exit()

    connection = sqlite3.connect(DATABASE_FILE)

    if setup_db:
        create_solve_table(connection)

    insert_times_into_db(connection, RESULT_FILE)

    read_times_from_database(connection)

    connection.commit()
    connection.close()