Convert create_datadir to Pathlib
This commit is contained in:
parent
fbd229810f
commit
8b4827ad85
@ -1,18 +1,20 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def create_datadir(config: Dict[str, Any], datadir: Optional[str] = None) -> str:
|
def create_datadir(config: Dict[str, Any], datadir: Optional[str] = None) -> str:
|
||||||
|
|
||||||
|
folder = Path(datadir) if datadir else Path('user_data/data')
|
||||||
if not datadir:
|
if not datadir:
|
||||||
# set datadir
|
# set datadir
|
||||||
exchange_name = config.get('exchange', {}).get('name').lower()
|
exchange_name = config.get('exchange', {}).get('name').lower()
|
||||||
datadir = os.path.join('user_data', 'data', exchange_name)
|
folder = folder.joinpath(exchange_name)
|
||||||
|
|
||||||
if not os.path.isdir(datadir):
|
if not folder.is_dir():
|
||||||
os.makedirs(datadir)
|
folder.mkdir(parents=True)
|
||||||
logger.info(f'Created data directory: {datadir}')
|
logger.info(f'Created data directory: {datadir}')
|
||||||
return datadir
|
return str(folder)
|
||||||
|
@ -39,7 +39,7 @@ class PairListResolver(IResolver):
|
|||||||
current_path = Path(__file__).parent.parent.joinpath('pairlist').resolve()
|
current_path = Path(__file__).parent.parent.joinpath('pairlist').resolve()
|
||||||
|
|
||||||
abs_paths = [
|
abs_paths = [
|
||||||
current_path.parent.parent.joinpath('user_data/pairlist'),
|
Path.cwd().joinpath('user_data/pairlist'),
|
||||||
current_path,
|
current_path,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -585,11 +585,11 @@ def test_validate_default_conf(default_conf) -> None:
|
|||||||
|
|
||||||
|
|
||||||
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.object(Path, "is_dir", MagicMock(return_value=False))
|
||||||
md = MagicMock()
|
md = mocker.patch.object(Path, 'mkdir', MagicMock())
|
||||||
mocker.patch('os.makedirs', md)
|
|
||||||
create_datadir(default_conf, '/foo/bar')
|
create_datadir(default_conf, '/foo/bar')
|
||||||
assert md.call_args[0][0] == "/foo/bar"
|
assert md.call_args[1]['parents'] is True
|
||||||
assert log_has('Created data directory: /foo/bar', caplog.record_tuples)
|
assert log_has('Created data directory: /foo/bar', caplog.record_tuples)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user