-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfulgurate-import
executable file
·61 lines (47 loc) · 1.48 KB
/
fulgurate-import
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
#!/usr/bin/env python2
"""
NAME
----
fulgurate-import - convert a two-column TSV file to a flashcard file
SYNOPSIS
--------
*fulgurate-import* ['OPTIONS'] TSV-FILE [CARDS-FILE]
DESCRIPTION
-----------
Takes a tab-separated value file where the columns correspond to the first (top) and second (bottom) fields of the card respectively, and produces a cards file with the cards at initial state.
OPTIONS
-------
*-n* 'YYYY-MM-DD'::
Set the current time. Defaults to the system clock.
"""
import cards
import sys
def load_data(input):
for i, line in enumerate(input, 1):
if line.startswith('#'):
continue
parts = line.strip().split('\t')
if len(parts) != 2:
raise IOError("wrong number of records on line %i" % (i))
yield(parts)
if __name__ == "__main__":
import datetime
import getopt
import argopen
try:
opts, args = getopt.getopt(sys.argv[1:], "n:")
if len(args) < 1 or len(args) > 2:
raise getopt.GetoptError("wrong number of positional arguments")
except getopt.GetoptError:
print >> sys.stderr, "usage: %s [-n TIME] DATA-FILE [CARDS-FILE]"
sys.exit(1)
now = datetime.datetime.now()
for opt, arg in opts:
if opt == '-n':
import dateutil.parser
now = dateutil.parser.parse(arg)
src = sys.argv[1]
dest = sys.argv[2] if len(sys.argv) >= 3 else "-"
with argopen.open(src) as input:
with argopen.open(dest, 'w') as output:
cards.save(output, (cards.card(top, bot, now) for top, bot in load_data(input)))