summaryrefslogtreecommitdiffstats
path: root/flashcards
blob: caf51d8466f0a9a38a830906772ec518b80d6b06 (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
#!/usr/bin/python3

import re
import sys
from pathlib import Path
from random import randint

cardRegex = "CARD: "
prefixLength = 6

# Returns a list of Path objects, containing the path to each valid file provided
def getFileList():
    fileList = []
    if len(sys.argv) > 1:
        for f in sys.argv[1:]:
            path = Path(f)
            if path.exists() and path.is_file():
                fileList.append(f)
        return fileList
    else:
        print("Missing arguments")
        sys.exit()

# Returns cards in the form [(front, back)]
def createCardList(files):
    cards = []
    for f in files:
        cards = cards + extractCards(f)
    return cards

# Extracts cards from a single file into a list of the form [(front, back)]
def extractCards(f):
    front = ""
    back = ""
    cards = []
    with open(f) as cardFile:
        for l in cardFile:
            match = re.match(cardRegex, l)
            if match:
                if front != "":
                    cards.append([front, back])
                    back = ""
                front = match.string[prefixLength:]
            else:
                back += l
    return cards

# Loops serving cards to the user until the program is exited
def serveCards(cards):
    while True:
        card = cards[randint(0, len(cards) - 1)]
        print(card[0])
        input()
        print(card[1])
        input()

def debugCards(cardList):
    for c in cardList:
        print("Front:", c[0])
        print("Back:", c[1])

def main():
    files = getFileList()
    cards = createCardList(files)
    serveCards(cards)

if __name__ == "__main__":
    main()