From d0e166140f79350c96700d15bc9b52b812a668a8 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Mon, 16 Sep 2024 17:21:39 +0200 Subject: [PATCH] Fix import pycountry on python 3.12 without warning Fix shadowed "babel" name --- cps/__init__.py | 3 ++- cps/about.py | 4 +--- cps/admin.py | 2 +- cps/{babel.py => cw_babel.py} | 0 cps/isoLanguages.py | 19 ++++++------------- cps/web.py | 2 +- 6 files changed, 11 insertions(+), 19 deletions(-) rename cps/{babel.py => cw_babel.py} (100%) diff --git a/cps/__init__.py b/cps/__init__.py index f72dcfda..0ad47aaf 100644 --- a/cps/__init__.py +++ b/cps/__init__.py @@ -34,7 +34,7 @@ from .cli import CliParameter from .reverseproxy import ReverseProxied from .server import WebServer from .dep_check import dependency_check -from .babel import babel, get_locale +# from .babel import babel, get_locale from .updater import Updater from . import config_sql from . import cache_buster @@ -182,6 +182,7 @@ def create_app(): app.secret_key = os.getenv('SECRET_KEY', config_sql.get_flask_session_key(ub.session)) web_server.init_app(app, config) + from .cw_babel import babel, get_locale if hasattr(babel, "localeselector"): babel.init_app(app) babel.localeselector(get_locale) diff --git a/cps/about.py b/cps/about.py index 4a170447..b43c97b9 100644 --- a/cps/about.py +++ b/cps/about.py @@ -42,10 +42,8 @@ opt = dep_check.load_dependencies(True) for i in (req + opt): modules[i[1]] = i[0] modules['Jinja2'] = importlib.metadata.version("jinja2") -try: +if sys.version_info < (3, 12): modules['pySqlite'] = sqlite3.version -except Exception: - pass modules['SQLite'] = sqlite3.sqlite_version sorted_modules = OrderedDict((sorted(modules.items(), key=lambda x: x[0].casefold()))) diff --git a/cps/admin.py b/cps/admin.py index a8eaafa1..367d9359 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -52,7 +52,7 @@ from .gdriveutils import is_gdrive_ready, gdrive_support from .render_template import render_title_template, get_sidebar_config from .services.worker import WorkerThread from .usermanagement import user_login_required -from .babel import get_available_translations, get_available_locale, get_user_locale_language +from .cw_babel import get_available_translations, get_available_locale, get_user_locale_language from . import debug_info from .string_helper import strip_whitespaces diff --git a/cps/babel.py b/cps/cw_babel.py similarity index 100% rename from cps/babel.py rename to cps/cw_babel.py diff --git a/cps/isoLanguages.py b/cps/isoLanguages.py index bbab7089..c7c869bc 100644 --- a/cps/isoLanguages.py +++ b/cps/isoLanguages.py @@ -25,26 +25,14 @@ log = logger.create() try: - from iso639 import languages - # iso_version = importlib.metadata.version("iso639") - get = languages.get - try: - if sys.version_info >= (3, 12): - import pkg_resources - except ImportError: - print("Python 3.12 isn't compatible with iso-639. Please install pycountry.") -except ImportError as ex: from pycountry import languages as pyc_languages - #try: - # iso_version = importlib.metadata.version("pycountry") + ' (PyCountry)' - #except (ImportError, Exception): - # iso_version = "?" + ' (PyCountry)' def _copy_fields(l): l.part1 = getattr(l, 'alpha_2', None) l.part3 = getattr(l, 'alpha_3', None) return l + def get(name=None, part1=None, part3=None): if part3 is not None: return _copy_fields(pyc_languages.get(alpha_3=part3)) @@ -52,6 +40,11 @@ except ImportError as ex: return _copy_fields(pyc_languages.get(alpha_2=part1)) if name is not None: return _copy_fields(pyc_languages.get(name=name)) +except ImportError as ex: + if sys.version_info >= (3, 12): + print("Python 3.12 isn't compatible with iso-639. Please install pycountry.") + from iso639 import languages + get = languages.get def get_language_names(locale): diff --git a/cps/web.py b/cps/web.py index afc4669a..e8905929 100644 --- a/cps/web.py +++ b/cps/web.py @@ -52,7 +52,7 @@ from .helper import check_valid_domain, check_email, check_username, \ edit_book_read_status, valid_password from .pagination import Pagination from .redirect import get_redirect_location -from .babel import get_available_locale +from .cw_babel import get_available_locale from .usermanagement import login_required_if_no_ano from .kobo_sync_status import remove_synced_book from .render_template import render_title_template