Skip to content

Commit

Permalink
Merge pull request #44 from thebeanogamer/removeimp
Browse files Browse the repository at this point in the history
Replace Usage of Imp with Importlib
  • Loading branch information
jagadeeshnv authored May 22, 2024
2 parents 7d7ff3d + 16e9caa commit a5fd054
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 a5fd054

Please sign in to comment.