def locale_from_tag(tag): if 'lang' in tag.attrib: loc = parse_lang_code(tag.get('lang')) if loc is not None: return loc if '{http://www.w3.org/XML/1998/namespace}lang' in tag.attrib: loc = parse_lang_code(tag.get('{http://www.w3.org/XML/1998/namespace}lang')) if loc is not None: return loc
def locale_from_tag(tag): if 'lang' in tag.attrib: loc = parse_lang_code(tag.get('lang')) if loc is not None: return loc if '{http://www.w3.org/XML/1998/namespace}lang' in tag.attrib: loc = parse_lang_code( tag.get('{http://www.w3.org/XML/1998/namespace}lang')) if loc is not None: return loc
def import_from_oxt(source_path, name, dest_dir=None, prefix='dic-'): from calibre.spell.dictionary import parse_lang_code dest_dir = dest_dir or os.path.join(config_dir, 'dictionaries') if not os.path.exists(dest_dir): os.makedirs(dest_dir) num = 0 with ZipFile(source_path) as zf: root = etree.fromstring(zf.open('META-INF/manifest.xml').read()) xcu = XPath( '//manifest:file-entry[@manifest:media-type="application/vnd.sun.star.configuration-data"]' )(root)[0].get('{%s}full-path' % NS_MAP['manifest']) for (dic, aff), locales in parse_xcu(zf.open(xcu).read(), origin='').iteritems(): dic, aff = dic.lstrip('/'), aff.lstrip('/') d = tempfile.mkdtemp(prefix=prefix, dir=dest_dir) locales = uniq([ x for x in map(fill_country_code, locales) if parse_lang_code(x).countrycode ]) if not locales: continue metadata = [name] + list(locales) with open(os.path.join(d, 'locales'), 'wb') as f: f.write(('\n'.join(metadata)).encode('utf-8')) with open(os.path.join(d, '%s.dic' % locales[0]), 'wb') as f: shutil.copyfileobj(zf.open(dic), f) with open(os.path.join(d, '%s.aff' % locales[0]), 'wb') as f: shutil.copyfileobj(zf.open(aff), f) num += 1 return num
def quoted_val(state, text, i, formats, user_data): ' A quoted attribute value ' quote = '"' if state.parse is DQ_VAL else "'" add_attr_data(user_data, ATTR_VALUE, ATTR_START, i) pos = text.find(quote, i) if pos == -1: num = len(text) - i is_link = is_class = False else: num = pos - i + 1 state.parse = IN_OPENING_TAG if state.tag_being_defined is not None and state.attribute_name in ( 'lang', 'xml:lang'): try: state.tag_being_defined.lang = parse_lang_code(text[i:pos]) except ValueError: pass add_attr_data(user_data, ATTR_VALUE, ATTR_END, i + num) is_link = state.attribute_name in LINK_ATTRS is_class = not is_link and state.attribute_name == 'class' if is_link: if verify_link(text[i:i + num - 1], user_data.doc_name) is False: return [(num - 1, formats['bad_link']), (1, formats['string'])] return [(num - 1, formats['link']), (1, formats['string'])] elif is_class: return [(num - 1, formats['class_attr']), (1, formats['string'])] return [(num, formats['string'])]
def quoted_val(state, text, i, formats, user_data): ' A quoted attribute value ' quote = '"' if state.parse is DQ_VAL else "'" add_attr_data(user_data, ATTR_VALUE, ATTR_START, i) pos = text.find(quote, i) if pos == -1: num = len(text) - i is_link = is_class = False else: num = pos - i + 1 state.parse = IN_OPENING_TAG if state.tag_being_defined is not None and state.attribute_name in ('lang', 'xml:lang'): try: state.tag_being_defined.lang = parse_lang_code(text[i:pos]) except ValueError: pass add_attr_data(user_data, ATTR_VALUE, ATTR_END, i + num) is_link = state.attribute_name in LINK_ATTRS is_class = not is_link and state.attribute_name == 'class' if is_link: if verify_link(text[i:i+num - 1], user_data.doc_name) is False: return [(num - 1, formats['bad_link']), (1, formats['string'])] return [(num - 1, formats['link']), (1, formats['string'])] elif is_class: return [(num - 1, formats['class_attr']), (1, formats['string'])] return [(num, formats['string'])]
def import_from_oxt(source_path, name, dest_dir=None, prefix="dic-"): from calibre.spell.dictionary import parse_lang_code dest_dir = dest_dir or os.path.join(config_dir, "dictionaries") if not os.path.exists(dest_dir): os.makedirs(dest_dir) num = 0 with ZipFile(source_path) as zf: root = etree.fromstring(zf.open("META-INF/manifest.xml").read()) xcu = XPath('//manifest:file-entry[@manifest:media-type="application/vnd.sun.star.configuration-data"]')(root)[ 0 ].get("{%s}full-path" % NS_MAP["manifest"]) for (dic, aff), locales in parse_xcu(zf.open(xcu).read(), origin="").iteritems(): dic, aff = dic.lstrip("/"), aff.lstrip("/") d = tempfile.mkdtemp(prefix=prefix, dir=dest_dir) locales = uniq([x for x in map(fill_country_code, locales) if parse_lang_code(x).countrycode]) if not locales: continue metadata = [name] + list(locales) with open(os.path.join(d, "locales"), "wb") as f: f.write(("\n".join(metadata)).encode("utf-8")) with open(os.path.join(d, "%s.dic" % locales[0]), "wb") as f: shutil.copyfileobj(zf.open(dic), f) with open(os.path.join(d, "%s.aff" % locales[0]), "wb") as f: shutil.copyfileobj(zf.open(aff), f) num += 1 return num
def import_from_oxt(source_path, name, dest_dir=None, prefix='dic-'): from calibre.spell.dictionary import parse_lang_code dest_dir = dest_dir or os.path.join(config_dir, 'dictionaries') if not os.path.exists(dest_dir): os.makedirs(dest_dir) num = 0 with ZipFile(source_path) as zf: root = etree.fromstring(zf.open('META-INF/manifest.xml').read()) xcu = XPath('//manifest:file-entry[@manifest:media-type="application/vnd.sun.star.configuration-data"]')(root)[0].get( '{%s}full-path' % NS_MAP['manifest']) for (dic, aff), locales in parse_xcu(zf.open(xcu).read(), origin='').iteritems(): dic, aff = dic.lstrip('/'), aff.lstrip('/') d = tempfile.mkdtemp(prefix=prefix, dir=dest_dir) locales = [x for x in locales if parse_lang_code(x).countrycode] if not locales: continue metadata = [name] + locales with open(os.path.join(d, 'locales'), 'wb') as f: f.write(('\n'.join(metadata)).encode('utf-8')) with open(os.path.join(d, '%s.dic' % locales[0]), 'wb') as f: shutil.copyfileobj(zf.open(dic), f) with open(os.path.join(d, '%s.aff' % locales[0]), 'wb') as f: shutil.copyfileobj(zf.open(aff), f) num += 1 return num
def set_book_locale(lang): dictionaries.initialize() try: dictionaries.default_locale = parse_lang_code(lang) if dictionaries.default_locale.langcode == 'und': raise ValueError('') except ValueError: dictionaries.default_locale = dictionaries.ui_locale
def locale_from_tag(tag): a = tag.attrib if 'lang' in a: try: loc = parse_lang_code(tag.get('lang')) except ValueError: loc = None if loc is not None: return loc if '{http://www.w3.org/XML/1998/namespace}lang' in a: try: loc = parse_lang_code( tag.get('{http://www.w3.org/XML/1998/namespace}lang')) except ValueError: loc = None if loc is not None: return loc
def set_book_locale(lang): dictionaries.initialize() try: dictionaries.default_locale = parse_lang_code(lang) if dictionaries.default_locale.langcode == 'und': raise ValueError('') except ValueError: dictionaries.default_locale = dictionaries.ui_locale from calibre.gui2.tweak_book.editor.syntax.html import refresh_spell_check_status refresh_spell_check_status()
def quoted_val(state, text, i, formats, user_data): ' A quoted attribute value ' quote = '"' if state.parse is DQ_VAL else "'" add_attr_data(user_data, ATTR_VALUE, ATTR_START, i) pos = text.find(quote, i) if pos == -1: num = len(text) - i else: num = pos - i + 1 state.parse = IN_OPENING_TAG if state.tag_being_defined is not None and state.attribute_name in ('lang', 'xml:lang'): try: state.tag_being_defined.lang = parse_lang_code(text[i:pos]) except ValueError: pass add_attr_data(user_data, ATTR_VALUE, ATTR_END, i + num) return [(num, formats['string'])]
def quoted_val(state, text, i, formats, user_data): ' A quoted attribute value ' quote = '"' if state.parse is DQ_VAL else "'" add_attr_data(user_data, ATTR_VALUE, ATTR_START, i) pos = text.find(quote, i) if pos == -1: num = len(text) - i else: num = pos - i + 1 state.parse = IN_OPENING_TAG if state.tag_being_defined is not None and state.attribute_name in ( 'lang', 'xml:lang'): try: state.tag_being_defined.lang = parse_lang_code(text[i:pos]) except ValueError: pass add_attr_data(user_data, ATTR_VALUE, ATTR_END, i + num) return [(num, formats['string'])]
def import_from_oxt(source_path, name, dest_dir=None, prefix='dic-'): from calibre.spell.dictionary import parse_lang_code dest_dir = dest_dir or os.path.join(config_dir, 'dictionaries') if not os.path.exists(dest_dir): os.makedirs(dest_dir) num = 0 with ZipFile(source_path) as zf: def read_file(key): try: return zf.open(key).read() except KeyError: # Some dictionaries apparently put the xcu in a sub-directory # and incorrectly make paths relative to that directory instead # of the root, for example: # http://extensions.libreoffice.org/extension-center/italian-dictionary-thesaurus-hyphenation-patterns/releases/4.1/dict-it.oxt while key.startswith('../'): key = key[3:] return zf.open(key.lstrip('/')).read() root = etree.fromstring(zf.open('META-INF/manifest.xml').read()) xcu = XPath( '//manifest:file-entry[@manifest:media-type="application/vnd.sun.star.configuration-data"]' )(root)[0].get('{%s}full-path' % NS_MAP['manifest']) for (dic, aff), locales in iteritems( parse_xcu(zf.open(xcu).read(), origin='')): dic, aff = dic.lstrip('/'), aff.lstrip('/') d = tempfile.mkdtemp(prefix=prefix, dir=dest_dir) locales = uniq([ x for x in map(fill_country_code, locales) if parse_lang_code(x).countrycode ]) if not locales: continue metadata = [name] + list(locales) with open(os.path.join(d, 'locales'), 'wb') as f: f.write(('\n'.join(metadata)).encode('utf-8')) dd, ad = convert_to_utf8(read_file(dic), read_file(aff)) with open(os.path.join(d, '%s.dic' % locales[0]), 'wb') as f: f.write(dd) with open(os.path.join(d, '%s.aff' % locales[0]), 'wb') as f: f.write(ad) num += 1 return num
def import_from_oxt(source_path, name, dest_dir=None, prefix='dic-'): from calibre.spell.dictionary import parse_lang_code dest_dir = dest_dir or os.path.join(config_dir, 'dictionaries') if not os.path.exists(dest_dir): os.makedirs(dest_dir) num = 0 with ZipFile(source_path) as zf: def read_file(key): try: return zf.open(key).read() except KeyError: # Some dictionaries apparently put the xcu in a sub-directory # and incorrectly make paths relative to that directory instead # of the root, for example: # http://extensions.libreoffice.org/extension-center/italian-dictionary-thesaurus-hyphenation-patterns/releases/4.1/dict-it.oxt while key.startswith('../'): key = key[3:] return zf.open(key.lstrip('/')).read() root = etree.fromstring(zf.open('META-INF/manifest.xml').read()) xcu = XPath('//manifest:file-entry[@manifest:media-type="application/vnd.sun.star.configuration-data"]')(root)[0].get( '{%s}full-path' % NS_MAP['manifest']) for (dic, aff), locales in iteritems(parse_xcu(zf.open(xcu).read(), origin='')): dic, aff = dic.lstrip('/'), aff.lstrip('/') d = tempfile.mkdtemp(prefix=prefix, dir=dest_dir) locales = uniq([x for x in map(fill_country_code, locales) if parse_lang_code(x).countrycode]) if not locales: continue metadata = [name] + list(locales) with open(os.path.join(d, 'locales'), 'wb') as f: f.write(('\n'.join(metadata)).encode('utf-8')) dd, ad = convert_to_utf8(read_file(dic), read_file(aff)) with open(os.path.join(d, '%s.dic' % locales[0]), 'wb') as f: f.write(dd) with open(os.path.join(d, '%s.aff' % locales[0]), 'wb') as f: f.write(ad) num += 1 return num