Merge branch 'fix-dictionaries' of https://github.com/un-pogaz/calibre

This commit is contained in:
Kovid Goyal 2023-10-04 15:03:21 +05:30
commit 0979c1a25c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 15 additions and 2 deletions

View File

@ -23,6 +23,9 @@ def parse_lang_code(raw):
if lc is None: if lc is None:
raise ValueError('Invalid language code: %r' % raw) raise ValueError('Invalid language code: %r' % raw)
cc = None cc = None
for sc in ['Cyrl', 'Latn']:
if sc in parts:
parts.remove(sc)
if len(parts) > 1: if len(parts) > 1:
ccodes, ccodemap = get_codes()[:2] ccodes, ccodemap = get_codes()[:2]
q = parts[1].upper() q = parts[1].upper()

View File

@ -45,6 +45,8 @@ def parse_xcu(raw, origin='%origin%'):
paths = [c.text.replace('%origin%', origin) for v in value for c in v.iterchildren('*') if c.text] paths = [c.text.replace('%origin%', origin) for v in value for c in v.iterchildren('*') if c.text]
aff, dic = paths if paths[0].endswith('.aff') else reversed(paths) aff, dic = paths if paths[0].endswith('.aff') else reversed(paths)
locales = ''.join(XPath('descendant::prop[@oor:name="Locales"]/value/text()')(node)).split() locales = ''.join(XPath('descendant::prop[@oor:name="Locales"]/value/text()')(node)).split()
if not locales:
locales = [str(item) for item in XPath('descendant::prop[@oor:name="Locales"]/value/it/text()')(node)]
ans[(dic, aff)] = locales ans[(dic, aff)] = locales
return ans return ans
@ -158,8 +160,16 @@ def import_from_oxt(source_path, name, dest_dir=None, prefix='dic-'):
def import_from_online(directory, name, dest_dir=None, prefix='dic-'): def import_from_online(directory, name, dest_dir=None, prefix='dic-'):
br = browser(timeout=30) br = browser(timeout=30)
def read_file(key): def read_file(key):
rp = br.open('/'.join((ONLINE_DICTIONARY_BASE_URL, directory, key))) try:
return rp.read() rp = br.open('/'.join((ONLINE_DICTIONARY_BASE_URL, directory, key)))
return rp.read()
except:
# Some dictionaries apparently put the dic and aff file in a
# sub-directory dictionaries and incorrectly make paths relative
# to that directory instead of the root, for example:
# https://github.com/LibreOffice/dictionaries/tree/master/ca
rp = br.open('/'.join((ONLINE_DICTIONARY_BASE_URL, directory, 'dictionaries', key)))
return rp.read()
return _import_from_virtual_directory(read_file, name, dest_dir=dest_dir, prefix=prefix) return _import_from_virtual_directory(read_file, name, dest_dir=dest_dir, prefix=prefix)