diff --git a/Chinese_support.py b/__init__.py similarity index 75% rename from Chinese_support.py rename to __init__.py index 35c34d2..f4e1c1d 100644 --- a/Chinese_support.py +++ b/__init__.py @@ -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 diff --git a/chinese/baidu_tts.py b/chinese/baidu_tts.py index 46bb6cd..f986c46 100644 --- a/chinese/baidu_tts.py +++ b/chinese/baidu_tts.py @@ -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' @@ -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: @@ -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): diff --git a/chinese/bopomofo.py b/chinese/bopomofo.py index 1edc010..ba90fca 100644 --- a/chinese/bopomofo.py +++ b/chinese/bopomofo.py @@ -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", "ㄩㄝ"), ] @@ -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", "ㄦ"), @@ -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", "ㄙ"), @@ -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", "ㄩㄥ"), @@ -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", "˙"), ] @@ -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]) @@ -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", "ㄝˋ"), ] diff --git a/chinese/config.py b/chinese/config.py index 29bc5cf..b94818c 100644 --- a/chinese/config.py +++ b/chinese/config.py @@ -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 Chinese Support Add-on.
Please remember to select a dictionary in the Tools->Chinese support menu, so that you may have automatic translation.', None), (None, None), @@ -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 diff --git a/chinese/dictdb.py b/chinese/dictdb.py index 9e59818..3c4d19b 100644 --- a/chinese/dictdb.py +++ b/chinese/dictdb.py @@ -44,7 +44,8 @@ class DictDB: def __init__(self): try: from aqt import mw - db_file = os.path.join(mw.pm.addonFolder(), "chinese", "db", "chinese_dict.sqlite") + package_path = os.path.join(mw.pm.addonFolder(), *__name__.split(".")[:-1]) + db_file = os.path.join(package_path, "db", "chinese_dict.sqlite") except: #Used for local debugging db_file = "db/chinese_dict.sqlite" @@ -110,7 +111,7 @@ def get_pinyin(self, w, taiwan=False, wl=4): #We're looking up a string that's not in the dictionary #We'll try each 4-character sequence in turn, then 3-sequence, then 2-sequence and if those fails, do unit lookup. #transcription = u"" - transcription = u"" + transcription = "" w = w[:] last_was_pinyin = False while len(w)>0: @@ -144,7 +145,7 @@ def get_pinyin(self, w, taiwan=False, wl=4): def get_cantonese(self, w, only_one=True): """Returns a character-by-character cantonese transcription.""" - t = u"" + t = "" for c in w: self.c.execute("select kCantonese from hanzi where cp = ?;", (c,) ) try: @@ -189,7 +190,7 @@ def get_traditional(self, w, wl=4): #We're looking up a string that's not in the dictionary #We'll try each 4-character sequence in turn, then 3-sequence, then 2-sequence and if those fails, do unit lookup. - traditional = u"" + traditional = "" w = w[:] while len(w)>0: word_was_found = False @@ -246,7 +247,7 @@ def get_simplified(self, w, wl=4): #We're looking up a string that's not in the dictionary #We'll try each 4-character sequence in turn, then 3-sequence, then 2-sequence and if those fails, do unit lookup. - simplified = u"" + simplified = "" w = w[:] while len(w)>0: word_was_found = False @@ -291,7 +292,7 @@ def get_classifiers(self, txt): self.c.execute("select distinct classifiers from cidian where (traditional=? or simplified=?);", (txt, txt)) try: #fetchall returns a list of tuples, converts to a list of strings - return filter(lambda a:a, map(lambda a:a[0], self.c.fetchall())) + return [a for a in [a[0] for a in self.c.fetchall()] if a] except: return [] @@ -299,7 +300,7 @@ def get_alt_spellings(self, txt): self.c.execute("select distinct alternates from cidian where (traditional=? or simplified=?);", (txt, txt)) try: #fetchall returns a list of tuples, converts to a list of strings - return filter(lambda a:a, map(lambda a:a[0], self.c.fetchall())) + return [a for a in [a[0] for a in self.c.fetchall()] if a] except: return [] diff --git a/chinese/edit.py b/chinese/edit.py index e98349e..36fb735 100644 --- a/chinese/edit.py +++ b/chinese/edit.py @@ -21,12 +21,11 @@ from aqt import mw from anki.hooks import addHook, wrap from aqt.editor import Editor, _html -from aqt.utils import getBase -import anki.js +#from aqt.utils import getBase +#import anki.js -import Chinese_support -import edit_ui -import edit_behavior +from . import edit_ui +from . import edit_behavior # Focus lost hook ########################################################################## @@ -47,7 +46,7 @@ def on_focus_lost(flag, fields_data, focus_field): edit_behavior.update_fields(efields, updated_field, model_name, model_type) for k in field_names: - if efields[k] <> fields_data[k]: + if efields[k] != fields_data[k]: fields_data[k] = efields[k] flag = True @@ -64,11 +63,11 @@ def colorize_notes(self, note, hide=True, focus=False): for l in note.model()["css"].split("\n"): if l.startswith(".tone"): css_colors += l+"\n" - myHtml = _html % ( - getBase(self.mw.col), anki.js.jquery, - _("Show Duplicates")) - myHtml = myHtml.replace("