Only resolve exchanges from correct location

This commit is contained in:
Matthias 2019-02-21 07:07:45 +01:00
parent e0f426d863
commit be754244a3
2 changed files with 13 additions and 12 deletions

View File

@ -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"

View File

@ -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)