From 808b624e5f5d7fe539863949dd7d0c04c6ce0dc7 Mon Sep 17 00:00:00 2001 From: Tobias Gruetzmacher Date: Sat, 11 Jul 2015 01:23:20 +0200 Subject: [PATCH] Remove hard dependency on pycountry again. This basically reverts commit 86b31dc12b8938655535320417831b647e62ee57. 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. --- dosage | 8 +------- dosagelib/languages.py | 10 ++++++++++ dosagelib/scraper.py | 26 +++++++++++++++++++++++++- requirements.txt | 1 - scripts/mklanguages.py | 41 +++++++++++++++++++++++++++++++++++++++++ setup.py | 1 - 6 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 dosagelib/languages.py create mode 100755 scripts/mklanguages.py 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"],