diff --git a/dosage b/dosage index dda573752..e7a914b6a 100755 --- a/dosage +++ b/dosage @@ -15,7 +15,6 @@ import os import argparse import pydoc from io import StringIO -import pycountry from dosagelib import events, configuration, singleton, director from dosagelib.output import out from dosagelib.util import internal_error, strlimit @@ -154,12 +153,7 @@ def displayComicHelp(scraperobj): out.context = scraperobj.getName() try: out.info(u"URL: " + scraperobj.url) - if scraperobj.lang: - 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) + out.info(u"Language: " + scraperobj.language()) if scraperobj.adult: out.info(u"Adult comic, use option --adult to fetch.") disabled = scraperobj.getDisabledReasons() diff --git a/dosagelib/languages.py b/dosagelib/languages.py new file mode 100644 index 000000000..fcdeac584 --- /dev/null +++ b/dosagelib/languages.py @@ -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', +} diff --git a/dosagelib/scraper.py b/dosagelib/scraper.py index 2bc6fddf4..835afc53b 100644 --- a/dosagelib/scraper.py +++ b/dosagelib/scraper.py @@ -22,7 +22,12 @@ try: except ImportError: 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, getDirname, unescape, tagre, normaliseURL, prettyMatcherList) from .comic import ComicStrip @@ -309,6 +314,25 @@ class Scraper(object): """ 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): """ diff --git a/requirements.txt b/requirements.txt index 588709337..b86af7c1b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ # required: requests -pycountry # optional: argcomplete lxml diff --git a/scripts/mklanguages.py b/scripts/mklanguages.py new file mode 100755 index 000000000..6e243060c --- /dev/null +++ b/scripts/mklanguages.py @@ -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() diff --git a/setup.py b/setup.py index fe41931b1..e6b8091c1 100644 --- a/setup.py +++ b/setup.py @@ -56,7 +56,6 @@ setup( ), install_requires = ( 'requests', - 'pycountry', ), extras_require = { 'xpath': ["lxml"],