mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Refactor
This commit is contained in:
parent
ebf87dada1
commit
d134c803ba
@ -26,9 +26,10 @@ from calibre.gui2.languages import LanguagesEdit
|
|||||||
from calibre.gui2.progress_indicator import ProgressIndicator
|
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 import dictionaries, current_container, set_book_locale, tprefs, editors
|
||||||
from calibre.gui2.tweak_book.widgets import Dialog
|
from calibre.gui2.tweak_book.widgets import Dialog
|
||||||
|
from calibre.spell import DictionaryLocale
|
||||||
from calibre.spell.dictionary import (
|
from calibre.spell.dictionary import (
|
||||||
builtin_dictionaries, custom_dictionaries, best_locale_for_language,
|
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.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.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
|
from calibre.utils.icu import sort_key, primary_sort_key, primary_contains, contains
|
||||||
|
@ -6,5 +6,36 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,16 +6,16 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
import cPickle, os, glob, shutil, re
|
import os, glob, shutil, re
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
from calibre.constants import plugins, config_dir
|
from calibre.constants import plugins, config_dir
|
||||||
|
from calibre.spell import parse_lang_code
|
||||||
from calibre.utils.config import JSONConfig
|
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')
|
Dictionary = namedtuple('Dictionary', 'primary_locale locales dicpath affpath builtin name id')
|
||||||
LoadedDictionary = namedtuple('Dictionary', 'primary_locale locales obj builtin name id')
|
LoadedDictionary = namedtuple('Dictionary', 'primary_locale locales obj builtin name id')
|
||||||
hunspell = plugins['hunspell'][0]
|
hunspell = plugins['hunspell'][0]
|
||||||
@ -40,30 +40,6 @@ class UserDictionary(object):
|
|||||||
return {'name':self.name, 'is_active': self.is_active, 'words':[
|
return {'name':self.name, 'is_active': self.is_active, 'words':[
|
||||||
(w, l) for w, l in self.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
|
_builtins = _custom = None
|
||||||
|
|
||||||
def builtin_dictionaries():
|
def builtin_dictionaries():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user