Skip to content

Commit

Permalink
Replace Usage of Imp with Importlib
Browse files Browse the repository at this point in the history
This is a pretty hacky reimplementation based on https://docs.python.org/3/whatsnew/3.12.html#imp, but I think it should work. Python no longer complains that the code is invalid, however I unfortunately don't have an iDRAC easily accessible to test with.
  • Loading branch information
thebeanogamer committed Apr 6, 2024
1 parent 7d7ff3d commit 16e9caa
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions omsdk/sdkinfra.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
# Authors: Vaideeswaran Ganesan
#
import os
import imp
import importlib.util
import importlib.machinery
import logging
import socket
import sys, glob
Expand All @@ -31,6 +32,18 @@
logger = logging.getLogger(__name__)


# https://docs.python.org/3/whatsnew/3.12.html#imp
def load_module(modname, filename, compiled):
if compiled:
loader = importlib.machinery.SourcelessFileLoader(modname, filename)
else:
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
return module


class sdkinfra:
"""
Class to initilaize and load the device drivers
Expand All @@ -45,9 +58,9 @@ def load_from_file(self, filepath):
mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])
logger.debug("Loading " + filepath + "...")
if file_ext.lower() == '.py':
py_mod = imp.load_source(mod_name, filepath)
py_mod = load_module(mod_name, filepath, False)
elif file_ext.lower() == '.pyc':
py_mod = imp.load_compiled(mod_name, filepath)
py_mod = load_module(mod_name, filepath, True)
return {"name": mod_name, "module": py_mod}

def importPath(self, srcdir=None):
Expand Down

0 comments on commit 16e9caa

Please sign in to comment.