Allow resetting of the directory

This commit is contained in:
Matthias 2019-11-01 14:08:55 +01:00
parent 19b1a6c638
commit 41494f28da
5 changed files with 19 additions and 6 deletions

View File

@ -37,7 +37,7 @@ ARGS_LIST_TIMEFRAMES = ["exchange", "print_one_column"]
ARGS_LIST_PAIRS = ["exchange", "print_list", "list_pairs_print_json", "print_one_column",
"print_csv", "base_currencies", "quote_currencies", "list_pairs_all"]
ARGS_CREATE_USERDIR = ["user_data_dir"]
ARGS_CREATE_USERDIR = ["user_data_dir", "reset"]
ARGS_DOWNLOAD_DATA = ["pairs", "pairs_file", "days", "download_trades", "exchange",
"timeframes", "erase"]

View File

@ -62,6 +62,11 @@ AVAILABLE_CLI_OPTIONS = {
help='Path to userdata directory.',
metavar='PATH',
),
"reset": Arg(
'--reset',
help='Reset sample files to their original state.',
action='store_true',
),
# Main options
"strategy": Arg(
'-s', '--strategy',

View File

@ -53,10 +53,11 @@ def create_userdata_dir(directory: str, create_dir=False) -> Path:
return folder
def copy_sample_files(directory: Path) -> None:
def copy_sample_files(directory: Path, overwrite: bool = False) -> None:
"""
Copy files from templates to User data directory.
:param directory: Directory to copy data to
:param overwrite: Overwrite existing sample files
"""
if not directory.is_dir():
raise OperationalException(f"Directory `{directory}` does not exist.")
@ -67,6 +68,9 @@ def copy_sample_files(directory: Path) -> None:
raise OperationalException(f"Directory `{targetdir}` does not exist.")
targetfile = targetdir / source
if targetfile.exists():
logger.warning(f"File `{targetfile}` exists already, not deploying sample file.")
continue
if not overwrite:
logger.warning(f"File `{targetfile}` exists already, not deploying sample file.")
continue
else:
logger.warning(f"File `{targetfile}` exists already, overwriting.")
shutil.copy(str(sourcedir / source), str(targetfile))

View File

@ -1,3 +1,4 @@
from freqtrade.loggers import setup_logging
import logging
import sys
from collections import OrderedDict
@ -82,7 +83,7 @@ def start_create_userdir(args: Dict[str, Any]) -> None:
"""
if "user_data_dir" in args and args["user_data_dir"]:
userdir = create_userdata_dir(args["user_data_dir"], create_dir=True)
copy_sample_files(userdir)
copy_sample_files(userdir, overwrite=args["reset"])
else:
logger.warning("`create-userdir` requires --userdir to be set.")
sys.exit(1)

View File

@ -80,4 +80,7 @@ def test_copy_sample_files_errors(mocker, default_conf, caplog) -> None:
mocker.patch.object(Path, "is_dir", MagicMock(return_value=True))
mocker.patch.object(Path, "exists", MagicMock(return_value=True))
copy_sample_files(Path('/tmp/bar'))
assert log_has_re(r"File `.*` exists already, not deploying sample.*", caplog)
assert log_has_re(r"File `.*` exists already, not deploying sample file\.", caplog)
caplog.clear()
copy_sample_files(Path('/tmp/bar'), overwrite=True)
assert log_has_re(r"File `.*` exists already, overwriting\.", caplog)