From 4743dc677fdbcd479665b09a75bcb35fe8a7b2c3 Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Mon, 14 Feb 2022 16:13:25 -0800 Subject: [PATCH 1/3] Use general purpose logging config in env logging. --- kolibri/utils/env.py | 18 +++++++++++++++++- kolibri/utils/logger.py | 16 +++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/kolibri/utils/env.py b/kolibri/utils/env.py index 88b743acbba..f48907677ac 100644 --- a/kolibri/utils/env.py +++ b/kolibri/utils/env.py @@ -3,9 +3,25 @@ import platform import sys +from colorlog import ColoredFormatter +from colorlog import getLogger +from colorlog import StreamHandler + +from .logger import LOG_COLORS + + logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO) logging.StreamHandler(sys.stdout) +handler = StreamHandler() +handler.setFormatter( + ColoredFormatter( + fmt="%(log_color)s%(levelname)-8s %(message)s", log_colors=LOG_COLORS + ) +) +logger = getLogger("env") +logger.addHandler(handler) + def settings_module(): from .build_config.default_settings import settings_path @@ -68,7 +84,7 @@ def prepend_cext_path(dist_path): # add it + the matching noarch (OpenSSL) modules to sys.path sys.path = [str(dirname), str(noarch_dir)] + sys.path else: - logging.info("No C extensions are available for this platform") + logger.info("No C extensions are available for this platform") def set_env(): diff --git a/kolibri/utils/logger.py b/kolibri/utils/logger.py index 3f3730ed47b..a23aeb71f19 100644 --- a/kolibri/utils/logger.py +++ b/kolibri/utils/logger.py @@ -8,6 +8,14 @@ NO_FILE_BASED_LOGGING = os.environ.get("KOLIBRI_NO_FILE_BASED_LOGGING", False) +LOG_COLORS = { + "DEBUG": "blue", + "INFO": "white", + "WARNING": "yellow", + "ERROR": "red", + "CRITICAL": "bold_red", +} + class KolibriTimedRotatingFileHandler(TimedRotatingFileHandler): """ @@ -143,13 +151,7 @@ def get_default_logging_config(LOG_ROOT, debug=False, debug_database=False): "color": { "()": "colorlog.ColoredFormatter", "format": "%(log_color)s%(levelname)-8s %(message)s", - "log_colors": { - "DEBUG": "blue", - "INFO": "white", - "WARNING": "yellow", - "ERROR": "red", - "CRITICAL": "bold_red", - }, + "log_colors": LOG_COLORS, }, }, "handlers": { From 0db5af97bc3d2ece6e47d3ddb897e49e3a4d14fd Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Mon, 14 Feb 2022 16:15:29 -0800 Subject: [PATCH 2/3] Add logged warning and warning warning for deprecation of Python 3.4 and 3.5. --- kolibri/utils/env.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/kolibri/utils/env.py b/kolibri/utils/env.py index f48907677ac..0e72939a428 100644 --- a/kolibri/utils/env.py +++ b/kolibri/utils/env.py @@ -2,6 +2,7 @@ import os import platform import sys +from warnings import warn from colorlog import ColoredFormatter from colorlog import getLogger @@ -87,6 +88,15 @@ def prepend_cext_path(dist_path): logger.info("No C extensions are available for this platform") +def check_python_versions(): + if sys.version_info.major == 3 and ( + sys.version_info.minor == 4 or sys.version_info.minor == 5 + ): + warning_text = "Python 3.4 and 3.5 support will be dropped in Kolibri 0.16, please upgrade your Python version" + logger.warn(warning_text) + warn(warning_text, DeprecationWarning) + + def set_env(): """ Sets the Kolibri environment for the CLI or other application worker @@ -98,6 +108,8 @@ def set_env(): """ from kolibri import dist as kolibri_dist # noqa + check_python_versions() + sys.path = [os.path.realpath(os.path.dirname(kolibri_dist.__file__))] + sys.path # Add path for c extensions to sys.path From bed1d14b17fc8efb7eb55cd520c1359de0d4bb39 Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Mon, 14 Feb 2022 17:42:49 -0800 Subject: [PATCH 3/3] Allow env to be invoked when no dependencies installed. --- kolibri/utils/env.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/kolibri/utils/env.py b/kolibri/utils/env.py index 0e72939a428..37859abcaf6 100644 --- a/kolibri/utils/env.py +++ b/kolibri/utils/env.py @@ -4,9 +4,18 @@ import sys from warnings import warn -from colorlog import ColoredFormatter -from colorlog import getLogger -from colorlog import StreamHandler +try: + # Do this to allow this to be accessed + # during build, when dependencies are not + # installed. + # TODO: Move version tools to build tools, so we don't have to do this + from colorlog import ColoredFormatter + from colorlog import getLogger + from colorlog import StreamHandler +except ImportError: + StreamHandler = None + getLogger = None + ColoredFormatter = None from .logger import LOG_COLORS @@ -14,14 +23,17 @@ logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO) logging.StreamHandler(sys.stdout) -handler = StreamHandler() -handler.setFormatter( - ColoredFormatter( - fmt="%(log_color)s%(levelname)-8s %(message)s", log_colors=LOG_COLORS +if StreamHandler and getLogger and ColoredFormatter: + handler = StreamHandler() + handler.setFormatter( + ColoredFormatter( + fmt="%(log_color)s%(levelname)-8s %(message)s", log_colors=LOG_COLORS + ) ) -) -logger = getLogger("env") -logger.addHandler(handler) + logger = getLogger("env") + logger.addHandler(handler) +else: + logger = logging.getLogger("env") def settings_module():