Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Steps towards Compatibility with Anki 2.1 #72

Closed
wants to merge 10 commits into from
26 changes: 12 additions & 14 deletions Chinese_support.py → __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,17 @@
# Quick-and-dirty trick to remove cjklib warning on a Linux with a
# full python install, about having two different versions of
# sqlalchemy, httplib2, ... on Ubuntu and Fedora
sys.path = filter(
lambda a: not(re.search(r'(dist|site)-packages$', a)), sys.path)
sys.path = [a for a in sys.path if not(re.search(r'(dist|site)-packages$', a))]

import chinese.upgrade
import chinese.templates.ruby ; chinese.templates.ruby.install()
import chinese.templates.chinese ; chinese.templates.chinese.install()
from .chinese import upgrade
from .chinese.templates import ruby as templates_ruby ; templates_ruby.install()
from .chinese.templates import chinese as templates_chinese ; templates_chinese.install()

import chinese.ui
import chinese.edit
import chinese.models.basic
import chinese.models.advanced
#import chinese.models.compatibility
#import chinese.models.ruby
#import chinese.models.ruby_synonyms
import chinese.ui
import chinese.graph
from .chinese import ui
from .chinese import edit
from .chinese.models import basic as models_basic
from .chinese.models import advanced as models_advanced
#from .chinese.models import compatibility as models_compatibility
#from .chinese.models import ruby as models_ruby
#from .chinese.models import ruby_synonyms as models_ruby_synonyms
from .chinese import graph
12 changes: 6 additions & 6 deletions chinese/baidu_tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
Download Chinese pronunciations from GoogleTTS
'''

import urllib
import urllib2
import urllib.request, urllib.parse, urllib.error
import urllib.request, urllib.error, urllib.parse
import os
import re

from aqt import mw


download_file_extension = u'.mp3'
download_file_extension = '.mp3'
url_gtts = 'http://tts.baidu.com/text2audio?'
user_agent_string = 'Mozilla/5.0'

Expand All @@ -33,9 +33,9 @@ def get_word_from_baidu(source, lang="zh"):
return filename
get_url = build_query_url(source, lang)
# This may throw an exception
request = urllib2.Request(get_url)
request = urllib.request.Request(get_url)
request.add_header('User-agent', user_agent_string)
response = urllib2.urlopen(request, timeout=5)
response = urllib.request.urlopen(request, timeout=5)
if 200 != response.code:
raise ValueError(str(response.code) + ': ' + response.msg)
with open(fullpath, 'wb') as audio_file:
Expand All @@ -45,7 +45,7 @@ def get_word_from_baidu(source, lang="zh"):

def build_query_url(source, lang):
qdict = dict(lan=lang, ie="UTF-8", text=source.encode('utf-8'))
return url_gtts + urllib.urlencode(qdict)
return url_gtts + urllib.parse.urlencode(qdict)


def get_filename(base, end):
Expand Down
110 changes: 52 additions & 58 deletions chinese/bopomofo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,25 @@



import string





# early replacements


replacements = [


(u"yu", u"u:"), (u"ü", u"u:"), (u"v", u"u:"),
("yu", "u:"), ("ü", "u:"), ("v", "u:"),


(u"yi", u"i"), (u"you", u"ㄧㄡ"), (u"y", u"i"),
("yi", "i"), ("you", "ㄧㄡ"), ("y", "i"),


(u"wu", u"u"), (u"wong", u"ㄨㄥ"), (u"w", u"u"),
("wu", "u"), ("wong", "ㄨㄥ"), ("w", "u"),


(u"jue", u"ㄐㄩㄝ"), (u"lue", u"ㄌㄩㄝ"), (u"nue", u"ㄋㄩㄝ"),
("jue", "ㄐㄩㄝ"), ("lue", "ㄌㄩㄝ"), ("nue", "ㄋㄩㄝ"),


(u"que", u"ㄑㄩㄝ"), (u"xue", u"ㄒㄩㄝ"), (u"yue", u"ㄩㄝ"),
("que", "ㄑㄩㄝ"), ("xue", "ㄒㄩㄝ"), ("yue", "ㄩㄝ"),

]

Expand All @@ -60,16 +54,16 @@
# special cases


(u"ju", u"ㄐㄩ"), (u"qu", u"ㄑㄩ"), (u"xu", u"ㄒㄩ"),
("ju", "ㄐㄩ"), ("qu", "ㄑㄩ"), ("xu", "ㄒㄩ"),


(u"zhi", u"ㄓ"), (u"chi", u"ㄔ"), (u"shi", u"ㄕ"), (u"ri", u"ㄖ"),
("zhi", "ㄓ"), ("chi", "ㄔ"), ("shi", "ㄕ"), ("ri", "ㄖ"),


(u"zi", u"ㄗ"), (u"ci", u"ㄘ"), (u"si", u"ㄙ"),
("zi", "ㄗ"), ("ci", "ㄘ"), ("si", "ㄙ"),


(u"r5", u"ㄦ"),
("r5", "ㄦ"),



Expand All @@ -78,22 +72,22 @@
# initials


(u"b", u"ㄅ"), (u"p", u"ㄆ"), (u"m", u"ㄇ"), (u"f", u"ㄈ"),
("b", "ㄅ"), ("p", "ㄆ"), ("m", "ㄇ"), ("f", "ㄈ"),


(u"d", u"ㄉ"), (u"t", u"ㄊ"), (u"n", u"ㄋ"), (u"l", u"ㄌ"),
("d", "ㄉ"), ("t", "ㄊ"), ("n", "ㄋ"), ("l", "ㄌ"),


(u"g", u"ㄍ"), (u"k", u"ㄎ"), (u"h", u"ㄏ"),
("g", "ㄍ"), ("k", "ㄎ"), ("h", "ㄏ"),


(u"j", u"ㄐ"), (u"q", u"ㄑ"), (u"x", u"ㄒ"),
("j", "ㄐ"), ("q", "ㄑ"), ("x", "ㄒ"),


(u"zh", u"ㄓ"), (u"ch", u"ㄔ"), (u"sh", u"ㄕ"), (u"r", u"ㄖ"),
("zh", "ㄓ"), ("ch", "ㄔ"), ("sh", "ㄕ"), ("r", "ㄖ"),


(u"z", u"ㄗ"), (u"c", u"ㄘ"), (u"s", u"ㄙ"),
("z", "ㄗ"), ("c", "ㄘ"), ("s", "ㄙ"),



Expand All @@ -102,40 +96,40 @@
# finals


(u"i", u"ㄧ"), (u"u", u"ㄨ"), (u"u:", u"ㄩ"),
("i", "ㄧ"), ("u", "ㄨ"), ("u:", "ㄩ"),


(u"a", u"ㄚ"), (u"o", u"ㄛ"), (u"e", u"ㄜ"), (u"ê", u"ㄝ"),
("a", "ㄚ"), ("o", "ㄛ"), ("e", "ㄜ"), ("ê", "ㄝ"),


(u"ai", u"ㄞ"), (u"ei", u"ㄟ"), (u"ao", u"ㄠ"), (u"ou", u"ㄡ"),
("ai", "ㄞ"), ("ei", "ㄟ"), ("ao", "ㄠ"), ("ou", "ㄡ"),


(u"an", u"ㄢ"), (u"en", u"ㄣ"), (u"ang", u"ㄤ"), (u"eng", u"ㄥ"),
("an", "ㄢ"), ("en", "ㄣ"), ("ang", "ㄤ"), ("eng", "ㄥ"),


(u"er", u"ㄦ"),
("er", "ㄦ"),


(u"ia", u"ㄧㄚ"), (u"io", u"ㄧㄛ"), (u"ie", u"ㄧㄝ"), (u"iai", u"ㄧㄞ"),
("ia", "ㄧㄚ"), ("io", "ㄧㄛ"), ("ie", "ㄧㄝ"), ("iai", "ㄧㄞ"),


(u"iao", u"ㄧㄠ"), (u"iu", u"ㄧㄡ"), (u"ian", u"ㄧㄢ"),
("iao", "ㄧㄠ"), ("iu", "ㄧㄡ"), ("ian", "ㄧㄢ"),


(u"in", u"ㄧㄣ"), (u"iang", u"ㄧㄤ"), (u"ing", u"ㄧㄥ"),
("in", "ㄧㄣ"), ("iang", "ㄧㄤ"), ("ing", "ㄧㄥ"),


(u"ua", u"ㄨㄚ"), (u"uo", u"ㄨㄛ"), (u"uai", u"ㄨㄞ"),
("ua", "ㄨㄚ"), ("uo", "ㄨㄛ"), ("uai", "ㄨㄞ"),


(u"ui", u"ㄨㄟ"), (u"uan", u"ㄨㄢ"), (u"un", u"ㄨㄣ"),
("ui", "ㄨㄟ"), ("uan", "ㄨㄢ"), ("un", "ㄨㄣ"),


(u"uang", u"ㄨㄤ"), (u"ong", u"ㄨㄥ"),
("uang", "ㄨㄤ"), ("ong", "ㄨㄥ"),


(u"u:e", u"ㄩㄝ"), (u"u:an", u"ㄩㄢ"), (u"u:n", u"ㄩㄣ"), (u"iong", u"ㄩㄥ"),
("u:e", "ㄩㄝ"), ("u:an", "ㄩㄢ"), ("u:n", "ㄩㄣ"), ("iong", "ㄩㄥ"),



Expand All @@ -144,13 +138,13 @@
# tones


(u"1", u""), (u"2", u"ˊ"),
("1", ""), ("2", "ˊ"),


(u"3", u"ˇ"), (u"4", u"ˋ"),
("3", "ˇ"), ("4", "ˋ"),


(u"5", u"˙"),
("5", "˙"),


]
Expand Down Expand Up @@ -192,7 +186,7 @@ def bopomofo(pinyin):
for pair in replacements:


pinyin = string.replace(pinyin, pair[0], pair[1])
pinyin = pinyin.replace(pair[0], pair[1])



Expand Down Expand Up @@ -222,67 +216,67 @@ def bopomofo(pinyin):
# a few of these are extremely rare in usage


(u"dong1 xi5", u"ㄉㄨㄥ ㄒㄧ˙"),
("dong1 xi5", "ㄉㄨㄥ ㄒㄧ˙"),


(u"lai2", u"ㄌㄞˊ"),
("lai2", "ㄌㄞˊ"),


(u"shui3", u"ㄕㄨㄟˇ"),
("shui3", "ㄕㄨㄟˇ"),


(u"de5", u"ㄉㄜ˙"),
("de5", "ㄉㄜ˙"),


(u"shi4", u"ㄕˋ"),
("shi4", "ㄕˋ"),


(u"zi3", u"ㄗˇ"),
("zi3", "ㄗˇ"),


(u"ri4", u"ㄖˋ"),
("ri4", "ㄖˋ"),


(u"ren2", u"ㄖㄣˊ"),
("ren2", "ㄖㄣˊ"),


(u"er4", u"ㄦˋ"),
("er4", "ㄦˋ"),


(u"r5", u"ㄦ"),
("r5", "ㄦ"),


(u"qu3", u"ㄑㄩˇ"),
("qu3", "ㄑㄩˇ"),


(u"xiong1", u"ㄒㄩㄥ"),
("xiong1", "ㄒㄩㄥ"),


(u"yue4", u"ㄩㄝˋ"),
("yue4", "ㄩㄝˋ"),


(u"yai2", u"ㄧㄞˊ"),
("yai2", "ㄧㄞˊ"),


(u"yo1", u"ㄧㄛ"),
("yo1", "ㄧㄛ"),


(u"yi1", u"ㄧ"),
("yi1", "ㄧ"),


(u"you3", u"ㄧㄡˇ"),
("you3", "ㄧㄡˇ"),


(u"wu3", u"ㄨˇ"),
("wu3", "ㄨˇ"),


(u"wong1", u"ㄨㄥ"),
("wong1", "ㄨㄥ"),


(u"e2", u"ㄜˊ"),
("e2", "ㄜˊ"),


(u"ê4", u"ㄝˋ"),
("ê4", "ㄝˋ"),


]
Expand Down
22 changes: 8 additions & 14 deletions chinese/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,9 @@

import os.path
import json
import __init__
from .__init__ import __version__
from aqt import mw

initial_options = {
'startup_tip_number':0,
'show_startup_tips':True,
'dictionary':'None',
'transcription':'Pinyin',
'speech':'Google TTS Mandarin',
}

startup_tips = [
('Thank you for downloading the <b>Chinese Support Add-on</b>.<br>Please remember to select a dictionary in the <tt>Tools-&gt;Chinese support</tt> menu, so that you may have automatic translation.', None),
(None, None),
Expand All @@ -44,16 +36,18 @@ def __init__(self):
self.filepath = os.path.join(mw.pm.addonFolder(), "chinese", "chinese_addon_config.json")
self.load()
#Options that may be missing in some installs initialized by old versions of this code
self.add_option("latest_available_version", __init__.__version__)
self.add_option("latest_available_version", __version__)
self.add_option("next_version_message", None)
self.add_option("warned_about_MS_translate_long_delays", False)
def load(self):
if not os.path.exists(self.filepath):
self.create_new()
self.options = json.load(open(self.filepath, 'r'))
# if not os.path.exists(self.filepath):
# self.create_new()
# self.options = json.load(open(self.filepath, 'r'))
self.options = mw.addonManager.getConfig(__name__)

def save(self):
json.dump(self.options, open(self.filepath, 'w'))
# json.dump(self.options, open(self.filepath, 'w'))
mw.addonManager.writeConfig(__name__, self.options)

def create_new(self):
self.options = initial_options
Expand Down
Loading