Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

update kana

By: a guest on Aug 2nd, 2012  |  syntax: Python  |  size: 2.74 KB  |  views: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # -*- coding: utf-8 -*-
  2. """
  3. Fill new_kana field with stripped version of kana field.
  4. """
  5.  
  6. from PyQt4.QtCore import *
  7. from PyQt4.QtGui import *
  8.  
  9. import os, urllib, urllib2, shutil, hashlib
  10.  
  11. from ankiqt import mw, ui
  12. from ankiqt.ui import utils
  13. from anki.cards import Card, Model
  14.  
  15. # User settings
  16.  
  17. """
  18. The name of the model. Other models will be ignored.
  19. """
  20. MODEL_NAME = 'Misc Japanese'
  21.  
  22. """
  23. The field which keeps the Kana to be stripped.
  24. """
  25. KANA_FIELD = 'Reading'
  26.  
  27. """
  28. Write Kana to this field.
  29. """
  30. NEW_KANA_FIELD = 'Kana'
  31.  
  32. def strip_non_kana(s):
  33.   """
  34.  Strip all non-kana (hiragana, katakana) characters
  35.  from the string s and return it.
  36.  """
  37.   s2 = ''
  38.   for c in s:
  39.     if ord(c) >= 0x3040 and ord(c) < 0x3100:
  40.       s2 = s2 + c
  41.   return s2
  42.  
  43. def update_card(deck, card, log):
  44.   """
  45.  Update the card by stripping kana.
  46.  """
  47.   kana = strip_non_kana(card.fact[KANA_FIELD])
  48.   card.fact[NEW_KANA_FIELD] = kana
  49.   card.fact.setModified(textChanged=True,deck=deck)
  50.  
  51. def get_cards_from_deck(deck, log):
  52.   """
  53.  Gets all the card adhering to the model from the specified deck.
  54.  """
  55.   sql = 'select id from models where name = \'%s\'' % MODEL_NAME
  56.   model_id = deck.s.scalar(sql)
  57.  
  58.   if model_id is None:
  59.     log.append('No model of name %s' % MODEL_NAME)
  60.     return []
  61.  
  62.   sql = 'select id from cardmodels where modelId = %s' % model_id
  63.   card_model_ids = deck.s.column0(sql)
  64.  
  65.   cards = []
  66.   for card_model_id in card_model_ids:
  67.     cards_query = deck.s.query(Card).filter('cardModelId = %s' % card_model_id)
  68.     for card in cards_query:
  69.       cards.append(card)
  70.  
  71.   return cards
  72.  
  73. def process_deck(deck, log):
  74.   """
  75.  Processes the cards in the specified deck.
  76.  """
  77.   cards = get_cards_from_deck(deck, log)
  78.   total = 0
  79.  
  80.   for card in cards:
  81.     total = total + 1
  82.     status = update_card(deck, card, log)
  83.   if total > 0:
  84.     deck.s.flush()
  85.     deck.setModified()
  86.  
  87.   log.append('Total of %d cards modified.' % (total))
  88.  
  89. def doFill():
  90.   """
  91.  Update deck and display log information.
  92.  """
  93.   # Check if we have a deck open
  94.   if mw.deck is None: return
  95.  
  96.   log = []
  97.   log.append('Starting update.')
  98.   process_deck(mw.deck, log)
  99.   log.append('Update complete.')
  100.   utils.showInfo('\n'.join(log))
  101.  
  102. def init_hook():
  103.     """
  104.    Initialises the Anki GUI to present an option to invoke the plugin.
  105.    """
  106.     l.append(('Fill audio fields', FillAudioField))
  107.  
  108. def init():
  109.     mw.mainWin.toolBar.addSeparator()
  110.     FillAudioField= QAction(mw)
  111.     FillAudioField.setText(u"Fill Kana Field")
  112.     FillAudioField.setEnabled(True)
  113.     mw.connect(FillAudioField,SIGNAL("triggered()"),doFill)
  114.     mw.mainWin.toolBar.addAction(FillAudioField)
  115.  
  116. mw.addHook("init", init)
  117. mw.registerPlugin(u"Fill Kana Field", 666)
clone this paste RAW Paste Data