Skip to content

Commit

Permalink
Fix for DeprecationWarning: the imp module is deprecated in favour of…
Browse files Browse the repository at this point in the history
… importlib
  • Loading branch information
nzlosh committed Mar 13, 2024
1 parent 848d9c2 commit 52b513e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions st2common/st2common/util/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

from __future__ import absolute_import

import imp
#::xx:: import imp
import importlib.util
import inspect
import json
import os
Expand Down Expand Up @@ -73,8 +74,8 @@ def _get_classes_in_module(module):
]


def _get_plugin_classes(module_name):
return _get_classes_in_module(module_name)
def _get_plugin_classes(module):
return _get_classes_in_module(module)


def _get_plugin_methods(plugin_klass):
Expand Down Expand Up @@ -129,7 +130,7 @@ def register_plugin_class(base_class, file_path, class_name):
"""
Retrieve a register plugin class from the provided file.
This method also validate that the class implements all the abstract methods
This method also validates that the class implements all the abstract methods
from the base plugin class.
:param base_class: Base plugin class.
Expand All @@ -148,7 +149,10 @@ def register_plugin_class(base_class, file_path, class_name):
if module_name is None:
return None

module = imp.load_source(module_name, file_path)
#::xx:: module = imp.load_source(module_name, file_path)
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
klass = getattr(module, class_name, None)

if not klass:
Expand All @@ -170,7 +174,10 @@ def register_plugin(plugin_base_class, plugin_abs_file_path):
if module_name is None:
return None

module = imp.load_source(module_name, plugin_abs_file_path)
#::xx:: module = imp.load_source(module_name, plugin_abs_file_path)
spec = importlib.util.spec_from_file_location(module_name, plugin_abs_file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
klasses = _get_plugin_classes(module)

# Try registering classes in plugin file. Some may fail.
Expand Down

0 comments on commit 52b513e

Please sign in to comment.