stable/freqtrade/configuration/directory_operations.py

77 lines
2.9 KiB
Python
Raw Normal View History

2019-07-12 00:26:27 +00:00
import logging
2019-11-01 12:28:35 +00:00
import shutil
2019-07-17 18:53:29 +00:00
from pathlib import Path
2019-11-01 12:28:35 +00:00
from typing import Any, Dict, Optional
2019-07-12 00:26:27 +00:00
2019-11-01 12:28:35 +00:00
from freqtrade.constants import USER_DATA_FILES
2020-09-28 17:39:41 +00:00
from freqtrade.exceptions import OperationalException
2019-07-12 00:26:27 +00:00
logger = logging.getLogger(__name__)
def create_datadir(config: Dict[str, Any], datadir: Optional[str] = None) -> Path:
2019-07-17 18:53:29 +00:00
2019-07-21 12:32:29 +00:00
folder = Path(datadir) if datadir else Path(f"{config['user_data_dir']}/data")
2019-07-12 00:26:27 +00:00
if not datadir:
# set datadir
exchange_name = config.get('exchange', {}).get('name').lower()
2019-07-17 18:53:29 +00:00
folder = folder.joinpath(exchange_name)
2019-07-12 00:26:27 +00:00
2019-07-17 18:53:29 +00:00
if not folder.is_dir():
folder.mkdir(parents=True)
2019-07-12 00:26:27 +00:00
logger.info(f'Created data directory: {datadir}')
return folder
2019-07-21 11:42:56 +00:00
2020-02-02 04:00:40 +00:00
def create_userdata_dir(directory: str, create_dir: bool = False) -> Path:
"""
Create userdata directory structure.
if create_dir is True, then the parent-directory will be created if it does not exist.
Sub-directories will always be created if the parent directory exists.
Raises OperationalException if given a non-existing directory.
:param directory: Directory to check
:param create_dir: Create directory if it does not exist.
:return: Path object containing the directory
"""
2020-04-21 17:47:49 +00:00
sub_dirs = ["backtest_results", "data", "hyperopts", "hyperopt_results", "logs",
"notebooks", "plot", "strategies", ]
2019-07-21 11:42:56 +00:00
folder = Path(directory)
if not folder.is_dir():
if create_dir:
folder.mkdir(parents=True)
logger.info(f'Created user-data directory: {folder}')
else:
raise OperationalException(
f"Directory `{folder}` does not exist. "
"Please use `freqtrade create-userdir` to create a user directory")
2019-07-21 11:42:56 +00:00
# Create required subdirectories
for f in sub_dirs:
subfolder = folder / f
if not subfolder.is_dir():
subfolder.mkdir(parents=False)
2019-07-21 13:49:52 +00:00
return folder
2019-11-01 12:28:35 +00:00
2019-11-01 13:08:55 +00:00
def copy_sample_files(directory: Path, overwrite: bool = False) -> None:
2019-11-01 12:28:35 +00:00
"""
Copy files from templates to User data directory.
:param directory: Directory to copy data to
2019-11-01 13:08:55 +00:00
:param overwrite: Overwrite existing sample files
2019-11-01 12:28:35 +00:00
"""
if not directory.is_dir():
raise OperationalException(f"Directory `{directory}` does not exist.")
sourcedir = Path(__file__).parents[1] / "templates"
for source, target in USER_DATA_FILES.items():
targetdir = directory / target
if not targetdir.is_dir():
raise OperationalException(f"Directory `{targetdir}` does not exist.")
targetfile = targetdir / source
if targetfile.exists():
2019-11-01 13:08:55 +00:00
if not overwrite:
logger.warning(f"File `{targetfile}` exists already, not deploying sample file.")
continue
logger.warning(f"File `{targetfile}` exists already, overwriting.")
2019-11-01 12:28:35 +00:00
shutil.copy(str(sourcedir / source), str(targetfile))