tools/file_import: simplify, remove deprecated load_module() call

Signed-off-by: Etienne Wodey <wodey@iqo.uni-hannover.de>
This commit is contained in:
Etienne Wodey 2021-02-09 16:13:36 +01:00 committed by Sébastien Bourdeauducq
parent e54dd08821
commit af411de639
1 changed files with 9 additions and 12 deletions

View File

@ -1,7 +1,8 @@
import asyncio import asyncio
import importlib.machinery import importlib.util
import logging import logging
import os import os
import pathlib
import string import string
import sys import sys
@ -69,20 +70,16 @@ def short_format(v):
def file_import(filename, prefix="file_import_"): def file_import(filename, prefix="file_import_"):
modname = filename filename = pathlib.Path(filename)
i = modname.rfind("/") modname = prefix + filename.stem
if i > 0:
modname = modname[i+1:]
i = modname.find(".")
if i > 0:
modname = modname[:i]
modname = prefix + modname
path = os.path.dirname(os.path.realpath(filename)) path = str(filename.resolve().parent)
sys.path.insert(0, path) sys.path.insert(0, path)
try: try:
loader = importlib.machinery.SourceFileLoader(modname, filename) spec = importlib.util.spec_from_file_location(modname, filename)
module = loader.load_module() module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
finally: finally:
sys.path.remove(path) sys.path.remove(path)