diff --git a/src/calibre/gui2/tweak_book/spell.py b/src/calibre/gui2/tweak_book/spell.py index d62c13b8a9..7bd3a45280 100644 --- a/src/calibre/gui2/tweak_book/spell.py +++ b/src/calibre/gui2/tweak_book/spell.py @@ -26,9 +26,10 @@ from calibre.gui2.languages import LanguagesEdit from calibre.gui2.progress_indicator import ProgressIndicator from calibre.gui2.tweak_book import dictionaries, current_container, set_book_locale, tprefs, editors from calibre.gui2.tweak_book.widgets import Dialog +from calibre.spell import DictionaryLocale from calibre.spell.dictionary import ( builtin_dictionaries, custom_dictionaries, best_locale_for_language, - get_dictionary, DictionaryLocale, dprefs, remove_dictionary, rename_dictionary) + get_dictionary, dprefs, remove_dictionary, rename_dictionary) from calibre.spell.import_from import import_from_oxt from calibre.utils.localization import calibre_langcode_to_name, get_language, get_lang, canonicalize_lang from calibre.utils.icu import sort_key, primary_sort_key, primary_contains, contains diff --git a/src/calibre/spell/__init__.py b/src/calibre/spell/__init__.py index 3adfa10d58..2ca92243f3 100644 --- a/src/calibre/spell/__init__.py +++ b/src/calibre/spell/__init__.py @@ -6,5 +6,36 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2014, Kovid Goyal ' +import cPickle +from collections import namedtuple + +from calibre.utils.localization import canonicalize_lang + +DictionaryLocale = namedtuple('DictionaryLocale', 'langcode countrycode') + +ccodes, ccodemap, country_names = None, None, None + +def get_codes(): + global ccodes, ccodemap, country_names + if ccodes is None: + data = cPickle.loads(P('localization/iso3166.pickle', allow_user_override=False, data=True)) + ccodes, ccodemap, country_names = data['codes'], data['three_map'], data['names'] + return ccodes, ccodemap + +def parse_lang_code(raw): + raw = raw or '' + parts = raw.replace('_', '-').split('-') + lc = canonicalize_lang(parts[0]) + if lc is None: + raise ValueError('Invalid language code: %r' % raw) + cc = None + if len(parts) > 1: + ccodes, ccodemap = get_codes()[:2] + q = parts[1].upper() + if q in ccodes: + cc = q + else: + cc = ccodemap.get(q, None) + return DictionaryLocale(lc, cc) diff --git a/src/calibre/spell/dictionary.py b/src/calibre/spell/dictionary.py index c939f6ac65..3b2b55a288 100644 --- a/src/calibre/spell/dictionary.py +++ b/src/calibre/spell/dictionary.py @@ -6,16 +6,16 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2014, Kovid Goyal ' -import cPickle, os, glob, shutil, re +import os, glob, shutil, re from collections import namedtuple from operator import attrgetter from itertools import chain from calibre.constants import plugins, config_dir +from calibre.spell import parse_lang_code from calibre.utils.config import JSONConfig -from calibre.utils.localization import get_lang, canonicalize_lang, get_system_locale +from calibre.utils.localization import get_lang, get_system_locale -DictionaryLocale = namedtuple('DictionaryLocale', 'langcode countrycode') Dictionary = namedtuple('Dictionary', 'primary_locale locales dicpath affpath builtin name id') LoadedDictionary = namedtuple('Dictionary', 'primary_locale locales obj builtin name id') hunspell = plugins['hunspell'][0] @@ -40,30 +40,6 @@ class UserDictionary(object): return {'name':self.name, 'is_active': self.is_active, 'words':[ (w, l) for w, l in self.words]} -ccodes, ccodemap, country_names = None, None, None -def get_codes(): - global ccodes, ccodemap, country_names - if ccodes is None: - data = cPickle.loads(P('localization/iso3166.pickle', allow_user_override=False, data=True)) - ccodes, ccodemap, country_names = data['codes'], data['three_map'], data['names'] - return ccodes, ccodemap - -def parse_lang_code(raw): - raw = raw or '' - parts = raw.replace('_', '-').split('-') - lc = canonicalize_lang(parts[0]) - if lc is None: - raise ValueError('Invalid language code: %r' % raw) - cc = None - if len(parts) > 1: - ccodes, ccodemap = get_codes()[:2] - q = parts[1].upper() - if q in ccodes: - cc = q - else: - cc = ccodemap.get(q, None) - return DictionaryLocale(lc, cc) - _builtins = _custom = None def builtin_dictionaries():