From 5c239df54658e534e974bce91417dff2f686d7c8 Mon Sep 17 00:00:00 2001 From: farhan Date: Wed, 27 Sep 2023 12:20:59 +0500 Subject: [PATCH] chore: add deprecation warnings on use of deprecated package import --- xblockutils/__init__.py | 2 +- xblockutils/deprecation/__init__.py | 0 xblockutils/deprecation/warn.py | 34 +++++++++++++++++++++++++++++ xblockutils/helpers.py | 7 ++++++ xblockutils/publish_event.py | 7 ++++++ xblockutils/resources.py | 11 +++++++--- xblockutils/settings.py | 6 +++++ xblockutils/studio_editable.py | 14 ++++++++---- xblockutils/templatetags/i18n.py | 6 +++++ 9 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 xblockutils/deprecation/__init__.py create mode 100644 xblockutils/deprecation/warn.py diff --git a/xblockutils/__init__.py b/xblockutils/__init__.py index ac57d0f..97599e3 100644 --- a/xblockutils/__init__.py +++ b/xblockutils/__init__.py @@ -2,4 +2,4 @@ Useful classes and functionality for building and testing XBlocks """ -__version__ = '3.4.1' +__version__ = '4.0.0' diff --git a/xblockutils/deprecation/__init__.py b/xblockutils/deprecation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xblockutils/deprecation/warn.py b/xblockutils/deprecation/warn.py new file mode 100644 index 0000000..1475602 --- /dev/null +++ b/xblockutils/deprecation/warn.py @@ -0,0 +1,34 @@ +""" +Utilities for warning about the use of deprecated package + +See https://github.com/openedx/xblock-utils/issues/197 for details. +""" + +import warnings + + +class DeprecatedPackageWarning(DeprecationWarning): + """ + A warning that a deprecated package is being used. + """ + + def __init__(self, old_import, new_import): + super().__init__() + self.old_import = old_import + self.new_import = new_import + + def __str__(self): + return ( + "Please use import {self.new_import} instead of {self.old_import} as " + "'xblock-utils' package has been deprecated and migrated within 'xblock' package. " + ).format(self=self) + + +def warn_deprecated_package(old_import, new_import): + """ + Warn that a package has been deprecated + """ + warnings.warn( + DeprecatedPackageWarning(old_import, new_import), + stacklevel=3, # Should surface the line that is doing the importing. + ) diff --git a/xblockutils/helpers.py b/xblockutils/helpers.py index 941daa3..b31773f 100644 --- a/xblockutils/helpers.py +++ b/xblockutils/helpers.py @@ -2,6 +2,13 @@ Useful helper methods """ +from xblockutils.deprecation.warn import warn_deprecated_package + +warn_deprecated_package( + 'xblockutils.helpers', + 'xblock.utils.helpers' +) + def child_isinstance(block, child_id, block_class_or_mixin): """ diff --git a/xblockutils/publish_event.py b/xblockutils/publish_event.py index d366f9c..00783de 100644 --- a/xblockutils/publish_event.py +++ b/xblockutils/publish_event.py @@ -22,6 +22,13 @@ from xblock.core import XBlock +from xblockutils.deprecation.warn import warn_deprecated_package + +warn_deprecated_package( + 'xblockutils.publish_event', + 'xblock.utils.publish_event' +) + class PublishEventMixin: """ diff --git a/xblockutils/resources.py b/xblockutils/resources.py index 2f6b268..8e9986c 100644 --- a/xblockutils/resources.py +++ b/xblockutils/resources.py @@ -25,12 +25,17 @@ import warnings import pkg_resources - from django.template import Context, Template, Engine from django.template.backends.django import get_installed_libraries - -from mako.template import Template as MakoTemplate from mako.lookup import TemplateLookup as MakoTemplateLookup +from mako.template import Template as MakoTemplate + +from xblockutils.deprecation.warn import warn_deprecated_package + +warn_deprecated_package( + 'xblockutils.resources', + 'xblock.utils.resources' +) class ResourceLoader: diff --git a/xblockutils/settings.py b/xblockutils/settings.py index 26552fe..8b5302c 100644 --- a/xblockutils/settings.py +++ b/xblockutils/settings.py @@ -5,8 +5,14 @@ This module contains a mixins that allows third party XBlocks to access Settings Service in edX LMS. """ +from xblockutils.deprecation.warn import warn_deprecated_package from xblockutils.resources import ResourceLoader +warn_deprecated_package( + 'xblockutils.settings', + 'xblock.utils.settings' +) + class XBlockWithSettingsMixin: """ diff --git a/xblockutils/studio_editable.py b/xblockutils/studio_editable.py index 7100681..5841baa 100644 --- a/xblockutils/studio_editable.py +++ b/xblockutils/studio_editable.py @@ -11,23 +11,29 @@ import logging -import simplejson as json +import simplejson as json +from web_fragments.fragment import Fragment from xblock.core import XBlock, XBlockMixin -from xblock.fields import Scope, JSONField, List, Integer, Float, Boolean, String, DateTime from xblock.exceptions import JsonHandlerError, NoSuchViewError -from web_fragments.fragment import Fragment +from xblock.fields import Scope, JSONField, List, Integer, Float, Boolean, String, DateTime from xblock.validation import Validation +from xblockutils.deprecation.warn import warn_deprecated_package from xblockutils.resources import ResourceLoader +warn_deprecated_package( + 'xblockutils.studio_editable', + 'xblock.utils.studio_editable' +) + # Globals ########################################################### log = logging.getLogger(__name__) loader = ResourceLoader(__name__) -# Classes ########################################################### +# Classes ########################################################### class FutureFields: """ diff --git a/xblockutils/templatetags/i18n.py b/xblockutils/templatetags/i18n.py index 90ab8a8..2867e50 100644 --- a/xblockutils/templatetags/i18n.py +++ b/xblockutils/templatetags/i18n.py @@ -10,6 +10,12 @@ from django.templatetags import i18n from django.utils.translation import get_language, trans_real +from xblockutils.deprecation.warn import warn_deprecated_package + +warn_deprecated_package( + 'xblockutils.templatetags.i18n', + 'xblock.utils.templatetags.i18n' +) register = Library()