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:
parent
d97a9c63e4
commit
808b624e5f
6 changed files with 77 additions and 10 deletions
8
dosage
8
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()
|
||||
|
|
10
dosagelib/languages.py
Normal file
10
dosagelib/languages.py
Normal 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',
|
||||
}
|
|
@ -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):
|
||||
"""
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
# required:
|
||||
requests
|
||||
pycountry
|
||||
# optional:
|
||||
argcomplete
|
||||
lxml
|
||||
|
|
41
scripts/mklanguages.py
Executable file
41
scripts/mklanguages.py
Executable 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()
|
1
setup.py
1
setup.py
|
@ -56,7 +56,6 @@ setup(
|
|||
),
|
||||
install_requires = (
|
||||
'requests',
|
||||
'pycountry',
|
||||
),
|
||||
extras_require = {
|
||||
'xpath': ["lxml"],
|
||||
|
|
Loading…
Reference in a new issue