Use `Client` in watchlist app

kivy_mainline_and_py3.8
Tyler Goodlet 2018-04-19 00:27:04 -04:00
parent 17feb17535
commit 4123139750
1 changed files with 70 additions and 65 deletions

View File

@ -319,7 +319,7 @@ async def update_quotes(
nursery: 'Nursery', nursery: 'Nursery',
brokermod: ModuleType, brokermod: ModuleType,
widgets: dict, widgets: dict,
queue: 'StreamQueue', client: 'Client',
symbol_data: dict, symbol_data: dict,
first_quotes: dict first_quotes: dict
): ):
@ -359,7 +359,7 @@ async def update_quotes(
grid.render_rows(cache) grid.render_rows(cache)
# core cell update loop # core cell update loop
async for quotes in queue: # new quotes data only async for quotes in client.aiter_recv(): # new quotes data only
for symbol, quote in quotes.items(): for symbol, quote in quotes.items():
record, displayable = brokermod.format_quote( record, displayable = brokermod.format_quote(
quote, symbol_data=symbol_data) quote, symbol_data=symbol_data)
@ -374,6 +374,7 @@ async def update_quotes(
log.warn("Server connection dropped") log.warn("Server connection dropped")
nursery.cancel_scope.cancel() nursery.cancel_scope.cancel()
async def run_kivy(root, nursery): async def run_kivy(root, nursery):
'''Trio-kivy entry point. '''Trio-kivy entry point.
''' '''
@ -387,76 +388,80 @@ async def _async_main(name, tickers, brokermod, rate):
This is started with cli command `piker watch`. This is started with cli command `piker watch`.
''' '''
# setup ticker stream # setup ticker stream
from ..brokers.core import StreamQueue from ..brokers.core import Client
queue = StreamQueue(await trio.open_tcp_stream('127.0.0.1', 1616))
await queue.put((brokermod.name, tickers)) # initial request for symbols price streams
# get initial symbol data async def subscribe(client):
async with brokermod.get_client() as client: # initial request for symbols price streams
# get long term data including last days close price await client.send((brokermod.name, tickers))
sd = await client.symbol_data(tickers)
async with trio.open_nursery() as nursery: async with Client(('127.0.0.1', 1616), subscribe) as client:
# get first quotes response
quotes = await queue.get()
first_quotes = [
brokermod.format_quote(quote, symbol_data=sd)[0]
for quote in quotes.values()]
if first_quotes[0].get('last') is None: # get initial symbol data
log.error("Broker API is down temporarily") async with brokermod.get_client() as bclient:
nursery.cancel_scope.cancel() # get long term data including last days close price
return sd = await bclient.symbol_data(tickers)
# build out UI async with trio.open_nursery() as nursery:
Window.set_title(f"watchlist: {name}\t(press ? for help)") # get first quotes response
Builder.load_string(_kv) quotes = await client.recv()
box = BoxLayout(orientation='vertical', padding=5, spacing=5) first_quotes = [
brokermod.format_quote(quote, symbol_data=sd)[0]
for quote in quotes.values()]
# define bid-ask "stacked" cells if first_quotes[0].get('last') is None:
# (TODO: needs some rethinking and renaming for sure) log.error("Broker API is down temporarily")
bidasks = brokermod._bidasks nursery.cancel_scope.cancel()
return
# add header row # build out UI
headers = first_quotes[0].keys() Window.set_title(f"watchlist: {name}\t(press ? for help)")
header = Row( Builder.load_string(_kv)
{key: key for key in headers}, box = BoxLayout(orientation='vertical', padding=5, spacing=5)
headers=headers,
bidasks=bidasks,
is_header_row=True,
size_hint=(1, None),
)
box.add_widget(header)
# build grid # define bid-ask "stacked" cells
grid = TickerTable( # (TODO: needs some rethinking and renaming for sure)
cols=1, bidasks = brokermod._bidasks
size_hint=(1, None),
)
for ticker_record in first_quotes:
grid.append_row(ticker_record, bidasks=bidasks)
# associate the col headers row with the ticker table even though
# they're technically wrapped separately in containing BoxLayout
header.table = grid
# mark the initial sorted column header as bold and underlined # add header row
sort_cell = header.get_cell(grid.sort_key) headers = first_quotes[0].keys()
sort_cell.bold = sort_cell.underline = True header = Row(
grid.last_clicked_col_cell = sort_cell {key: key for key in headers},
headers=headers,
bidasks=bidasks,
is_header_row=True,
size_hint=(1, None),
)
box.add_widget(header)
# set up a pager view for large ticker lists # build grid
grid.bind(minimum_height=grid.setter('height')) grid = TickerTable(
pager = PagerView(box, grid, nursery) cols=1,
box.add_widget(pager) size_hint=(1, None),
)
for ticker_record in first_quotes:
grid.append_row(ticker_record, bidasks=bidasks)
# associate the col headers row with the ticker table even though
# they're technically wrapped separately in containing BoxLayout
header.table = grid
widgets = { # mark the initial sorted column header as bold and underlined
# 'anchor': anchor, sort_cell = header.get_cell(grid.sort_key)
'root': box, sort_cell.bold = sort_cell.underline = True
'grid': grid, grid.last_clicked_col_cell = sort_cell
'box': box,
'header': header, # set up a pager view for large ticker lists
'pager': pager, grid.bind(minimum_height=grid.setter('height'))
} pager = PagerView(box, grid, nursery)
nursery.start_soon(run_kivy, widgets['root'], nursery) box.add_widget(pager)
nursery.start_soon(
update_quotes, nursery, brokermod, widgets, queue, sd, quotes) widgets = {
# 'anchor': anchor,
'root': box,
'grid': grid,
'box': box,
'header': header,
'pager': pager,
}
nursery.start_soon(run_kivy, widgets['root'], nursery)
nursery.start_soon(
update_quotes, nursery, brokermod, widgets, client, sd, quotes)