propoer import from file_path after all else fails
This commit is contained in:
committed by
Sung Ching Liu
parent
9b74298328
commit
8df37ad91f
@@ -23,36 +23,44 @@ def import_from_path(module_name, file_path):
|
|||||||
|
|
||||||
|
|
||||||
def load(strategy, cfg, module_base=None, **kwargs):
|
def load(strategy, cfg, module_base=None, **kwargs):
|
||||||
try:
|
if len(strategy.split(".")) == 1:
|
||||||
if len(strategy.split(".")) == 1:
|
strategy = strategy + ".default"
|
||||||
strategy = strategy + ".default"
|
load_fn = strategy.split(".")[-1]
|
||||||
load_fn = strategy.split(".")[-1]
|
func = None
|
||||||
if len(strategy.split(".")) > 1:
|
if len(strategy.split(".")) > 1:
|
||||||
try:
|
try:
|
||||||
importlib.import_module(
|
mod = importlib.import_module(
|
||||||
strategy.split(".")[-2],
|
strategy.split(".")[-2],
|
||||||
".".join(strategy.split(".")[:-2]),
|
".".join(strategy.split(".")[:-2]),
|
||||||
)
|
)
|
||||||
module_base = ".".join(strategy.split(".")[:-2])
|
func = getattr(mod, load_fn)
|
||||||
strategy = strategy.split(".")[-2]
|
return func(cfg, **kwargs)
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
try:
|
pass
|
||||||
file_path = "/".join(strategy.split(".")[:-1]) + ".py"
|
|
||||||
module_name = strategy.split(".")[-2]
|
|
||||||
mod = import_from_path(module_name, file_path)
|
|
||||||
func = getattr(mod, load_fn)
|
|
||||||
if func is None:
|
|
||||||
strategy = "." + ".".join(strategy.split(".")[:-1])
|
|
||||||
else:
|
|
||||||
return func(cfg, **kwargs)
|
|
||||||
except FileNotFoundError:
|
|
||||||
strategy = "." + ".".join(strategy.split(".")[:-1])
|
|
||||||
|
|
||||||
else:
|
try:
|
||||||
strategy = "." + ".".join(strategy.split(".")[:-1])
|
mod = importlib.import_module(
|
||||||
|
"." + ".".join(strategy.split(".")[:-1]), module_base
|
||||||
|
)
|
||||||
|
func = getattr(mod, load_fn)
|
||||||
|
return func(cfg, **kwargs)
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
file_path = "/".join(strategy.split(".")[:-1]) + ".py"
|
||||||
|
module_name = strategy.split(".")[-2]
|
||||||
|
mod = import_from_path(module_name, file_path)
|
||||||
|
func = getattr(mod, load_fn)
|
||||||
|
if func is not None:
|
||||||
|
return func(cfg, **kwargs)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
strategy = "." + ".".join(strategy.split(".")[:-1])
|
||||||
mod = importlib.import_module(strategy, module_base)
|
mod = importlib.import_module(strategy, module_base)
|
||||||
func = getattr(mod, load_fn)
|
func = getattr(mod, load_fn)
|
||||||
return func(cfg, **kwargs)
|
return func(cfg, **kwargs)
|
||||||
except Exception: # pylint: disable=broad-exception-caught
|
|
||||||
LOG.warning(f"unable to load strategy {strategy}")
|
LOG.warning(f"unable to load strategy {strategy}")
|
||||||
return None
|
return func
|
||||||
|
|||||||
Reference in New Issue
Block a user