SHARE
TWEET

Untitled

a guest Aug 2nd, 2012 129 Never
  1. # Adrian VELICU
  2. # adi.velicu@gmail.com
  3. #
  4. # No warranty. Use at own risk. Database backup recommended.
  5.  
  6.  
  7. import sqlite3
  8. import sys
  9. import argparse
  10.  
  11. def critical(message):
  12.         print message
  13.         sys.exit()
  14.  
  15. parser = argparse.ArgumentParser(description='Adds Heisig information to a special field in an Anki deck.')
  16. parser.add_argument('target', action='store',
  17.                                         help='The target deck to modify.')
  18. parser.add_argument('heisig', action='store',
  19.                                         help='The Heisig deck to look up Kanji info in.')
  20.  
  21. parser.add_argument('--model', action='store', dest='model', default=None,
  22.                                         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.')
  23. parser.add_argument('--vocab-field', action='store', dest='vocabfield', default='Vocabulary-Kanji',
  24.                                         help='The field within TARGET to find kanji in (eg. Vocabulary-Kanji).')
  25. parser.add_argument('--output-field', action='store', dest='outputfield', default='Kanji-Info',
  26.                                         help='The field within TARGET to write kanji info in (eg. Kanji-Info).')
  27. parser.add_argument('--kanji-field', action='store', dest='kanjifield', default='Kanji',
  28.                                         help='The field within HEISIG which stores the Kanji (eg. Kanji). ')
  29. parser.add_argument('--keyword-field', action='store', dest='keywordfield', default='Keyword',
  30.                                         help='The field within HEISIG which stores the keyword (eg. Keyword). ')
  31.  
  32. args = parser.parse_args()
  33.  
  34.  
  35. targetdb = sqlite3.connect(args.target)
  36. targetcur = targetdb.cursor()
  37. heisigdb = sqlite3.connect(args.heisig)
  38. heisigcur = heisigdb.cursor()
  39.  
  40. model_id = None
  41. if args.model is not None:
  42.         targetcur.execute("SELECT id FROM models WHERE name=?", (args.model,))
  43.         model_id = targetcur.fetchone()
  44.         if model_id is None:
  45.                 critical("No such model: %s" % args.model)
  46.         else:
  47.                 model_id = model_id[0]
  48.  
  49. if model_id is not None:
  50.         targetcur.execute("SELECT id FROM fieldModels WHERE name=? AND model_id=?", (args.vocabfield, model_id))
  51. else:
  52.         targetcur.execute("SELECT id FROM fieldModels WHERE name=?", (args.vocabfield,))
  53.  
  54. vocab_field_id = targetcur.fetchone()
  55. if vocab_field_id is None:
  56.         critical("No such field: %s" % args.vocabfield)
  57. else:
  58.         vocab_field_id = vocab_field_id[0]
  59.  
  60. if model_id is not None:
  61.         targetcur.execute("SELECT id FROM fieldModels WHERE name=? AND model_id=?", (args.outputfield, model_id))
  62. else:
  63.         targetcur.execute("SELECT id FROM fieldModels WHERE name=?", (args.outputfield,))
  64.  
  65. output_field_id = targetcur.fetchone()
  66. if output_field_id is None:
  67.         critical("No such field: %s" % args.outputfield)
  68. else:
  69.         output_field_id = output_field_id[0]
  70.  
  71. heisigcur.execute("SELECT id FROM fieldModels WHERE name=?", (args.kanjifield,))
  72. kanji_field_id = heisigcur.fetchone()
  73. if kanji_field_id is None:
  74.         critical("No such field: %s" % args.kanjifield)
  75. else:
  76.         kanji_field_id = kanji_field_id[0]
  77.  
  78. heisigcur.execute("SELECT id FROM fieldModels WHERE name=?", (args.keywordfield,))
  79. keyword_field_id = heisigcur.fetchone()
  80. if keyword_field_id is None:
  81.         critical("No such field: %s" % args.keywordfield)
  82. else:
  83.         keyword_field_id = keyword_field_id[0]
  84.  
  85. print "Creating Kanji map... "
  86.  
  87.  
  88. heisigcur.execute("""SELECT kj.value, kw.value
  89.                 FROM fields kj INNER JOIN fields kw ON (kj.factId = kw.factId)
  90.                 WHERE kj.fieldModelId=? AND kw.fieldModelId=?""", (kanji_field_id, keyword_field_id))
  91. rows = heisigcur.fetchall()
  92. kanji = {}
  93. for row in rows:
  94.         kanji[row[0]]=row[1]
  95.  
  96. #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)
  97.  
  98. print "Determining facts that need to be updated... "
  99.  
  100. targetcur.execute("SELECT factId, value FROM fields WHERE fieldModelId=?", (vocab_field_id,))
  101. rows = targetcur.fetchall()
  102. pending_updates = []
  103. for row in rows:
  104.         id = row[0]
  105.         txt = row[1]
  106.         to_append = ""
  107.         for x in txt:
  108.                 if x in kanji.keys():
  109.                         to_append += "%s: %s<br />" % (x, kanji[x])
  110.         pending_updates.append((id, to_append))
  111.  
  112. cntnew = 0
  113. cntupd = 0
  114.  
  115. print "Applying updates... "
  116.  
  117. for update in pending_updates:
  118.         targetcur.execute("SELECT id, value FROM fields WHERE fieldModelId=? AND factId=?", (output_field_id, update[0]))
  119.         fnd = targetcur.fetchone()
  120.         if fnd is None:
  121.                 cntnew+=1
  122.                 raise Exception("Should never have to add fields.")
  123.                 #print "[+] %s: %s" % (update[0], update[1])
  124. #               print "INSERT INTO fields (factId, fieldModelId, ordinal, value) VALUES (%s, %s, %s, %s)" % (update[0], output_field_id, 0, update[1])
  125.                 targetcur.execute("INSERT INTO fields (factId, fieldModelId, ordinal, value) VALUES (?, ?, ?, ?)", (update[0], output_field_id, 0, update[1]))
  126.         else:
  127.                 factid = fnd[0]
  128.                 cntupd+=1
  129.                 #print "[~] %s: %s" % (factid, update[1])
  130. #               print "UPDATE fields SET value=%s WHERE id=%s" % (update[1], update[0])
  131.                 targetcur.execute("UPDATE fields SET value=? WHERE id=?", (update[1], factid))
  132. print "%s inserts, %s updates. Committing..." % (cntnew, cntupd)
  133. targetdb.commit()
RAW Paste Data
Top