From f7e640fd98387f494393ceaec29b6554e528c9ca Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Sat, 2 Mar 2019 03:13:43 +0200 Subject: [PATCH 1/2] support pycountry if iso-639 is not available --- cps/epub.py | 2 +- cps/isoLanguages.py | 28 ++++++++++++++++++++++++++++ cps/web.py | 5 ++--- 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 cps/isoLanguages.py diff --git a/cps/epub.py b/cps/epub.py index 76d74793..913feaca 100644 --- a/cps/epub.py +++ b/cps/epub.py @@ -21,7 +21,7 @@ import zipfile from lxml import etree import os import uploader -from iso639 import languages as isoLanguages +import isoLanguages def extractCover(zipFile, coverFile, coverpath, tmp_file_name): diff --git a/cps/isoLanguages.py b/cps/isoLanguages.py new file mode 100644 index 00000000..5190d8b7 --- /dev/null +++ b/cps/isoLanguages.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +try: + from iso639 import languages, __version__ + get = languages.get +except ImportError: + from pycountry import languages as pyc_languages + try: + import pkg_resources + __version__ = pkg_resources.get_distribution('pycountry').version + ' (PyCountry)' + del pkg_resources + except: + __version__ = "? (PyCountry)" + + def _copy_fields(l): + l.part1 = l.alpha_2 + l.part3 = l.alpha_3 + return l + + def get(name=None, part1=None, part3=None): + if (part3 is not None): + return _copy_fields(pyc_languages.get(alpha_3=part3)) + if (part1 is not None): + return _copy_fields(pyc_languages.get(alpha_2=part1)) + if (name is not None): + return _copy_fields(pyc_languages.get(name=name)) diff --git a/cps/web.py b/cps/web.py index 4bc0267c..4cc82f4b 100644 --- a/cps/web.py +++ b/cps/web.py @@ -61,8 +61,7 @@ import base64 from sqlalchemy.sql import * import json import datetime -from iso639 import languages as isoLanguages -from iso639 import __version__ as iso639Version +import isoLanguages from pytz import __version__ as pytzVersion from uuid import uuid4 import os.path @@ -1657,7 +1656,7 @@ def stats(): versions['Flask'] = 'v' + flaskVersion versions['Flask Login'] = 'v' + flask_loginVersion versions['Flask Principal'] = 'v' + flask_principalVersion - versions['Iso 639'] = 'v' + iso639Version + versions['Iso 639'] = 'v' + isoLanguages.__version__ versions['pytz'] = 'v' + pytzVersion versions['Requests'] = 'v' + requests.__version__ From 858c80bb207e8bafef17f79cf7e6aa949cf663fa Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Fri, 29 Mar 2019 17:06:26 +0200 Subject: [PATCH 2/2] 'except' style fix --- cps/isoLanguages.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cps/isoLanguages.py b/cps/isoLanguages.py index 5190d8b7..0d300ea0 100644 --- a/cps/isoLanguages.py +++ b/cps/isoLanguages.py @@ -11,7 +11,7 @@ except ImportError: import pkg_resources __version__ = pkg_resources.get_distribution('pycountry').version + ' (PyCountry)' del pkg_resources - except: + except (ImportError, Exception): __version__ = "? (PyCountry)" def _copy_fields(l):