SHARE
TWEET
Untitled
a guest
Aug 2nd, 2012
129
Never
- # Adrian VELICU
- # adi.velicu@gmail.com
- #
- # No warranty. Use at own risk. Database backup recommended.
- import sqlite3
- import sys
- import argparse
- def critical(message):
- print message
- sys.exit()
- parser = argparse.ArgumentParser(description='Adds Heisig information to a special field in an Anki deck.')
- parser.add_argument('target', action='store',
- help='The target deck to modify.')
- parser.add_argument('heisig', action='store',
- help='The Heisig deck to look up Kanji info in.')
- parser.add_argument('--model', action='store', dest='model', default=None,
- help='The model within TARGET to operate on (eg. Japanese). You only need to specify this if you want to use a field named identically within two different models as the Vocab or Output field.')
- parser.add_argument('--vocab-field', action='store', dest='vocabfield', default='Vocabulary-Kanji',
- help='The field within TARGET to find kanji in (eg. Vocabulary-Kanji).')
- parser.add_argument('--output-field', action='store', dest='outputfield', default='Kanji-Info',
- help='The field within TARGET to write kanji info in (eg. Kanji-Info).')
- parser.add_argument('--kanji-field', action='store', dest='kanjifield', default='Kanji',
- help='The field within HEISIG which stores the Kanji (eg. Kanji). ')
- parser.add_argument('--keyword-field', action='store', dest='keywordfield', default='Keyword',
- help='The field within HEISIG which stores the keyword (eg. Keyword). ')
- args = parser.parse_args()
- targetdb = sqlite3.connect(args.target)
- targetcur = targetdb.cursor()
- heisigdb = sqlite3.connect(args.heisig)
- heisigcur = heisigdb.cursor()
- model_id = None
- if args.model is not None:
- targetcur.execute("SELECT id FROM models WHERE name=?", (args.model,))
- model_id = targetcur.fetchone()
- if model_id is None:
- critical("No such model: %s" % args.model)
- else:
- model_id = model_id[0]
- if model_id is not None:
- targetcur.execute("SELECT id FROM fieldModels WHERE name=? AND model_id=?", (args.vocabfield, model_id))
- else:
- targetcur.execute("SELECT id FROM fieldModels WHERE name=?", (args.vocabfield,))
- vocab_field_id = targetcur.fetchone()
- if vocab_field_id is None:
- critical("No such field: %s" % args.vocabfield)
- else:
- vocab_field_id = vocab_field_id[0]
- if model_id is not None:
- targetcur.execute("SELECT id FROM fieldModels WHERE name=? AND model_id=?", (args.outputfield, model_id))
- else:
- targetcur.execute("SELECT id FROM fieldModels WHERE name=?", (args.outputfield,))
- output_field_id = targetcur.fetchone()
- if output_field_id is None:
- critical("No such field: %s" % args.outputfield)
- else:
- output_field_id = output_field_id[0]
- heisigcur.execute("SELECT id FROM fieldModels WHERE name=?", (args.kanjifield,))
- kanji_field_id = heisigcur.fetchone()
- if kanji_field_id is None:
- critical("No such field: %s" % args.kanjifield)
- else:
- kanji_field_id = kanji_field_id[0]
- heisigcur.execute("SELECT id FROM fieldModels WHERE name=?", (args.keywordfield,))
- keyword_field_id = heisigcur.fetchone()
- if keyword_field_id is None:
- critical("No such field: %s" % args.keywordfield)
- else:
- keyword_field_id = keyword_field_id[0]
- print "Creating Kanji map... "
- heisigcur.execute("""SELECT kj.value, kw.value
- FROM fields kj INNER JOIN fields kw ON (kj.factId = kw.factId)
- WHERE kj.fieldModelId=? AND kw.fieldModelId=?""", (kanji_field_id, keyword_field_id))
- rows = heisigcur.fetchall()
- kanji = {}
- for row in rows:
- kanji[row[0]]=row[1]
- #print "vocab_field_id = %s, output_field_id = %s, keyword_field_id = %s, kanji_field_id = %s" % (vocab_field_id, output_field_id, keyword_field_id, kanji_field_id)
- print "Determining facts that need to be updated... "
- targetcur.execute("SELECT factId, value FROM fields WHERE fieldModelId=?", (vocab_field_id,))
- rows = targetcur.fetchall()
- pending_updates = []
- for row in rows:
- id = row[0]
- txt = row[1]
- to_append = ""
- for x in txt:
- if x in kanji.keys():
- to_append += "%s: %s<br />" % (x, kanji[x])
- pending_updates.append((id, to_append))
- cntnew = 0
- cntupd = 0
- print "Applying updates... "
- for update in pending_updates:
- targetcur.execute("SELECT id, value FROM fields WHERE fieldModelId=? AND factId=?", (output_field_id, update[0]))
- fnd = targetcur.fetchone()
- if fnd is None:
- cntnew+=1
- raise Exception("Should never have to add fields.")
- #print "[+] %s: %s" % (update[0], update[1])
- # print "INSERT INTO fields (factId, fieldModelId, ordinal, value) VALUES (%s, %s, %s, %s)" % (update[0], output_field_id, 0, update[1])
- targetcur.execute("INSERT INTO fields (factId, fieldModelId, ordinal, value) VALUES (?, ?, ?, ?)", (update[0], output_field_id, 0, update[1]))
- else:
- factid = fnd[0]
- cntupd+=1
- #print "[~] %s: %s" % (factid, update[1])
- # print "UPDATE fields SET value=%s WHERE id=%s" % (update[1], update[0])
- targetcur.execute("UPDATE fields SET value=? WHERE id=?", (update[1], factid))
- print "%s inserts, %s updates. Committing..." % (cntnew, cntupd)
- targetdb.commit()
RAW Paste Data
