2020-11-06 17:23:14 +00:00
|
|
|
# piker: trading gear for hackers
|
|
|
|
# Copyright (C) 2018-present Tyler Goodlet (in stewardship of piker0)
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2018-01-23 02:26:38 +00:00
|
|
|
"""
|
|
|
|
Broker configuration mgmt.
|
|
|
|
"""
|
2019-02-26 00:29:54 +00:00
|
|
|
import os
|
2021-05-22 19:11:23 +00:00
|
|
|
from os.path import dirname
|
|
|
|
import shutil
|
2021-08-23 18:42:26 +00:00
|
|
|
from typing import Optional
|
2021-05-22 19:11:23 +00:00
|
|
|
|
2021-09-08 18:01:54 +00:00
|
|
|
from bidict import bidict
|
2019-03-15 23:37:04 +00:00
|
|
|
import toml
|
2018-02-08 07:25:10 +00:00
|
|
|
import click
|
2021-05-22 19:11:23 +00:00
|
|
|
|
2021-09-07 01:26:28 +00:00
|
|
|
from .log import get_logger
|
2018-01-23 02:26:38 +00:00
|
|
|
|
|
|
|
log = get_logger('broker-config')
|
|
|
|
|
2018-03-06 00:05:36 +00:00
|
|
|
_config_dir = click.get_app_dir('piker')
|
2019-03-15 23:37:04 +00:00
|
|
|
_file_name = 'brokers.toml'
|
2018-01-23 02:26:38 +00:00
|
|
|
|
|
|
|
|
2019-02-26 00:29:54 +00:00
|
|
|
def _override_config_dir(
|
|
|
|
path: str
|
|
|
|
) -> None:
|
|
|
|
global _config_dir
|
|
|
|
_config_dir = path
|
|
|
|
|
|
|
|
|
|
|
|
def get_broker_conf_path():
|
2021-03-19 17:23:33 +00:00
|
|
|
"""Return the default config path normally under
|
|
|
|
``~/.config/piker`` on linux.
|
|
|
|
|
|
|
|
Contains files such as:
|
|
|
|
- brokers.toml
|
|
|
|
- watchlists.toml
|
|
|
|
- signals.toml
|
|
|
|
- strats.toml
|
|
|
|
|
|
|
|
"""
|
2019-02-26 00:29:54 +00:00
|
|
|
return os.path.join(_config_dir, _file_name)
|
|
|
|
|
|
|
|
|
2021-05-22 19:11:23 +00:00
|
|
|
def repodir():
|
|
|
|
"""Return the abspath to the repo directory.
|
|
|
|
"""
|
|
|
|
dirpath = os.path.abspath(
|
|
|
|
# we're 3 levels down in **this** module file
|
2021-09-16 20:34:43 +00:00
|
|
|
dirname(dirname(os.path.realpath(__file__)))
|
2021-05-22 19:11:23 +00:00
|
|
|
)
|
|
|
|
return dirpath
|
|
|
|
|
|
|
|
|
2019-02-26 00:29:54 +00:00
|
|
|
def load(
|
|
|
|
path: str = None
|
2019-03-15 23:37:04 +00:00
|
|
|
) -> (dict, str):
|
2018-01-23 02:26:38 +00:00
|
|
|
"""Load broker config.
|
|
|
|
"""
|
2019-02-26 00:29:54 +00:00
|
|
|
path = path or get_broker_conf_path()
|
2021-05-22 19:11:23 +00:00
|
|
|
if not os.path.isfile(path):
|
|
|
|
shutil.copyfile(
|
2021-09-16 20:34:43 +00:00
|
|
|
os.path.join(repodir(), 'config', 'brokers.toml'),
|
2021-05-22 19:11:23 +00:00
|
|
|
path,
|
|
|
|
)
|
|
|
|
|
2019-03-15 23:37:04 +00:00
|
|
|
config = toml.load(path)
|
2019-02-04 05:17:11 +00:00
|
|
|
log.debug(f"Read config file {path}")
|
|
|
|
return config, path
|
2018-01-23 02:26:38 +00:00
|
|
|
|
|
|
|
|
2019-02-26 00:29:54 +00:00
|
|
|
def write(
|
2019-03-15 23:37:04 +00:00
|
|
|
config: dict, # toml config as dict
|
2019-02-26 00:29:54 +00:00
|
|
|
path: str = None,
|
|
|
|
) -> None:
|
2018-01-23 02:26:38 +00:00
|
|
|
"""Write broker config to disk.
|
2018-03-06 00:05:36 +00:00
|
|
|
|
|
|
|
Create a ``brokers.ini`` file if one does not exist.
|
2018-01-23 02:26:38 +00:00
|
|
|
"""
|
2019-02-26 00:29:54 +00:00
|
|
|
path = path or get_broker_conf_path()
|
|
|
|
dirname = os.path.dirname(path)
|
|
|
|
if not os.path.isdir(dirname):
|
2018-03-06 00:05:36 +00:00
|
|
|
log.debug(f"Creating config dir {_config_dir}")
|
2019-02-26 00:29:54 +00:00
|
|
|
os.makedirs(dirname)
|
2018-03-06 00:05:36 +00:00
|
|
|
|
2019-03-15 23:37:04 +00:00
|
|
|
if not config:
|
|
|
|
raise ValueError(
|
|
|
|
"Watch out you're trying to write a blank config!")
|
|
|
|
|
2019-02-26 00:29:54 +00:00
|
|
|
log.debug(f"Writing config file {path}")
|
|
|
|
with open(path, 'w') as cf:
|
2019-03-15 23:37:04 +00:00
|
|
|
return toml.dump(config, cf)
|
2021-08-23 18:42:26 +00:00
|
|
|
|
|
|
|
|
2021-09-08 18:01:54 +00:00
|
|
|
def load_accounts(
|
2021-09-09 14:37:20 +00:00
|
|
|
|
2021-09-10 15:35:30 +00:00
|
|
|
providers: Optional[list[str]] = None
|
2021-09-08 18:01:54 +00:00
|
|
|
|
|
|
|
) -> bidict[str, Optional[str]]:
|
2021-08-23 18:42:26 +00:00
|
|
|
|
|
|
|
conf, path = load()
|
2021-09-10 15:35:30 +00:00
|
|
|
accounts = bidict()
|
2021-09-09 14:37:20 +00:00
|
|
|
for provider_name, section in conf.items():
|
|
|
|
accounts_section = section.get('accounts')
|
|
|
|
if (
|
2021-09-10 15:35:30 +00:00
|
|
|
providers is None or
|
|
|
|
providers and provider_name in providers
|
2021-09-09 14:37:20 +00:00
|
|
|
):
|
|
|
|
if accounts_section is None:
|
|
|
|
log.warning(f'No accounts named for {provider_name}?')
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
for label, value in accounts_section.items():
|
|
|
|
accounts[
|
|
|
|
f'{provider_name}.{label}'
|
|
|
|
] = value
|
2021-08-23 18:42:26 +00:00
|
|
|
|
2021-09-10 15:35:30 +00:00
|
|
|
# our default paper engine entry
|
|
|
|
accounts['paper'] = None
|
2021-08-23 18:42:26 +00:00
|
|
|
return accounts
|