Remove hard dependency on pycountry again.

This basically reverts commit 86b31dc12b.

It now works like this: If the use has pycountry installed, it is used.
If not, Dosage falls back to a small internal list generated from
pycountry by scripts/mklanguages.py.

This means additional work if we ever decide to translate Dosage, since
pycountry already has all the translations for language names...

This fixes #23.
This commit is contained in:
Tobias Gruetzmacher 2015-07-11 01:23:20 +02:00
parent d97a9c63e4
commit 808b624e5f
6 changed files with 77 additions and 10 deletions

8
dosage
View file

@ -15,7 +15,6 @@ import os
import argparse import argparse
import pydoc import pydoc
from io import StringIO from io import StringIO
import pycountry
from dosagelib import events, configuration, singleton, director from dosagelib import events, configuration, singleton, director
from dosagelib.output import out from dosagelib.output import out
from dosagelib.util import internal_error, strlimit from dosagelib.util import internal_error, strlimit
@ -154,12 +153,7 @@ def displayComicHelp(scraperobj):
out.context = scraperobj.getName() out.context = scraperobj.getName()
try: try:
out.info(u"URL: " + scraperobj.url) out.info(u"URL: " + scraperobj.url)
if scraperobj.lang: out.info(u"Language: " + scraperobj.language())
try:
lang = pycountry.languages.get(alpha2 = scraperobj.lang)
except KeyError:
lang = pycountry.languages.get(iso639_1_code = scraperobj.lang)
out.info(u"Language: " + lang.name)
if scraperobj.adult: if scraperobj.adult:
out.info(u"Adult comic, use option --adult to fetch.") out.info(u"Adult comic, use option --adult to fetch.")
disabled = scraperobj.getDisabledReasons() disabled = scraperobj.getDisabledReasons()

10
dosagelib/languages.py Normal file
View file

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
# ISO 693-1 language codes from pycountry
# This file is automatically generated, DO NOT EDIT!
Languages = {
'de': u'German',
'en': u'English',
'es': u'Spanish; Castilian',
'fi': u'Finnish',
'fr': u'French',
}

View file

@ -22,7 +22,12 @@ try:
except ImportError: except ImportError:
cssselect = None cssselect = None
from . import loader, configuration, util try:
import pycountry
except ImportError:
pycountry = None
from . import loader, configuration, util, languages
from .util import (getPageContent, makeSequence, get_system_uid, urlopen, from .util import (getPageContent, makeSequence, get_system_uid, urlopen,
getDirname, unescape, tagre, normaliseURL, prettyMatcherList) getDirname, unescape, tagre, normaliseURL, prettyMatcherList)
from .comic import ComicStrip from .comic import ComicStrip
@ -309,6 +314,25 @@ class Scraper(object):
""" """
return {} return {}
@classmethod
def language(cls):
"""
Return language of the comic as a human-readable language name instead
of a 2-character ISO639-1 code.
"""
lang = 'Unknown (%s)' % cls.lang
if pycountry is None:
if cls.lang in languages.Languages:
lang = languages.Languages[cls.lang]
else:
try:
lang = pycountry.languages.get(alpha2 = cls.lang).name
except KeyError:
try:
lang = pycountry.languages.get(iso639_1_code = cls.lang).name
except KeyError:
pass
return lang
class _BasicScraper(Scraper): class _BasicScraper(Scraper):
""" """

View file

@ -1,6 +1,5 @@
# required: # required:
requests requests
pycountry
# optional: # optional:
argcomplete argcomplete
lxml lxml

41
scripts/mklanguages.py Executable file
View file

@ -0,0 +1,41 @@
#!/usr/bin/python
# update languages.py from pycountry
import os
import sys
import codecs
basepath = os.path.dirname(os.path.dirname(__file__))
sys.path.append(basepath)
from dosagelib.scraper import get_scraperclasses
def main():
"""Update language information in dosagelib/languages.py."""
fn = os.path.join(basepath, 'dosagelib', 'languages.py')
encoding = 'utf-8'
with codecs.open(fn, 'w', encoding) as f:
f.write('# -*- coding: %s -*-%s' % (encoding, os.linesep))
f.write('# ISO 693-1 language codes from pycountry%s' % os.linesep)
f.write('# This file is automatically generated, DO NOT EDIT!%s' % os.linesep)
lang = get_used_languages()
write_languages(f, lang)
def get_used_languages():
lang = {}
for scraperclass in get_scraperclasses():
l = scraperclass.lang
if l not in lang:
lang[l] = scraperclass.language()
return lang
def write_languages(f, l):
"""Write language information."""
f.write("Languages = {%s" % os.linesep)
for lang in sorted(l):
f.write(" %r: %r,%s" % (lang, l[lang], os.linesep))
f.write("}%s" % os.linesep)
if __name__ == '__main__':
main()

View file

@ -56,7 +56,6 @@ setup(
), ),
install_requires = ( install_requires = (
'requests', 'requests',
'pycountry',
), ),
extras_require = { extras_require = {
'xpath': ["lxml"], 'xpath': ["lxml"],