Merge pull request #2117 from hroff-1902/config-load-config

Minor configuration cleanup
This commit is contained in:
Matthias 2019-08-10 19:34:03 +02:00 committed by GitHub
commit 8ba7657007
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 30 deletions

View File

@ -1,9 +1,7 @@
""" """
This module contains the configuration class This module contains the configuration class
""" """
import json
import logging import logging
import sys
import warnings import warnings
from argparse import Namespace from argparse import Namespace
from typing import Any, Callable, Dict, Optional from typing import Any, Callable, Dict, Optional
@ -12,6 +10,7 @@ from freqtrade import OperationalException, constants
from freqtrade.configuration.check_exchange import check_exchange from freqtrade.configuration.check_exchange import check_exchange
from freqtrade.configuration.create_datadir import create_datadir from freqtrade.configuration.create_datadir import create_datadir
from freqtrade.configuration.json_schema import validate_config_schema from freqtrade.configuration.json_schema import validate_config_schema
from freqtrade.configuration.load_config import load_config_file
from freqtrade.loggers import setup_logging from freqtrade.loggers import setup_logging
from freqtrade.misc import deep_merge_dicts from freqtrade.misc import deep_merge_dicts
from freqtrade.state import RunMode from freqtrade.state import RunMode
@ -52,24 +51,7 @@ class Configuration(object):
logger.info('Using config: %s ...', path) logger.info('Using config: %s ...', path)
# Merge config options, overwriting old values # Merge config options, overwriting old values
config = deep_merge_dicts(self._load_config_file(path), config) config = deep_merge_dicts(load_config_file(path), config)
return config
def _load_config_file(self, path: str) -> Dict[str, Any]:
"""
Loads a config file from the given path
:param path: path as str
:return: configuration as dictionary
"""
try:
# Read config from stdin if requested in the options
with open(path) if path != '-' else sys.stdin as file:
config = json.load(file)
except FileNotFoundError:
raise OperationalException(
f'Config file "{path}" not found!'
' Please create a config file or check whether it exists.')
return config return config

View File

@ -0,0 +1,30 @@
"""
This module contain functions to load the configuration file
"""
import json
import logging
import sys
from typing import Any, Dict
from freqtrade import OperationalException
logger = logging.getLogger(__name__)
def load_config_file(path: str) -> Dict[str, Any]:
"""
Loads a config file from the given path
:param path: path as str
:return: configuration as dictionary
"""
try:
# Read config from stdin if requested in the options
with open(path) if path != '-' else sys.stdin as file:
config = json.load(file)
except FileNotFoundError:
raise OperationalException(
f'Config file "{path}" not found!'
' Please create a config file or check whether it exists.')
return config

View File

@ -45,7 +45,7 @@ def get_args(args):
def patched_configuration_load_config_file(mocker, config) -> None: def patched_configuration_load_config_file(mocker, config) -> None:
mocker.patch( mocker.patch(
'freqtrade.configuration.configuration.Configuration._load_config_file', 'freqtrade.configuration.configuration.load_config_file',
lambda *args, **kwargs: config lambda *args, **kwargs: config
) )

View File

@ -15,6 +15,7 @@ from freqtrade.configuration import Arguments, Configuration
from freqtrade.configuration.check_exchange import check_exchange from freqtrade.configuration.check_exchange import check_exchange
from freqtrade.configuration.create_datadir import create_datadir from freqtrade.configuration.create_datadir import create_datadir
from freqtrade.configuration.json_schema import validate_config_schema from freqtrade.configuration.json_schema import validate_config_schema
from freqtrade.configuration.load_config import load_config_file
from freqtrade.constants import DEFAULT_DB_DRYRUN_URL, DEFAULT_DB_PROD_URL from freqtrade.constants import DEFAULT_DB_DRYRUN_URL, DEFAULT_DB_PROD_URL
from freqtrade.loggers import _set_loggers from freqtrade.loggers import _set_loggers
from freqtrade.state import RunMode from freqtrade.state import RunMode
@ -26,8 +27,7 @@ from freqtrade.tests.conftest import (log_has, log_has_re,
def all_conf(): def all_conf():
config_file = Path(__file__).parents[2] / "config_full.json.example" config_file = Path(__file__).parents[2] / "config_full.json.example"
print(config_file) print(config_file)
configuration = Configuration(Namespace()) conf = load_config_file(str(config_file))
conf = configuration._load_config_file(str(config_file))
return conf return conf
@ -53,12 +53,11 @@ def test_load_config_incorrect_stake_amount(default_conf) -> None:
def test_load_config_file(default_conf, mocker, caplog) -> None: def test_load_config_file(default_conf, mocker, caplog) -> None:
file_mock = mocker.patch('freqtrade.configuration.configuration.open', mocker.mock_open( file_mock = mocker.patch('freqtrade.configuration.load_config.open', mocker.mock_open(
read_data=json.dumps(default_conf) read_data=json.dumps(default_conf)
)) ))
configuration = Configuration(Namespace()) validated_conf = load_config_file('somefile')
validated_conf = configuration._load_config_file('somefile')
assert file_mock.call_count == 1 assert file_mock.call_count == 1
assert validated_conf.items() >= default_conf.items() assert validated_conf.items() >= default_conf.items()
@ -114,7 +113,7 @@ def test_load_config_combine_dicts(default_conf, mocker, caplog) -> None:
configsmock = MagicMock(side_effect=config_files) configsmock = MagicMock(side_effect=config_files)
mocker.patch( mocker.patch(
'freqtrade.configuration.configuration.Configuration._load_config_file', 'freqtrade.configuration.configuration.load_config_file',
configsmock configsmock
) )
@ -154,10 +153,9 @@ def test_load_config_file_exception(mocker) -> None:
'freqtrade.configuration.configuration.open', 'freqtrade.configuration.configuration.open',
MagicMock(side_effect=FileNotFoundError('File not found')) MagicMock(side_effect=FileNotFoundError('File not found'))
) )
configuration = Configuration(Namespace())
with pytest.raises(OperationalException, match=r'.*Config file "somefile" not found!*'): with pytest.raises(OperationalException, match=r'.*Config file "somefile" not found!*'):
configuration._load_config_file('somefile') load_config_file('somefile')
def test_load_config(default_conf, mocker) -> None: def test_load_config(default_conf, mocker) -> None:

View File

@ -12,6 +12,7 @@ from freqtrade.configuration import Arguments, TimeRange
from freqtrade.configuration import Configuration from freqtrade.configuration import Configuration
from freqtrade.configuration.arguments import ARGS_DOWNLOADER from freqtrade.configuration.arguments import ARGS_DOWNLOADER
from freqtrade.configuration.check_exchange import check_exchange from freqtrade.configuration.check_exchange import check_exchange
from freqtrade.configuration.load_config import load_config_file
from freqtrade.data.history import download_pair_history from freqtrade.data.history import download_pair_history
from freqtrade.exchange import Exchange from freqtrade.exchange import Exchange
from freqtrade.misc import deep_merge_dicts from freqtrade.misc import deep_merge_dicts
@ -42,7 +43,7 @@ if args.config:
for path in args.config: for path in args.config:
logger.info(f"Using config: {path}...") logger.info(f"Using config: {path}...")
# Merge config options, overwriting old values # Merge config options, overwriting old values
config = deep_merge_dicts(configuration._load_config_file(path), config) config = deep_merge_dicts(load_config_file(path), config)
config['stake_currency'] = '' config['stake_currency'] = ''
# Ensure we do not use Exchange credentials # Ensure we do not use Exchange credentials