SHARE
TWEET
update kana
a guest
Aug 2nd, 2012
34
Never
- # -*- coding: utf-8 -*-
- """
- Fill new_kana field with stripped version of kana field.
- """
- from PyQt4.QtCore import *
- from PyQt4.QtGui import *
- import os, urllib, urllib2, shutil, hashlib
- from ankiqt import mw, ui
- from ankiqt.ui import utils
- from anki.cards import Card, Model
- # User settings
- """
- The name of the model. Other models will be ignored.
- """
- MODEL_NAME = 'Misc Japanese'
- """
- The field which keeps the Kana to be stripped.
- """
- KANA_FIELD = 'Reading'
- """
- Write Kana to this field.
- """
- NEW_KANA_FIELD = 'Kana'
- def strip_non_kana(s):
- """
- Strip all non-kana (hiragana, katakana) characters
- from the string s and return it.
- """
- s2 = ''
- for c in s:
- if ord(c) >= 0x3040 and ord(c) < 0x3100:
- s2 = s2 + c
- return s2
- def update_card(deck, card, log):
- """
- Update the card by stripping kana.
- """
- kana = strip_non_kana(card.fact[KANA_FIELD])
- card.fact[NEW_KANA_FIELD] = kana
- card.fact.setModified(textChanged=True,deck=deck)
- def get_cards_from_deck(deck, log):
- """
- Gets all the card adhering to the model from the specified deck.
- """
- sql = 'select id from models where name = \'%s\'' % MODEL_NAME
- model_id = deck.s.scalar(sql)
- if model_id is None:
- log.append('No model of name %s' % MODEL_NAME)
- return []
- sql = 'select id from cardmodels where modelId = %s' % model_id
- card_model_ids = deck.s.column0(sql)
- cards = []
- for card_model_id in card_model_ids:
- cards_query = deck.s.query(Card).filter('cardModelId = %s' % card_model_id)
- for card in cards_query:
- cards.append(card)
- return cards
- def process_deck(deck, log):
- """
- Processes the cards in the specified deck.
- """
- cards = get_cards_from_deck(deck, log)
- total = 0
- for card in cards:
- total = total + 1
- status = update_card(deck, card, log)
- if total > 0:
- deck.s.flush()
- deck.setModified()
- log.append('Total of %d cards modified.' % (total))
- def doFill():
- """
- Update deck and display log information.
- """
- # Check if we have a deck open
- if mw.deck is None: return
- log = []
- log.append('Starting update.')
- process_deck(mw.deck, log)
- log.append('Update complete.')
- utils.showInfo('\n'.join(log))
- def init_hook():
- """
- Initialises the Anki GUI to present an option to invoke the plugin.
- """
- l.append(('Fill audio fields', FillAudioField))
- def init():
- mw.mainWin.toolBar.addSeparator()
- FillAudioField= QAction(mw)
- FillAudioField.setText(u"Fill Kana Field")
- FillAudioField.setEnabled(True)
- mw.connect(FillAudioField,SIGNAL("triggered()"),doFill)
- mw.mainWin.toolBar.addAction(FillAudioField)
- mw.addHook("init", init)
- mw.registerPlugin(u"Fill Kana Field", 666)
RAW Paste Data
