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 # Add extra hyperopt directory on top of search paths
abs_paths.insert(0, Path(extra_dir)) abs_paths.insert(0, Path(extra_dir))
for _path in abs_paths: (hyperopt, module_path) = self._load_object(paths=abs_paths,
try: object_type=IHyperOpt,
(hyperopt, module_path) = self._search_object(directory=_path, object_name=hyperopt_name,
object_type=IHyperOpt, kwargs={})
object_name=hyperopt_name) if hyperopt:
if hyperopt: return 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()))
raise OperationalException( raise OperationalException(
f"Impossible to load Hyperopt '{hyperopt_name}'. This class does not exist " f"Impossible to load Hyperopt '{hyperopt_name}'. This class does not exist "
"or contains Python code errors." "or contains Python code errors."
@ -125,17 +120,12 @@ class HyperOptLossResolver(IResolver):
# Add extra hyperopt directory on top of search paths # Add extra hyperopt directory on top of search paths
abs_paths.insert(0, Path(extra_dir)) abs_paths.insert(0, Path(extra_dir))
for _path in abs_paths: (hyperoptloss, module_path) = self._load_object(paths=abs_paths,
try: object_type=IHyperOptLoss,
(hyperoptloss, module_path) = self._search_object(directory=_path, object_name=hyper_loss_name,
object_type=IHyperOptLoss, kwargs={})
object_name=hyper_loss_name) if hyperoptloss:
if hyperoptloss: return 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()))
raise OperationalException( raise OperationalException(
f"Impossible to load HyperoptLoss '{hyper_loss_name}'. This class does not exist " 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 inspect
import logging import logging
from pathlib import Path 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__) logger = logging.getLogger(__name__)
@ -64,3 +64,26 @@ class IResolver(object):
if obj: if obj:
return (obj(**kwargs), module_path) return (obj(**kwargs), module_path)
return (None, None) 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, current_path,
] ]
for _path in abs_paths: (pairlist, module_path) = self._load_object(paths=abs_paths,
try: object_type=IPairList,
(pairlist, module_path) = self._search_object(directory=_path, object_name=pairlist_name,
object_type=IPairList, kwargs=kwargs)
object_name=pairlist_name, if pairlist:
kwargs=kwargs) return pairlist
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()))
raise OperationalException( raise OperationalException(
f"Impossible to load Pairlist '{pairlist_name}'. This class does not exist " f"Impossible to load Pairlist '{pairlist_name}'. This class does not exist "
"or contains Python code errors." "or contains Python code errors."

View File

@ -147,19 +147,10 @@ class StrategyResolver(IResolver):
# register temp path with the bot # register temp path with the bot
abs_paths.insert(0, temp.resolve()) abs_paths.insert(0, temp.resolve())
for _path in abs_paths: (strategy, module_path) = self._load_object(paths=abs_paths,
try: object_type=IStrategy,
(strategy, module_path) = self._search_object(directory=_path, object_name=strategy_name,
object_type=IStrategy, kwargs={'config': config})
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()))
if strategy: if strategy:
strategy._populate_fun_len = len(getfullargspec(strategy.populate_indicators).args) strategy._populate_fun_len = len(getfullargspec(strategy.populate_indicators).args)
strategy._buy_fun_len = len(getfullargspec(strategy.populate_buy_trend).args) strategy._buy_fun_len = len(getfullargspec(strategy.populate_buy_trend).args)