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 This module loads custom exchanges
""" """
import logging import logging
from pathlib import Path
from freqtrade.exchange import Exchange from freqtrade.exchange import Exchange
import freqtrade.exchange as exchanges
from freqtrade.resolvers import IResolver from freqtrade.resolvers import IResolver
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -20,7 +20,7 @@ class ExchangeResolver(IResolver):
def __init__(self, exchange_name: str, config: dict) -> None: def __init__(self, exchange_name: str, config: dict) -> None:
""" """
Load the custom class from config parameter Load the custom class from config parameter
:param config: configuration dictionary or None :param config: configuration dictionary
""" """
try: try:
self.exchange = self._load_exchange(exchange_name, kwargs={'config': config}) self.exchange = self._load_exchange(exchange_name, kwargs={'config': config})
@ -32,22 +32,22 @@ class ExchangeResolver(IResolver):
def _load_exchange( def _load_exchange(
self, exchange_name: str, kwargs: dict) -> 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 exchange_name: name of the module to import
:param extra_dir: additional directory to search for the given exchange
:return: Exchange instance or None :return: Exchange instance or None
""" """
abs_path = Path(__file__).parent.parent.joinpath('exchange').resolve()
try: try:
exchange = self._search_object(directory=abs_path, object_type=Exchange, ex_class = getattr(exchanges, exchange_name)
object_name=exchange_name,
kwargs=kwargs) exchange = ex_class(kwargs['config'])
if exchange: if exchange:
logger.info("Using resolved exchange %s from '%s'", exchange_name, abs_path) logger.info("Using resolved exchange %s", exchange_name)
return exchange return exchange
except FileNotFoundError: except AttributeError:
logger.warning('Path "%s" does not exist', abs_path.relative_to(Path.cwd())) # Pass and raise ImportError instead
pass
raise ImportError( raise ImportError(
"Impossible to load Exchange '{}'. This class does not exist" "Impossible to load Exchange '{}'. This class does not exist"

View File

@ -12,7 +12,7 @@ import pytest
from pandas import DataFrame from pandas import DataFrame
from freqtrade import DependencyException, OperationalException, TemporaryError 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.exchange.exchange import API_RETRY_COUNT
from freqtrade.tests.conftest import get_patched_exchange, log_has, log_has_re from freqtrade.tests.conftest import get_patched_exchange, log_has, log_has_re
from freqtrade.resolvers.exchange_resolver import ExchangeResolver 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 exchange = ExchangeResolver('Kraken', default_conf).exchange
assert isinstance(exchange, Exchange) assert isinstance(exchange, Exchange)
assert isinstance(exchange, Kraken)
assert not log_has_re(r"No .* specific subclass found. Using the generic class instead.", assert not log_has_re(r"No .* specific subclass found. Using the generic class instead.",
caplog.record_tuples) caplog.record_tuples)