koehlma / pygtkspellcheck

A simple but quite powerful spellchecking library for GTK written in pure Python.
GNU General Public License v3.0
23 stars 9 forks source link

Exception on unknown country code #22

Closed jaap-karssenberg closed 8 years ago

jaap-karssenberg commented 8 years ago

I got the exception below when running the simple example unmodified. The reason is that I still have discitonaries installed that use the country code "AN". A quick check on wikipedia learns that this was a valid code but has been changed. Although it is not a valid code any more I believe it is a reasonable assumption that this will happen more often and in general it is good to make the code robust for this kind of inconsistency.

Below an alternative for "from_broker" that does work for me.

Regards,

Jaap

Suggested alternative

@classmethod
def from_broker(cls, broker):
    lang = []
    for language in broker.list_languages():
        try:
           lang.append((language, code_to_name(language)))
        except:
           pass # TODO - log exception with logging?

    return cls(sorted(lang, key=lambda language: language[1]))

Error log

jaap@Fantomes:~/code/zim-trunk$ python ~/Downloads/simple_pygtk.py Traceback (most recent call last):
  File "/home/jaap/Downloads/simple_pygtk.py", line 38, in <module>
    spellchecker = SpellChecker(view, locale.getdefaultlocale()[0])
  File "/usr/lib/python2.7/dist-packages/gtkspellcheck/spellcheck.py", line 165, in __init__
    self.languages = SpellChecker._LanguageList.from_broker(self._broker)
  File "/usr/lib/python2.7/dist-packages/gtkspellcheck/spellcheck.py", line 118, in from_broker
    for language in broker.list_languages()],
  File "/usr/lib/python2.7/dist-packages/pylocales/locales.py", line 135, in code_to_name
    country = Country.by_alpha_2(code[1]).translation
  File "/usr/lib/python2.7/dist-packages/pylocales/locales.py", line 88, in by_alpha_2
    return Country.get_country(code, 'alpha_2')
  File "/usr/lib/python2.7/dist-packages/pylocales/locales.py", line 84, in get_country
    raise CountryNotFound('code: %s, codec: %s' % (code, codec))
pylocales.locales.CountryNotFound: code: AN, codec: alpha_2
jaap@Fantomes:~/code/zim-trunk$ 
koehlma commented 8 years ago

Hi there, which version of pygtkspellcheck do you use? It seems to me that this is the same problem as reported in issue #17 and it should be fixed since commit afabf77cf63027f24409c3749e530c69bd6c882f.

jaap-karssenberg commented 8 years ago

I'm pulling from your Ubuntu PPA.

Indeed this fix is not in the version I'm trying -- so PPA probably not up to date.

Cheers!

jaap

koehlma commented 8 years ago

Yeah, thanks for pointing out the outdated PPA, I removed the link from PyPI and my website. Pygtkspellcheck is available in the official repositories of Debian/Ubuntu and Archlinux although Debian/Ubuntu have an outdated version too.

jaap-karssenberg commented 8 years ago

I removed the PPA and installed the version from Ubuntu repository: python-gtkspellcheck (3.0-1.1)

Afraid fix is also not in that version. Looks like Debian / Ubuntu repository is still shipping your 2012 version.

REgards,

Jaap