move create_datadir() to separate file

This commit is contained in:
hroff-1902 2019-07-12 03:26:27 +03:00
parent 7e103e34f8
commit bbfbd87a9f
6 changed files with 32 additions and 25 deletions

View File

@ -3,13 +3,13 @@ This module contains the configuration class
""" """
import json import json
import logging import logging
import os
import sys import sys
from argparse import Namespace from argparse import Namespace
from typing import Any, Callable, Dict, Optional from typing import Any, Callable, Dict, Optional
from freqtrade import OperationalException, constants 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.json_schema import validate_config_schema from freqtrade.configuration.json_schema import validate_config_schema
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
@ -171,17 +171,6 @@ class Configuration(object):
return config return config
def _create_datadir(self, config: Dict[str, Any], datadir: Optional[str] = None) -> str:
if not datadir:
# set datadir
exchange_name = config.get('exchange', {}).get('name').lower()
datadir = os.path.join('user_data', 'data', exchange_name)
if not os.path.isdir(datadir):
os.makedirs(datadir)
logger.info(f'Created data directory: {datadir}')
return datadir
def _args_to_config(self, config: Dict[str, Any], argname: str, def _args_to_config(self, config: Dict[str, Any], argname: str,
logstring: str, logfun: Optional[Callable] = None) -> None: logstring: str, logfun: Optional[Callable] = None) -> None:
""" """
@ -207,9 +196,9 @@ class Configuration(object):
the --datadir option the --datadir option
""" """
if 'datadir' in self.args and self.args.datadir: if 'datadir' in self.args and self.args.datadir:
config.update({'datadir': self._create_datadir(config, self.args.datadir)}) config.update({'datadir': create_datadir(config, self.args.datadir)})
else: else:
config.update({'datadir': self._create_datadir(config, None)}) config.update({'datadir': create_datadir(config, None)})
logger.info('Using data directory: %s ...', config.get('datadir')) logger.info('Using data directory: %s ...', config.get('datadir'))
def _load_optimize_config(self, config: Dict[str, Any]) -> Dict[str, Any]: def _load_optimize_config(self, config: Dict[str, Any]) -> Dict[str, Any]:

View File

@ -0,0 +1,18 @@
import logging
import os
from typing import Any, Dict, Optional
logger = logging.getLogger(__name__)
def create_datadir(config: Dict[str, Any], datadir: Optional[str] = None) -> str:
if not datadir:
# set datadir
exchange_name = config.get('exchange', {}).get('name').lower()
datadir = os.path.join('user_data', 'data', exchange_name)
if not os.path.isdir(datadir):
os.makedirs(datadir)
logger.info(f'Created data directory: {datadir}')
return datadir

View File

@ -205,8 +205,8 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) ->
def test_setup_bt_configuration_with_arguments(mocker, default_conf, caplog) -> None: def test_setup_bt_configuration_with_arguments(mocker, default_conf, caplog) -> None:
patched_configuration_open(mocker, default_conf) patched_configuration_open(mocker, default_conf)
mocker.patch( mocker.patch(
'freqtrade.configuration.configuration.Configuration._create_datadir', 'freqtrade.configuration.configuration.create_datadir',
lambda s, c, x: x lambda c, x: x
) )
args = [ args = [

View File

@ -46,8 +46,8 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) ->
def test_setup_edge_configuration_with_arguments(mocker, edge_conf, caplog) -> None: def test_setup_edge_configuration_with_arguments(mocker, edge_conf, caplog) -> None:
patched_configuration_open(mocker, edge_conf) patched_configuration_open(mocker, edge_conf)
mocker.patch( mocker.patch(
'freqtrade.configuration.configuration.Configuration._create_datadir', 'freqtrade.configuration.configuration.create_datadir',
lambda s, c, x: x lambda c, x: x
) )
args = [ args = [

View File

@ -83,8 +83,8 @@ def test_setup_hyperopt_configuration_without_arguments(mocker, default_conf, ca
def test_setup_hyperopt_configuration_with_arguments(mocker, default_conf, caplog) -> None: def test_setup_hyperopt_configuration_with_arguments(mocker, default_conf, caplog) -> None:
patched_configuration_open(mocker, default_conf) patched_configuration_open(mocker, default_conf)
mocker.patch( mocker.patch(
'freqtrade.configuration.configuration.Configuration._create_datadir', 'freqtrade.configuration.configuration.create_datadir',
lambda s, c, x: x lambda c, x: x
) )
args = [ args = [

View File

@ -12,6 +12,7 @@ from jsonschema import Draft4Validator, ValidationError, validate
from freqtrade import OperationalException, constants from freqtrade import OperationalException, constants
from freqtrade.configuration import Arguments, Configuration 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.json_schema import validate_config_schema from freqtrade.configuration.json_schema import validate_config_schema
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
@ -308,8 +309,8 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) ->
def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> None: def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> None:
patched_configuration_open(mocker, default_conf) patched_configuration_open(mocker, default_conf)
mocker.patch( mocker.patch(
'freqtrade.configuration.configuration.Configuration._create_datadir', 'freqtrade.configuration.configuration.create_datadir',
lambda s, c, x: x lambda c, x: x
) )
arglist = [ arglist = [
@ -580,12 +581,11 @@ def test_validate_default_conf(default_conf) -> None:
validate(default_conf, constants.CONF_SCHEMA, Draft4Validator) validate(default_conf, constants.CONF_SCHEMA, Draft4Validator)
def test__create_datadir(mocker, default_conf, caplog) -> None: def test_create_datadir(mocker, default_conf, caplog) -> None:
mocker.patch('os.path.isdir', MagicMock(return_value=False)) mocker.patch('os.path.isdir', MagicMock(return_value=False))
md = MagicMock() md = MagicMock()
mocker.patch('os.makedirs', md) mocker.patch('os.makedirs', md)
cfg = Configuration(Namespace()) create_datadir(default_conf, '/foo/bar')
cfg._create_datadir(default_conf, '/foo/bar')
assert md.call_args[0][0] == "/foo/bar" assert md.call_args[0][0] == "/foo/bar"
assert log_has('Created data directory: /foo/bar', caplog.record_tuples) assert log_has('Created data directory: /foo/bar', caplog.record_tuples)