Extract load_object from multiple paths to iResolver

This commit is contained in:
Matthias 2019-07-21 15:03:12 +02:00
parent 89db5c6bab
commit b35efd96dc
4 changed files with 46 additions and 48 deletions

View File

@ -63,17 +63,12 @@ class HyperOptResolver(IResolver):
# Add extra hyperopt directory on top of search paths
abs_paths.insert(0, Path(extra_dir))
for _path in abs_paths:
try:
(hyperopt, module_path) = self._search_object(directory=_path,
object_type=IHyperOpt,
object_name=hyperopt_name)
if hyperopt:
logger.info(f"Using resolved hyperopt {hyperopt_name} from '{module_path}'...")
return hyperopt
except FileNotFoundError:
logger.warning('Path "%s" does not exist.', _path.relative_to(Path.cwd()))
(hyperopt, module_path) = self._load_object(paths=abs_paths,
object_type=IHyperOpt,
object_name=hyperopt_name,
kwargs={})
if hyperopt:
return hyperopt
raise OperationalException(
f"Impossible to load Hyperopt '{hyperopt_name}'. This class does not exist "
"or contains Python code errors."
@ -125,17 +120,12 @@ class HyperOptLossResolver(IResolver):
# Add extra hyperopt directory on top of search paths
abs_paths.insert(0, Path(extra_dir))
for _path in abs_paths:
try:
(hyperoptloss, module_path) = self._search_object(directory=_path,
object_type=IHyperOptLoss,
object_name=hyper_loss_name)
if hyperoptloss:
logger.info(
f"Using resolved hyperopt {hyper_loss_name} from '{module_path}'...")
return hyperoptloss
except FileNotFoundError:
logger.warning('Path "%s" does not exist.', _path.relative_to(Path.cwd()))
(hyperoptloss, module_path) = self._load_object(paths=abs_paths,
object_type=IHyperOptLoss,
object_name=hyper_loss_name,
kwargs={})
if hyperoptloss:
return hyperoptloss
raise OperationalException(
f"Impossible to load HyperoptLoss '{hyper_loss_name}'. This class does not exist "

View File

@ -7,7 +7,7 @@ import importlib.util
import inspect
import logging
from pathlib import Path
from typing import Any, Optional, Tuple, Type, Union
from typing import Any, List, Optional, Tuple, Type, Union
logger = logging.getLogger(__name__)
@ -64,3 +64,26 @@ class IResolver(object):
if obj:
return (obj(**kwargs), module_path)
return (None, None)
@staticmethod
def _load_object(paths: List[Path], object_type, object_name: str,
kwargs: dict = {}) -> Union[Tuple[Any, Path], Tuple[None, None]]:
"""
Try to load object from path list.
"""
for _path in paths:
try:
(module, module_path) = IResolver._search_object(directory=_path,
object_type=object_type,
object_name=object_name,
kwargs=kwargs)
if module:
logger.info(
f"Using resolved {object_type.__name__.lower()[1:]} {object_name} "
f"from '{module_path}'...")
return (module, module_path)
except FileNotFoundError:
logger.warning('Path "%s" does not exist.', _path.relative_to(Path.cwd()))
return (None, None)

View File

@ -43,18 +43,12 @@ class PairListResolver(IResolver):
current_path,
]
for _path in abs_paths:
try:
(pairlist, module_path) = self._search_object(directory=_path,
object_type=IPairList,
object_name=pairlist_name,
kwargs=kwargs)
if pairlist:
logger.info(f"Using resolved pairlist {pairlist_name} from '{module_path}'...")
return pairlist
except FileNotFoundError:
logger.warning('Path "%s" does not exist.', _path.relative_to(Path.cwd()))
(pairlist, module_path) = self._load_object(paths=abs_paths,
object_type=IPairList,
object_name=pairlist_name,
kwargs=kwargs)
if pairlist:
return pairlist
raise OperationalException(
f"Impossible to load Pairlist '{pairlist_name}'. This class does not exist "
"or contains Python code errors."

View File

@ -147,19 +147,10 @@ class StrategyResolver(IResolver):
# register temp path with the bot
abs_paths.insert(0, temp.resolve())
for _path in abs_paths:
try:
(strategy, module_path) = self._search_object(directory=_path,
object_type=IStrategy,
object_name=strategy_name,
kwargs={'config': config})
if strategy:
logger.info(f"Using resolved strategy {strategy_name} from '{module_path}'...")
break
except FileNotFoundError:
logger.warning('Path "%s" does not exist.', _path.relative_to(Path.cwd()))
(strategy, module_path) = self._load_object(paths=abs_paths,
object_type=IStrategy,
object_name=strategy_name,
kwargs={'config': config})
if strategy:
strategy._populate_fun_len = len(getfullargspec(strategy.populate_indicators).args)
strategy._buy_fun_len = len(getfullargspec(strategy.populate_buy_trend).args)