def indexes_generator(indexes_lang): """ factory that acts according to glossary language :param indexes_lang: str """ indexer = None """Callable[[Sequence[str], str], Sequence[str]]""" if indexes_lang: from . import indexes as idxs indexer = idxs.languages.get(indexes_lang, None) if not indexer: msg = "extended indexes not supported for the specified language: %s.\n"\ "following languages avaible: %s." %\ (indexes_lang, ', '.join(list(idxs.languages.keys()))) from pyglossary.glossary import log log.error(msg) raise ValueError(msg) def generate_indexes(title, alts, content, BeautifulSoup): indexes = [title] indexes.extend(alts) if BeautifulSoup: quoted_title = BeautifulSoup.dammit.EntitySubstitution.substitute_xml( title, True) else: quoted_title = '"%s"' % title.replace('>', '>').replace( '"', """) if indexer: indexes = set(indexer(indexes, content)) normal_indexes = set() for idx in indexes: normal = _normalize.title(idx, BeautifulSoup) normal_indexes.add(_normalize.title_long(normal)) normal_indexes.add(_normalize.title_short(normal)) normal_indexes.discard(title) normal_indexes = [s for s in normal_indexes if s.strip()] # skip empty titles. everything could happen. s = '<d:index d:value=%s d:title=%s/>' % (quoted_title, quoted_title) if BeautifulSoup: for idx in normal_indexes: s += '<d:index d:value=%s d:title=%s/>' % ( BeautifulSoup.dammit.EntitySubstitution.substitute_xml( idx, True), quoted_title) else: for idx in normal_indexes: s += '<d:index d:value="%s" d:title=%s/>' % (idx.replace( '>', '>').replace('"', """), quoted_title) return s return generate_indexes
def safe_listdir_set(path): """ :rtype: set """ if not path: return set() if not os.path.isdir(path): from pyglossary.glossary import log log.error("resource path is not a directory: %r" % path) return set() return {os.path.join(path, node) for node in os.listdir(path)}
def indexes_generator(indexes_lang): """ factory that acts according to glossary language :param indexes_lang: str """ indexer = None """Callable[[Sequence[str], str], Sequence[str]]""" if indexes_lang: from . import indexes as idxs indexer = idxs.languages.get(indexes_lang, None) if not indexer: msg = "extended indexes not supported for the specified language: %s.\n"\ "following languages avaible: %s." %\ (indexes_lang, ', '.join(list(idxs.languages.keys()))) from pyglossary.glossary import log log.error(msg) raise ValueError(msg) def generate_indexes(title, alts, content, BeautifulSoup): indexes = [title] indexes.extend(alts) if BeautifulSoup: quoted_title = BeautifulSoup.dammit.EntitySubstitution.substitute_xml(title, True) else: quoted_title = '"%s"' % title.replace('>', '>').replace('"', """) if indexer: indexes = set(indexer(indexes, content)) normal_indexes = set() for idx in indexes: normal = _normalize.title(idx, BeautifulSoup) normal_indexes.add(_normalize.title_long(normal)) normal_indexes.add(_normalize.title_short(normal)) normal_indexes.discard(title) normal_indexes = [s for s in normal_indexes if s.strip()] # skip empty titles. everything could happen. s = '<d:index d:value=%s d:title=%s/>' % (quoted_title, quoted_title) if BeautifulSoup: for idx in normal_indexes: s += '<d:index d:value=%s d:title=%s/>' % ( BeautifulSoup.dammit.EntitySubstitution.substitute_xml(idx, True), quoted_title) else: for idx in normal_indexes: s += '<d:index d:value="%s" d:title=%s/>' % ( idx.replace('>', '>').replace('"', """), quoted_title) return s return generate_indexes
def format_default_prefs(defaultPrefs): """ :type defaultPrefs: dict or None as by 14th of Jan 2016, it is highly recommended that prefs should contain {'version': '1'}, otherwise Dictionary.app does not keep user changes between restarts. """ if not defaultPrefs: return "" if not isinstance(defaultPrefs, dict): raise TypeError("defaultPrefs not a dictionary: %r" % defaultPrefs) if str(defaultPrefs.get('version', None)) != '1': from pyglossary.glossary import log log.error("default prefs does not contain {'version': '1'}. prefs " "will not be persistent between Dictionary.app restarts.") return "\n".join("\t\t<key>%s</key>\n\t\t<string>%s</string>" % i for i in sorted(defaultPrefs.items())).strip()
__all__ = ['languages', 'log'] languages = {} """ Dict[str, Callable[[Sequence[str], str], Sequence[str]]] submodules must register languages by adding (language name -> function) pairs to the mapping. function must follow signature bellow: :param titles: flat iterable of title and altenrative titles :param content: cleaned entry content :return: iterable of indexes (str). use ``` from . import languages # or from appledict.indexes import languages ``` """ here = os.path.dirname(os.path.abspath(__file__)) for _, module, _ in pkgutil.iter_modules([here]): try: __import__('%s.%s' % (__name__, module)) except ImportError: log.error("error while importing indexes plugin %s" % module, exc_info=1)