• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

freqtrade / freqtrade / 9394559170

26 Apr 2024 06:36AM UTC coverage: 94.656% (-0.02%) from 94.674%
9394559170

push

github

xmatthias
Loader should be passed as kwarg for clarity

20280 of 21425 relevant lines covered (94.66%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.23
/freqtrade/configuration/directory_operations.py
1
import logging
1✔
2
import shutil
1✔
3
from pathlib import Path
1✔
4
from typing import Optional
1✔
5

6
from freqtrade.configuration.detect_environment import running_in_docker
1✔
7
from freqtrade.constants import (USER_DATA_FILES, USERPATH_FREQAIMODELS, USERPATH_HYPEROPTS,
1✔
8
                                 USERPATH_NOTEBOOKS, USERPATH_STRATEGIES, Config)
9
from freqtrade.exceptions import OperationalException
1✔
10

11

12
logger = logging.getLogger(__name__)
1✔
13

14

15
def create_datadir(config: Config, datadir: Optional[str] = None) -> Path:
1✔
16

17
    folder = Path(datadir) if datadir else Path(f"{config['user_data_dir']}/data")
1✔
18
    if not datadir:
1✔
19
        # set datadir
20
        exchange_name = config.get('exchange', {}).get('name', '').lower()
1✔
21
        folder = folder.joinpath(exchange_name)
1✔
22

23
    if not folder.is_dir():
1✔
24
        folder.mkdir(parents=True)
1✔
25
        logger.info(f'Created data directory: {datadir}')
1✔
26
    return folder
1✔
27

28

29
def chown_user_directory(directory: Path) -> None:
1✔
30
    """
31
    Use Sudo to change permissions of the home-directory if necessary
32
    Only applies when running in docker!
33
    """
34
    if running_in_docker():
1✔
35
        try:
1✔
36
            import subprocess
1✔
37
            subprocess.check_output(
1✔
38
                ['sudo', 'chown', '-R', 'ftuser:', str(directory.resolve())])
39
        except Exception:
×
40
            logger.warning(f"Could not chown {directory}")
×
41

42

43
def create_userdata_dir(directory: str, create_dir: bool = False) -> Path:
1✔
44
    """
45
    Create userdata directory structure.
46
    if create_dir is True, then the parent-directory will be created if it does not exist.
47
    Sub-directories will always be created if the parent directory exists.
48
    Raises OperationalException if given a non-existing directory.
49
    :param directory: Directory to check
50
    :param create_dir: Create directory if it does not exist.
51
    :return: Path object containing the directory
52
    """
53
    sub_dirs = ["backtest_results", "data", USERPATH_HYPEROPTS, "hyperopt_results", "logs",
1✔
54
                USERPATH_NOTEBOOKS, "plot", USERPATH_STRATEGIES, USERPATH_FREQAIMODELS]
55
    folder = Path(directory)
1✔
56
    chown_user_directory(folder)
1✔
57
    if not folder.is_dir():
1✔
58
        if create_dir:
1✔
59
            folder.mkdir(parents=True)
1✔
60
            logger.info(f'Created user-data directory: {folder}')
1✔
61
        else:
62
            raise OperationalException(
1✔
63
                f"Directory `{folder}` does not exist. "
64
                "Please use `freqtrade create-userdir` to create a user directory")
65

66
    # Create required subdirectories
67
    for f in sub_dirs:
1✔
68
        subfolder = folder / f
1✔
69
        if not subfolder.is_dir():
1✔
70
            subfolder.mkdir(parents=False)
1✔
71
    return folder
1✔
72

73

74
def copy_sample_files(directory: Path, overwrite: bool = False) -> None:
1✔
75
    """
76
    Copy files from templates to User data directory.
77
    :param directory: Directory to copy data to
78
    :param overwrite: Overwrite existing sample files
79
    """
80
    if not directory.is_dir():
1✔
81
        raise OperationalException(f"Directory `{directory}` does not exist.")
1✔
82
    sourcedir = Path(__file__).parents[1] / "templates"
1✔
83
    for source, target in USER_DATA_FILES.items():
1✔
84
        targetdir = directory / target
1✔
85
        if not targetdir.is_dir():
1✔
86
            raise OperationalException(f"Directory `{targetdir}` does not exist.")
1✔
87
        targetfile = targetdir / source
1✔
88
        if targetfile.exists():
1✔
89
            if not overwrite:
1✔
90
                logger.warning(f"File `{targetfile}` exists already, not deploying sample file.")
1✔
91
                continue
1✔
92
            logger.warning(f"File `{targetfile}` exists already, overwriting.")
1✔
93
        shutil.copy(str(sourcedir / source), str(targetfile))
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc