aboutsummaryrefslogtreecommitdiff
path: root/modules/todo/todo.py
blob: 89a2b78411a69ddf8ebd18e9fd3f08ef3f347bee (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
import sqlite3

import lib


def init_todo_db(conn: sqlite3.Connection):
    c = conn.cursor()
    c.execute(
        """
        CREATE TABLE IF NOT EXISTS todos (
          id INTEGER PRIMARY KEY,
          text VARCHAR(1024)
        );
        """)
    conn.commit()


def add_todo(todo):
    conn = sqlite3.connect('todos.db')
    init_todo_db(conn)
    c = conn.cursor()
    c.execute(
        """
        INSERT INTO todos (text) VALUES (?);
        """, [todo])
    conn.commit()


def read_todos() -> sqlite3.Cursor:
    conn = sqlite3.connect('todos.db')
    init_todo_db(conn)
    c = conn.cursor()
    return c.execute(
        """
        SELECT id, text FROM todos LIMIT 50;
        """)


@lib.match_text('(?i)^TODO: (?P<todos>.*)$')
def handle_todo(ctx: lib.MatchContext):
    for todo in ctx.named_groups['todos'].split(','):
        todo = todo.strip()
        add_todo(todo)
        ctx.respond(todo + ' added')


@lib.name('todos')
def list_todos(ctx: lib.CommandContext):
    mes = 'Todos: \n'
    for row in read_todos():
        mes += '%d - %s\n' % row
    ctx.respond(mes)