From be754244a3c9f153a5920353d5450f25b8058122 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 21 Feb 2019 07:07:45 +0100 Subject: [PATCH] Only resolve exchanges from correct location --- freqtrade/resolvers/exchange_resolver.py | 22 +++++++++++----------- freqtrade/tests/exchange/test_exchange.py | 3 ++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/freqtrade/resolvers/exchange_resolver.py b/freqtrade/resolvers/exchange_resolver.py index a68219527..8d1845c71 100644 --- a/freqtrade/resolvers/exchange_resolver.py +++ b/freqtrade/resolvers/exchange_resolver.py @@ -2,9 +2,9 @@ This module loads custom exchanges """ import logging -from pathlib import Path from freqtrade.exchange import Exchange +import freqtrade.exchange as exchanges from freqtrade.resolvers import IResolver logger = logging.getLogger(__name__) @@ -20,7 +20,7 @@ class ExchangeResolver(IResolver): def __init__(self, exchange_name: str, config: dict) -> None: """ Load the custom class from config parameter - :param config: configuration dictionary or None + :param config: configuration dictionary """ try: self.exchange = self._load_exchange(exchange_name, kwargs={'config': config}) @@ -32,22 +32,22 @@ class ExchangeResolver(IResolver): def _load_exchange( self, exchange_name: str, kwargs: dict) -> Exchange: """ - Search and loads the specified exchange. + Loads the specified exchange. + Only checks for exchanges exported in freqtrade.exchanges :param exchange_name: name of the module to import - :param extra_dir: additional directory to search for the given exchange :return: Exchange instance or None """ - abs_path = Path(__file__).parent.parent.joinpath('exchange').resolve() try: - exchange = self._search_object(directory=abs_path, object_type=Exchange, - object_name=exchange_name, - kwargs=kwargs) + ex_class = getattr(exchanges, exchange_name) + + exchange = ex_class(kwargs['config']) if exchange: - logger.info("Using resolved exchange %s from '%s'", exchange_name, abs_path) + logger.info("Using resolved exchange %s", exchange_name) return exchange - except FileNotFoundError: - logger.warning('Path "%s" does not exist', abs_path.relative_to(Path.cwd())) + except AttributeError: + # Pass and raise ImportError instead + pass raise ImportError( "Impossible to load Exchange '{}'. This class does not exist" diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index ca76a0bd7..15da5e924 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -12,7 +12,7 @@ import pytest from pandas import DataFrame from freqtrade import DependencyException, OperationalException, TemporaryError -from freqtrade.exchange import Exchange +from freqtrade.exchange import Exchange, Kraken from freqtrade.exchange.exchange import API_RETRY_COUNT from freqtrade.tests.conftest import get_patched_exchange, log_has, log_has_re from freqtrade.resolvers.exchange_resolver import ExchangeResolver @@ -121,6 +121,7 @@ def test_exchange_resolver(default_conf, mocker, caplog): exchange = ExchangeResolver('Kraken', default_conf).exchange assert isinstance(exchange, Exchange) + assert isinstance(exchange, Kraken) assert not log_has_re(r"No .* specific subclass found. Using the generic class instead.", caplog.record_tuples)