-
Notifications
You must be signed in to change notification settings - Fork 3
/
inputMerge.py
97 lines (83 loc) · 3.15 KB
/
inputMerge.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
import sys
import json
import importlib
import os
import click
sys.path.append('anki')
from anki import Collection as aopen
START = 'start'
END_OF_DICT = '----'
def initAnkiModule(data, collection, card_type):
if bool(collection) == False or "deck" not in data:
print('Collection or deck is not found!')
return None
deck = aopen(collection)
deckId = deck.decks.id(data['deck'])
deck.decks.select(deckId)
model = deck.models.byName(card_type.GetCardType(deck.models))
if model is not None:
model['did'] = deckId
deck.models.save(model)
deck.models.setCurrent(model)
return deck
def handleProfile(data, collection, download_dir, inputFilePath):
print('deck file:{}'.format(data['deck']))
print('dict_source :{}'.format(data['dict_source']))
print('card_type:{}'.format(data['card_type'] if 'card_type' in data else 'Basic'))
if not os.path.exists(inputFilePath):
print("No input file, Exit")
return False
input_file = "{}/{}".format(os.getcwd(), inputFilePath)
card_type = data['card_type'] if 'card_type' in data else 'basic'
dict_source = importlib.import_module('module.{}'.format(data['dict_source'].lower()))
dict_source_str = '[{}]'.format(data['dict_source'].lower())
card_type = importlib.import_module('cardtype.{}'.format(card_type))
deck = initAnkiModule(data, collection, card_type)
stat = None
with open(input_file , encoding='utf-8') as word_list:
for word in word_list:
word = word.splitlines()[0]
if stat == START and word != END_OF_DICT:
result = dict_source.LookUp(word, data, download_dir)
if result is None:
continue
elif result is False:
deck.save()
deck.close()
return False
card_data = card_type.MakeCard(result)
if 0 == len(card_data):
continue
card = deck.newNote()
for key in card_data:
card[key] = card_data[key]
try:
deck.addNote(card)
except(Exception, e):
if hasattr(e, "data"):
sys.exit("ERROR: Cound not add {}:{}", e.data["field"], e.data['type'])
else:
sys.exit(e)
elif stat == START and word == END_OF_DICT:
stat = None
elif stat == None:
if word == dict_source_str:
stat = START
deck.save()
deck.close()
def load_config(path):
with open(path, encoding='utf-8') as data_file:
return json.load(data_file)
if '__main__':
if len(sys.argv) >= 2:
config_path = sys.argv[1]
else:
config_path = 'inputMergeConfig.json'
data = load_config(config_path)
inputFilePath = data['input']
collection = data['collection']
download_dir = data['download_dir']
for profile in data['profiles']:
if handleProfile(profile, collection, download_dir, inputFilePath) == False:
break