Esempio n. 1
0
    def addCustomDict (self, customDictPath):
        try:
            self._createCustomDictLang (self._folders[-1])
        except IOError:
            pass

        key = (CUSTOM_DICT_LANG, customDictPath)

        if key not in self._dictCache:
            broker = Broker ()
            broker.set_param ('enchant.myspell.dictionary.path',
                              self._folders[-1])

            try:
                currentDict = DictWithPWL (CUSTOM_DICT_LANG,
                                           customDictPath,
                                           broker=broker)
            except enchant.errors.Error:
                return

            self._dictCache[key] = currentDict
        else:
            currentDict = self._dictCache[key]

        self._customCheckers.append (currentDict)
Esempio n. 2
0
    def addCustomDict(self, customDictPath):
        try:
            self._createCustomDictLang(self._folders[-1])
        except IOError as err:
            logger.error("Can't create custom dictionary")

        key = (ENCHANT_CUSTOM_DICT_LANG, customDictPath)

        if key not in self._dictCache:
            broker = Broker()
            broker.set_param('enchant.myspell.dictionary.path',
                             self._folders[-1])

            try:
                currentDict = DictWithPWL(ENCHANT_CUSTOM_DICT_LANG,
                                          customDictPath,
                                          broker=broker)
            except enchant.errors.Error as err:
                logger.error('Custom dictionary error. path={}; lang={}'.format(customDictPath, key))
                logger.error(err)
                return

            self._dictCache[key] = currentDict
        else:
            currentDict = self._dictCache[key]

        self._customCheckers.append(currentDict)
Esempio n. 3
0
    def addCustomDict(self, customDictPath):
        try:
            self._createCustomDictLang(self._folders[-1])
        except IOError:
            pass

        key = (CUSTOM_DICT_LANG, customDictPath)

        if key not in self._dictCache:
            broker = Broker()
            broker.set_param('enchant.myspell.dictionary.path',
                             self._folders[-1])

            try:
                currentDict = DictWithPWL(CUSTOM_DICT_LANG,
                                          customDictPath,
                                          broker=broker)
            except enchant.errors.Error:
                return

            self._dictCache[key] = currentDict
        else:
            currentDict = self._dictCache[key]

        self._customCheckers.append(currentDict)
Esempio n. 4
0
    def _init_checker(self, lang='en_US'):
        """
        Initialize a checker of selected language if it is not yet present
        lang: language to initialize the dictionary
        """

        # C language means English
        if lang == 'C':
            lang = 'en_US'

        # test if we actually have working enchant
        if not ENCHANT:
            print_warning(
                '(none): W: unable to init enchant, spellchecking disabled.')
            return

        # there might not be myspell/aspell/etc dicts present
        broker = Broker()
        if not broker.dict_exists(lang):
            print_warning(
                f'(none): W: unable to load spellchecking dictionary for {lang}.'
            )
            return

        if lang not in self._enchant_checkers:
            checker = SpellChecker(
                lang, filters=[EmailFilter, URLFilter, WikiWordFilter])
            self._enchant_checkers[lang] = checker
Esempio n. 5
0
    def addCustomDict(self, customDictPath):
        try:
            self._createCustomDictLang(self._folders[-1])
        except IOError as err:
            logger.error("Can't create custom dictionary")

        key = (CUSTOM_DICT_LANG, customDictPath)

        if key not in self._dictCache:
            broker = Broker()
            broker.set_param('enchant.myspell.dictionary.path',
                             self._folders[-1])

            try:
                currentDict = DictWithPWL(CUSTOM_DICT_LANG,
                                          customDictPath,
                                          broker=broker)
            except enchant.errors.Error as err:
                logger.error('Custom dictionary error. path={}; lang={}'.format(customDictPath, key))
                logger.error(err)
                return

            self._dictCache[key] = currentDict
        else:
            currentDict = self._dictCache[key]

        self._customCheckers.append(currentDict)
Esempio n. 6
0
def test_GetSetParam(broker):
    # Older enchnt versions do not have these functions.
    if not hasattr(_e.broker_get_param, "argtypes"):
        return
    assert broker.get_param("pyenchant.unittest") is None
    broker.set_param("pyenchant.unittest", "testing")
    assert broker.get_param("pyenchant.unittest") == "testing"
    other_broker = Broker()
    assert other_broker.get_param("pyenchant.unittest") is None
Esempio n. 7
0
    def _getDict(self, lang, path):
        key = (lang, path)
        if key not in self._dictCache:
            broker = Broker()
            broker.set_param('enchant.myspell.dictionary.path', path)
            currentDict = Dict(lang, broker)
            self._dictCache[key] = currentDict
        else:
            currentDict = self._dictCache[key]

        return currentDict
Esempio n. 8
0
    def _getDict (self, lang, path):
        key = (lang, path)
        if key not in self._dictCache:
            broker = Broker ()
            broker.set_param ('enchant.myspell.dictionary.path', path)
            currentDict = Dict (lang, broker)
            self._dictCache[key] = currentDict
        else:
            currentDict = self._dictCache[key]

        return currentDict
Esempio n. 9
0
def test_get_set_param(broker):
    """
    Scenario:
      Either broker.set_param(key, value) works
      Or broker.set_param(key, value) throw
      AttributeError

    """
    key = "pyenchant.unittest"
    value = "testing"

    error = None
    try:
        broker.set_param(key, value)
    except AttributeError as e:
        error = e

    if error:
        return
    else:
        assert broker.get_param("pyenchant.unittest") == "testing"
        other_broker = Broker()
        assert other_broker.get_param("pyenchant.unittest") is None
Esempio n. 10
0
    def __init__(self, path, wl_dir, chunkers, filters):
        self.popath = path
        self.po = polib.pofile(path)
        self.lang = self.po.metadata["Language"]

        available_lang = Broker().list_languages()
        if self.lang not in available_lang:
            baselang = self.lang.split("_")[0]
            if baselang in available_lang:
                self.lang = baselang
            else:
                print("Dictionary for language '%s' could not be found." % self.lang)
                raise(errors.DictNotFoundError)

        wordlist = Check.get_wordlist(self.lang, wl_dir, path)
        try:
            check_dict = DictWithPWL(self.lang, pwl=wordlist)
        except errors.Error as e:
            check_dict = Dict(self.lang)
            print(e)
        self.checker = SpellChecker(check_dict, chunkers=chunkers, filters=filters)
Esempio n. 11
0
def broker():
    res = Broker()
    yield res
    del res
Esempio n. 12
0
def test_prov_ordering(broker):
    """Test that provider ordering works correctly."""
    langs = {}
    provs = []
    # Find the providers for each language, and a list of all providers
    for (tag, prov) in broker.list_dicts():
        # Skip hyphenation dictionaries installed by OOo
        if tag.startswith("hyph_") and prov.name == "myspell":
            continue
        # Canonicalize separators
        tag = tag.replace("-", "_")
        langs[tag] = []
        # NOTE: we are excluding Zemberek here as it appears to return
        #       a broker for any language, even nonexistent ones
        if prov not in provs and prov.name != "zemberek":
            provs.append(prov)
    for prov in provs:
        for tag in langs:
            b2 = Broker()
            b2.set_ordering(tag, prov.name)
            try:
                d = b2.request_dict(tag)
                if d.provider != prov:
                    raise ValueError()
                langs[tag].append(prov)
            # TODO: bare except
            except:  # noqa
                pass
    # Check availability using a single entry in ordering
    for tag in langs:
        for prov in langs[tag]:
            b2 = Broker()
            b2.set_ordering(tag, prov.name)
            d = b2.request_dict(tag)
            assert (d.provider, tag) == (prov, tag)
            del d
            del b2
    # Place providers that don't have the language in the ordering
    for tag in langs:
        for prov in langs[tag]:
            order = prov.name
            for prov2 in provs:
                if prov2 not in langs[tag]:
                    order = prov2.name + "," + order
            b2 = Broker()
            b2.set_ordering(tag, order)
            d = b2.request_dict(tag)
            assert (d.provider, tag, order) == (prov, tag, order)
            del d
            del b2