Refactor load-path building to parent class

This commit is contained in:
Matthias 2019-10-15 12:11:14 +02:00
parent 1a765f1a17
commit fda71085e0
4 changed files with 23 additions and 29 deletions

View File

@ -52,14 +52,8 @@ class HyperOptResolver(IResolver):
""" """
current_path = Path(__file__).parent.parent.joinpath('optimize').resolve() current_path = Path(__file__).parent.parent.joinpath('optimize').resolve()
abs_paths = [ abs_paths = self.build_search_paths(config, current_path=current_path,
config['user_data_dir'].joinpath('hyperopts'), user_subdir='hyperopts', extra_dir=extra_dir)
current_path,
]
if extra_dir:
# Add extra hyperopt directory on top of search paths
abs_paths.insert(0, Path(extra_dir).resolve())
hyperopt = self._load_object(paths=abs_paths, object_type=IHyperOpt, hyperopt = self._load_object(paths=abs_paths, object_type=IHyperOpt,
object_name=hyperopt_name, kwargs={'config': config}) object_name=hyperopt_name, kwargs={'config': config})
@ -109,14 +103,8 @@ class HyperOptLossResolver(IResolver):
""" """
current_path = Path(__file__).parent.parent.joinpath('optimize').resolve() current_path = Path(__file__).parent.parent.joinpath('optimize').resolve()
abs_paths = [ abs_paths = self.build_search_paths(config, current_path=current_path,
config['user_data_dir'].joinpath('hyperopts'), user_subdir='hyperopts', extra_dir=extra_dir)
current_path,
]
if extra_dir:
# Add extra hyperopt directory on top of search paths
abs_paths.insert(0, Path(extra_dir).resolve())
hyperoptloss = self._load_object(paths=abs_paths, object_type=IHyperOptLoss, hyperoptloss = self._load_object(paths=abs_paths, object_type=IHyperOptLoss,
object_name=hyper_loss_name) object_name=hyper_loss_name)

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, List, Optional, Tuple, Type, Union, Generator from typing import Any, List, Optional, Tuple, Union, Generator
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -17,6 +17,20 @@ class IResolver:
This class contains all the logic to load custom classes This class contains all the logic to load custom classes
""" """
def build_search_paths(self, config, current_path: Path, user_subdir: str,
extra_dir: Optional[str] = None) -> List[Path]:
abs_paths = [
config['user_data_dir'].joinpath(user_subdir),
current_path,
]
if extra_dir:
# Add extra directory to the top of the search paths
abs_paths.insert(0, Path(extra_dir).resolve())
return abs_paths
@staticmethod @staticmethod
def _get_valid_object(object_type, module_path: Path, def _get_valid_object(object_type, module_path: Path,
object_name: str) -> Generator[Any, None, None]: object_name: str) -> Generator[Any, None, None]:

View File

@ -39,10 +39,8 @@ class PairListResolver(IResolver):
""" """
current_path = Path(__file__).parent.parent.joinpath('pairlist').resolve() current_path = Path(__file__).parent.parent.joinpath('pairlist').resolve()
abs_paths = [ abs_paths = self.build_search_paths(config, current_path=current_path,
config['user_data_dir'].joinpath('pairlist'), user_subdir='pairlist', extra_dir=None)
current_path,
]
pairlist = self._load_object(paths=abs_paths, object_type=IPairList, pairlist = self._load_object(paths=abs_paths, object_type=IPairList,
object_name=pairlist_name, kwargs=kwargs) object_name=pairlist_name, kwargs=kwargs)

View File

@ -124,14 +124,8 @@ class StrategyResolver(IResolver):
""" """
current_path = Path(__file__).parent.parent.joinpath('strategy').resolve() current_path = Path(__file__).parent.parent.joinpath('strategy').resolve()
abs_paths = [ abs_paths = self.build_search_paths(config, current_path=current_path,
config['user_data_dir'].joinpath('strategies'), user_subdir='strategies', extra_dir=extra_dir)
current_path,
]
if extra_dir:
# Add extra strategy directory on top of search paths
abs_paths.insert(0, Path(extra_dir).resolve())
if ":" in strategy_name: if ":" in strategy_name:
logger.info("loading base64 encoded strategy") logger.info("loading base64 encoded strategy")