stable/freqtrade/configuration/directory_operations.py

72 lines
2.6 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
from freqtrade import OperationalException
2019-11-01 12:28:35 +00:00
from freqtrade.constants import USER_DATA_FILES
2019-07-12 00:26:27 +00:00
logger = logging.getLogger(__name__)
def create_datadir(config: Dict[str, Any], datadir: Optional[str] = None) -> str:
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}')
2019-07-17 18:53:29 +00:00
return str(folder)
2019-07-21 11:42:56 +00:00
def create_userdata_dir(directory: str, create_dir=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
"""
2019-07-31 05:07:46 +00:00
sub_dirs = ["backtest_results", "data", "hyperopts", "hyperopt_results", "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
def copy_sample_files(directory: Path) -> None:
"""
Copy files from templates to User data directory.
:param directory: Directory to copy data to
"""
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():
logger.warning(f"File `{targetfile}` exists already, not deploying sample file.")
continue
shutil.copy(str(sourcedir / source), str(targetfile))