From 20de8c82e4c25aec91aa12fee76070dabc4d5f13 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 24 Nov 2018 20:39:16 +0100 Subject: [PATCH] Convert to Pathlib --- freqtrade/resolvers/hyperopt_resolver.py | 8 ++++---- freqtrade/resolvers/iresolver.py | 14 +++++++------- freqtrade/resolvers/strategy_resolver.py | 15 +++++++-------- freqtrade/tests/strategy/test_strategy.py | 5 ++--- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/freqtrade/resolvers/hyperopt_resolver.py b/freqtrade/resolvers/hyperopt_resolver.py index 38cb683c9..da7b65648 100644 --- a/freqtrade/resolvers/hyperopt_resolver.py +++ b/freqtrade/resolvers/hyperopt_resolver.py @@ -4,7 +4,7 @@ This module load custom hyperopts """ import logging -from os import path +from pathlib import Path from typing import Optional, Dict from freqtrade.constants import DEFAULT_HYPEROPT @@ -40,16 +40,16 @@ class HyperOptResolver(IResolver): :param extra_dir: additional directory to search for the given hyperopt :return: HyperOpt instance or None """ - current_path = path.join(path.dirname(path.dirname(path.realpath(__file__))), 'optimize') + current_path = Path(__file__).parent.parent.joinpath('optimize').resolve() abs_paths = [ - path.join(current_path, '..', '..', 'user_data', 'hyperopts'), + current_path.parent.parent.joinpath('user_data/hyperopts'), current_path, ] if extra_dir: # Add extra hyperopt directory on top of search paths - abs_paths.insert(0, extra_dir) + abs_paths.insert(0, Path(extra_dir)) for _path in abs_paths: hyperopt = self._search_object(directory=_path, object_type=IHyperOpt, diff --git a/freqtrade/resolvers/iresolver.py b/freqtrade/resolvers/iresolver.py index 5cf6a1bc2..78df32e89 100644 --- a/freqtrade/resolvers/iresolver.py +++ b/freqtrade/resolvers/iresolver.py @@ -6,7 +6,7 @@ This module load custom objects import importlib.util import inspect import logging -import os +from pathlib import Path from typing import Optional, Dict, Type, Any logger = logging.getLogger(__name__) @@ -25,7 +25,7 @@ class IResolver(object): config = config or {} @staticmethod - def _get_valid_objects(object_type, module_path: str, + def _get_valid_objects(object_type, module_path: Path, object_name: str) -> Optional[Type[Any]]: """ Returns a list of all possible objects for the given module_path of type oject_type @@ -36,7 +36,7 @@ class IResolver(object): """ # Generate spec based on absolute path - spec = importlib.util.spec_from_file_location('unknown', module_path) + spec = importlib.util.spec_from_file_location('unknown', str(module_path)) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) # type: ignore # importlib does not use typehints @@ -47,7 +47,7 @@ class IResolver(object): return next(valid_objects_gen, None) @staticmethod - def _search_object(directory: str, object_type, object_name: str, + def _search_object(directory: Path, object_type, object_name: str, kwargs: dict = {}) -> Optional[Any]: """ Search for the objectname in the given directory @@ -55,13 +55,13 @@ class IResolver(object): :return: object instance """ logger.debug('Searching for %s %s in \'%s\'', object_type.__name__, object_name, directory) - for entry in os.listdir(directory): + for entry in directory.iterdir(): # Only consider python files - if not entry.endswith('.py'): + if not str(entry).endswith('.py'): logger.debug('Ignoring %s', entry) continue obj = IResolver._get_valid_objects( - object_type, os.path.abspath(os.path.join(directory, entry)), object_name + object_type, Path.resolve(directory.joinpath(entry)), object_name ) if obj: return obj(**kwargs) diff --git a/freqtrade/resolvers/strategy_resolver.py b/freqtrade/resolvers/strategy_resolver.py index f950a6e41..4576d0ec8 100644 --- a/freqtrade/resolvers/strategy_resolver.py +++ b/freqtrade/resolvers/strategy_resolver.py @@ -5,7 +5,6 @@ This module load custom strategies """ import inspect import logging -from os import getcwd, path import tempfile from base64 import urlsafe_b64decode from collections import OrderedDict @@ -103,16 +102,16 @@ class StrategyResolver(IResolver): :param extra_dir: additional directory to search for the given strategy :return: Strategy instance or None """ - current_path = path.join(path.dirname(path.dirname(path.realpath(__file__))), 'strategy') + current_path = Path(__file__).parent.parent.joinpath('strategy').resolve() abs_paths = [ - path.join(getcwd(), 'user_data', 'strategies'), + Path.cwd().joinpath('user_data/strategies'), current_path, ] if extra_dir: # Add extra strategy directory on top of search paths - abs_paths.insert(0, extra_dir) + abs_paths.insert(0, Path(extra_dir).resolve()) if ":" in strategy_name: logger.info("loading base64 endocded strategy") @@ -125,17 +124,17 @@ class StrategyResolver(IResolver): temp.joinpath(name).write_text(urlsafe_b64decode(strat[1]).decode('utf-8')) temp.joinpath("__init__.py").touch() - strategy_name = path.splitext(name)[0] + strategy_name = strat[0] # register temp path with the bot - abs_paths.insert(0, str(temp.resolve())) + abs_paths.insert(0, temp.resolve()) for _path in abs_paths: try: strategy = self._search_object(directory=_path, object_type=IStrategy, object_name=strategy_name, kwargs={'config': config}) if strategy: - logger.info('Using resolved strategy %s from \'%s\'', strategy_name, path) + logger.info('Using resolved strategy %s from \'%s\'', strategy_name, _path) strategy._populate_fun_len = len( inspect.getfullargspec(strategy.populate_indicators).args) strategy._buy_fun_len = len( @@ -145,7 +144,7 @@ class StrategyResolver(IResolver): return import_strategy(strategy, config=config) except FileNotFoundError: - logger.warning('Path "%s" does not exist', _path) + logger.warning('Path "%s" does not exist', _path.relative_to(Path.cwd())) raise ImportError( "Impossible to load Strategy '{}'. This class does not exist" diff --git a/freqtrade/tests/strategy/test_strategy.py b/freqtrade/tests/strategy/test_strategy.py index 230ec7ab7..80bd9e120 100644 --- a/freqtrade/tests/strategy/test_strategy.py +++ b/freqtrade/tests/strategy/test_strategy.py @@ -2,6 +2,7 @@ import logging from base64 import urlsafe_b64encode from os import path +from pathlib import Path import warnings import pytest @@ -40,9 +41,7 @@ def test_import_strategy(caplog): def test_search_strategy(): default_config = {} - default_location = path.join(path.dirname( - path.realpath(__file__)), '..', '..', 'strategy' - ) + default_location = Path(__file__).parent.parent.joinpath('strategy').resolve() assert isinstance( StrategyResolver._search_object( directory=default_location,