2020-05-23 19:39:17 +00:00
|
|
|
"""
|
|
|
|
CLI commons.
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
|
|
|
|
import click
|
|
|
|
import tractor
|
|
|
|
|
|
|
|
from ..log import get_console_log, get_logger
|
|
|
|
from ..brokers import get_brokermod, config
|
|
|
|
|
2020-07-15 12:28:13 +00:00
|
|
|
|
2020-05-23 19:39:17 +00:00
|
|
|
log = get_logger('cli')
|
|
|
|
DEFAULT_BROKER = 'questrade'
|
|
|
|
|
2020-05-26 17:08:16 +00:00
|
|
|
_config_dir = click.get_app_dir('piker')
|
|
|
|
_watchlists_data_path = os.path.join(_config_dir, 'watchlists.json')
|
2020-05-23 19:39:17 +00:00
|
|
|
_context_defaults = dict(
|
|
|
|
default_map={
|
2020-06-10 17:48:21 +00:00
|
|
|
# Questrade specific quote poll rates
|
2020-05-23 19:39:17 +00:00
|
|
|
'monitor': {
|
|
|
|
'rate': 3,
|
|
|
|
},
|
|
|
|
'optschain': {
|
|
|
|
'rate': 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.option('--loglevel', '-l', default='warning', help='Logging level')
|
|
|
|
@click.option('--tl', is_flag=True, help='Enable tractor logging')
|
|
|
|
@click.option('--host', '-h', default='127.0.0.1', help='Host address to bind')
|
|
|
|
def pikerd(loglevel, host, tl):
|
|
|
|
"""Spawn the piker broker-daemon.
|
|
|
|
"""
|
2020-07-15 12:28:13 +00:00
|
|
|
from ..data import _data_mods
|
2020-05-23 19:39:17 +00:00
|
|
|
get_console_log(loglevel)
|
|
|
|
tractor.run_daemon(
|
|
|
|
rpc_module_paths=_data_mods,
|
|
|
|
name='brokerd',
|
|
|
|
loglevel=loglevel if tl else None,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@click.group(context_settings=_context_defaults)
|
|
|
|
@click.option('--broker', '-b', default=DEFAULT_BROKER,
|
|
|
|
help='Broker backend to use')
|
|
|
|
@click.option('--loglevel', '-l', default='warning', help='Logging level')
|
2020-08-02 02:23:19 +00:00
|
|
|
@click.option('--tl', is_flag=True, help='Enable tractor logging')
|
2020-05-23 19:39:17 +00:00
|
|
|
@click.option('--configdir', '-c', help='Configuration directory')
|
|
|
|
@click.pass_context
|
2020-08-02 02:23:19 +00:00
|
|
|
def cli(ctx, broker, loglevel, tl, configdir):
|
2020-05-23 19:39:17 +00:00
|
|
|
if configdir is not None:
|
|
|
|
assert os.path.isdir(configdir), f"`{configdir}` is not a valid path"
|
|
|
|
config._override_config_dir(configdir)
|
|
|
|
|
|
|
|
ctx.ensure_object(dict)
|
|
|
|
ctx.obj.update({
|
|
|
|
'broker': broker,
|
|
|
|
'brokermod': get_brokermod(broker),
|
|
|
|
'loglevel': loglevel,
|
2020-08-02 02:23:19 +00:00
|
|
|
'tractorloglevel': None,
|
2020-05-23 19:39:17 +00:00
|
|
|
'log': get_console_log(loglevel),
|
2020-05-26 17:08:16 +00:00
|
|
|
'confdir': _config_dir,
|
|
|
|
'wl_path': _watchlists_data_path,
|
2020-05-23 19:39:17 +00:00
|
|
|
})
|
|
|
|
|
2020-08-02 02:23:19 +00:00
|
|
|
# allow enabling same loglevel in ``tractor`` machinery
|
|
|
|
if tl:
|
|
|
|
ctx.obj.update({'tractorloglevel': loglevel})
|
|
|
|
|
2020-05-23 19:39:17 +00:00
|
|
|
|
2020-07-15 12:28:13 +00:00
|
|
|
def _load_clis() -> None:
|
|
|
|
from ..data import marketstore as _
|
|
|
|
from ..brokers import cli as _ # noqa
|
|
|
|
from ..ui import cli as _ # noqa
|
|
|
|
from ..watchlists import cli as _ # noqa
|
|
|
|
|
|
|
|
|
2020-05-23 19:39:17 +00:00
|
|
|
# load downstream cli modules
|
2020-07-15 12:28:13 +00:00
|
|
|
_load_clis()
|