Skip to content

Commit

Permalink
Only require -b <brokername> for filtering
Browse files Browse the repository at this point in the history
Instead of requiring any `-b` try to import all built-in broker backend
python modules by default and only load those detected from the input symbol
list's fqsn values. In other words the `piker chart` cmd can be run sin
`-b` now and that flag is only required if you only want to load
a subset of the built-ins or are trying to load a specific
not-yet-builtin backend.
  • Loading branch information
goodboy committed Nov 17, 2022
1 parent 4e6cd33 commit 4948bae
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
14 changes: 8 additions & 6 deletions piker/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@


log = get_logger('cli')
DEFAULT_BROKER = 'questrade'


@click.command()
Expand Down Expand Up @@ -118,7 +117,7 @@ async def main():
@click.group(context_settings=config._context_defaults)
@click.option(
'--brokers', '-b',
default=[DEFAULT_BROKER],
default=None,
multiple=True,
help='Broker backend to use'
)
Expand All @@ -144,10 +143,13 @@ def cli(

ctx.ensure_object(dict)

if len(brokers) == 1:
brokermods = [get_brokermod(brokers[0])]
else:
brokermods = [get_brokermod(broker) for broker in brokers]
if not brokers:
# (try to) load all (supposedly) supported data/broker backends
from piker.brokers import __brokers__
brokers = __brokers__

brokermods = [get_brokermod(broker) for broker in brokers]
assert brokermods

reg_addr: None | tuple[str, int] = None
if host or port:
Expand Down
37 changes: 25 additions & 12 deletions piker/ui/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
'''
from functools import partial
from types import ModuleType

from PyQt5.QtCore import QEvent
import trio

from .._daemon import maybe_spawn_brokerd
from ..brokers import get_brokermod
from . import _event
from ._exec import run_qtractor
from ..data.feed import install_brokerd_search
from ..data._source import unpack_fqsn
from . import _search
from ._chart import GodWidget
from ..log import get_logger
Expand All @@ -36,27 +37,26 @@


async def load_provider_search(

broker: str,
brokermod: str,
loglevel: str,

) -> None:

log.info(f'loading brokerd for {broker}..')
name = brokermod.name
log.info(f'loading brokerd for {name}..')

async with (

maybe_spawn_brokerd(
broker,
name,
loglevel=loglevel
) as portal,

install_brokerd_search(
portal,
get_brokermod(broker),
brokermod,
),
):

# keep search engine stream up until cancelled
await trio.sleep_forever()

Expand All @@ -67,7 +67,7 @@ async def _async_main(
main_widget: GodWidget,

syms: list[str],
brokernames: str,
brokers: dict[str, ModuleType],
loglevel: str,

) -> None:
Expand Down Expand Up @@ -99,6 +99,11 @@ async def _async_main(
sbar = godwidget.window.status_bar
starting_done = sbar.open_status('starting ze sexy chartz')

needed_brokermods: dict[str, ModuleType] = {}
for fqsn in syms:
brokername, *_ = unpack_fqsn(fqsn)
needed_brokermods[brokername] = brokers[brokername]

async with (
trio.open_nursery() as root_n,
):
Expand Down Expand Up @@ -140,8 +145,12 @@ async def _async_main(
):
# load other providers into search **after**
# the chart's select cache
for broker in brokernames:
root_n.start_soon(load_provider_search, broker, loglevel)
for brokername, mod in needed_brokermods.items():
root_n.start_soon(
load_provider_search,
mod,
loglevel,
)

await order_mode_ready.wait()

Expand Down Expand Up @@ -171,7 +180,7 @@ async def _async_main(

def _main(
syms: list[str],
brokernames: [str],
brokermods: list[ModuleType],
piker_loglevel: str,
tractor_kwargs,
) -> None:
Expand All @@ -182,7 +191,11 @@ def _main(
'''
run_qtractor(
func=_async_main,
args=(syms, brokernames, piker_loglevel),
args=(
syms,
{mod.name: mod for mod in brokermods},
piker_loglevel,
),
main_widget_type=GodWidget,
tractor_kwargs=tractor_kwargs,
)
6 changes: 3 additions & 3 deletions piker/ui/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,16 @@ def chart(
))
return


# global opts
brokernames = config['brokers']
brokermods = config['brokermods']
assert brokermods
tractorloglevel = config['tractorloglevel']
pikerloglevel = config['loglevel']


_main(
syms=symbols,
brokernames=brokernames,
brokermods=brokermods,
piker_loglevel=pikerloglevel,
tractor_kwargs={
'debug_mode': pdb,
Expand Down

0 comments on commit 4948bae

Please sign in to comment.