def addKanji(note, flag=False, currentFieldIndex=None, skipDstFieldIfSet=True): ''' Checks to see if a kanji should be added, and adds it if so. ''' if not modelIsCorrectType(note.model()): return flag if currentFieldIndex != None: # We've left a field # But it isn't the relevant one if note.model()['flds'][currentFieldIndex]['name'] != srcField: return flag if skipDstFieldIfSet and note[dstField]: return flag srcTxt = mw.col.media.strip(note[srcField]) dst = '' #srcTxt = string.replace(srcTxt, u'\uff5e', u'\u301c').encode('euc-jp') for character in characters_to_colorize(unicode(srcTxt)): # write to file; anki works in the media directory by default try: filename = KanjiVG(character).ascii_filename except InvalidCharacterError: # silently ignore non-Japanese characters continue char_svg = kc.get_colored_svg(character).encode('utf_8') anki_fname = mw.col.media.writeData(unicode(filename, 'utf_8'), char_svg) dst += '<img src="{!s}">'.format(anki_fname).encode('utf_8') note[dstField] = dst note.flush() return True
def test_with_invalid_character_raises_correct_ex_args(self): with self.assertRaises(colorizer.InvalidCharacterError) as cm: KanjiVG(u'Л') # args set self.assertEqual(cm.exception.args[0], u'Л') self.assertEqual(cm.exception.args[1], '') # message contains the useful information self.assertIn(repr(u'Л'), repr(cm.exception))
def test_with_nonexistent_variant_raises_correct_ex_args(self): with self.assertRaises(colorizer.InvalidCharacterError) as cm: KanjiVG(u'字', 'gobbledygook') # args set self.assertEqual(cm.exception.args[0], u'字') self.assertEqual(cm.exception.args[1], 'gobbledygook') # message contains the useful information self.assertIn(repr(u'字'), repr(cm.exception)) self.assertIn(repr('gobbledygook'), repr(cm.exception))
def addKanji(note, flag=False, currentFieldIndex=None): ''' Checks to see if a kanji should be added, and adds it if so. ''' if not modelIsCorrectType(note.model()): return flag if currentFieldIndex != None: # We've left a field # But it isn't the relevant one if note.model()['flds'][currentFieldIndex]['name'] != srcField: return None srcTxt = mw.col.media.strip(note[srcField]) dst = '' #srcTxt = string.replace(srcTxt, u'\uff5e', u'\u301c').encode('euc-jp') for character in characters_to_colorize(unicode(srcTxt)): # write to file; anki works in the media directory by default try: filename = KanjiVG(character).ascii_filename except InvalidCharacterError: # silently ignore non-Japanese characters continue try: with open(filename, 'w', encoding='utf-8') as file: file.write(kc.get_colored_svg(character)) mw.col.media.addFile(os.path.abspath(unicode(filename))) dst += u'<img src="%s">' % filename except IOError as e: if e.errno == FILE_NOT_FOUND: print "file not found: " + filename + ". Ignoring ..." else: raise note[dstField] = dst note.flush() return dst != ''
def test_explicit_none_variant_inits_to_empty_string(self): k = KanjiVG(u'字', None) self.assertEquals(k.variant, '')
def test_valid_variant_contains_named_stroke_group(self): ''' This is a proxy for having read the correct file ''' k = KanjiVG(u'字', 'Kaisho') self.assertIn('kvg:StrokePaths_05b57-Kaisho', k.svg)
def test_valid_variant_inits(self): k = KanjiVG(u'字', 'Kaisho') self.assertEqual(k.character, u'字') self.assertEqual(k.variant, 'Kaisho')
def test_valid_nonascii_character_contains_named_stroke_group(self): ''' This is a proxy for having read the correct file ''' k = KanjiVG(u'あ') self.assertIn('kvg:StrokePaths_03042', k.svg)
def test_valid_nonascii_character_inits(self): k = KanjiVG(u'あ') self.assertEqual(k.character, u'あ') self.assertEqual(k.variant, '')
def test_valid_ascii_character_inits(self): k = KanjiVG('a') self.assertEqual(k.character, 'a') self.assertEqual(k.variant, '')
def test_five_digit_inits(self): k = KanjiVG._create_from_filename('26951.svg') self.assertEquals(k.character, u'𦥑')
def test_without_variant_has_correct_filename(self): k = KanjiVG(u'あ') self.assertEqual(k.character_filename, u'あ.svg')
def test_with_variant_has_correct_filename(self): k = KanjiVG(u'字', 'Kaisho') self.assertEqual(k.character_filename, u'字-Kaisho.svg')
def test_five_digit_unicode_character_has_correct_filename(self): k = KanjiVG(u'𦥑') self.assertEqual(k.ascii_filename, '26951.svg')
def test_with_variant_has_correct_filename(self): k = KanjiVG(u'字', 'Kaisho') self.assertEqual(k.ascii_filename, '05b57-Kaisho.svg')
def test_without_variant_has_correct_filename(self): k = KanjiVG(u'あ') self.assertEqual(k.ascii_filename, '03042.svg')
def test_without_variant_with_hex_inits(self): k = KanjiVG._create_from_filename('06f22.svg') self.assertEquals(k.character, u'漢') self.assertEquals(k.variant, '')
def test_first_is_a_kanji(self): all_kanji = KanjiVG.get_all() self.assertIsInstance(all_kanji[0], KanjiVG)
def test_has_correct_number(self): all_kanji = KanjiVG.get_all() self.assertEqual(len(all_kanji), TOTAL_NUMBER_CHARACTERS)
def test_with_variant_inits(self): k = KanjiVG._create_from_filename('05b57-Kaisho.svg') self.assertEquals(k.character, u'字') self.assertEquals(k.variant, 'Kaisho')