diff --git a/piker/_cacheables.py b/piker/_cacheables.py index 279f3188..43457364 100644 --- a/piker/_cacheables.py +++ b/piker/_cacheables.py @@ -20,9 +20,6 @@ Cacheing apis and toolz. ''' from collections import OrderedDict -from contextlib import ( - asynccontextmanager as acm, -) from typing import ( Awaitable, Callable, @@ -30,12 +27,8 @@ from typing import ( TypeVar, ) -from tractor.trionics import maybe_open_context - -from .brokers import get_brokermod from .log import get_logger - log = get_logger(__name__) T = TypeVar("T") @@ -104,29 +97,3 @@ def async_lifo_cache( return decorated return decorator - - -# TODO: move this to `.brokers.utils`.. -@acm -async def open_cached_client( - brokername: str, - **kwargs, - -) -> 'Client': # noqa - ''' - Get a cached broker client from the current actor's local vars. - - If one has not been setup do it and cache it. - - ''' - brokermod = get_brokermod(brokername) - async with maybe_open_context( - acm_func=brokermod.get_client, - kwargs=kwargs, - - ) as (cache_hit, client): - - if cache_hit: - log.info(f'Reusing existing {client}') - - yield client diff --git a/piker/brokers/__init__.py b/piker/brokers/__init__.py index 93393654..986905e2 100644 --- a/piker/brokers/__init__.py +++ b/piker/brokers/__init__.py @@ -17,10 +17,34 @@ """ Broker clients, daemons and general back end machinery. """ +from contextlib import ( + asynccontextmanager as acm, +) from importlib import import_module from types import ModuleType -__brokers__ = [ +from tractor.trionics import maybe_open_context + +from ._util import ( + log, + BrokerError, + SymbolNotFound, + NoData, + DataUnavailable, + DataThrottle, + resproc, +) + +__all__: list[str] = [ + 'BrokerError', + 'SymbolNotFound', + 'NoData', + 'DataUnavailable', + 'DataThrottle', + 'resproc', +] + +__brokers__: list[str] = [ 'binance', 'ib', 'kraken', @@ -58,3 +82,28 @@ def iter_brokermods(): ''' for name in __brokers__: yield get_brokermod(name) + + +@acm +async def open_cached_client( + brokername: str, + **kwargs, + +) -> 'Client': # noqa + ''' + Get a cached broker client from the current actor's local vars. + + If one has not been setup do it and cache it. + + ''' + brokermod = get_brokermod(brokername) + async with maybe_open_context( + acm_func=brokermod.get_client, + kwargs=kwargs, + + ) as (cache_hit, client): + + if cache_hit: + log.info(f'Reusing existing {client}') + + yield client diff --git a/piker/brokers/core.py b/piker/brokers/core.py index f4d670dc..a9a83e7c 100644 --- a/piker/brokers/core.py +++ b/piker/brokers/core.py @@ -29,7 +29,7 @@ import trio from ._util import log from . import get_brokermod from ..service import maybe_spawn_brokerd -from .._cacheables import open_cached_client +from . import open_cached_client from ..accounting import MktPair diff --git a/piker/brokers/deribit/feed.py b/piker/brokers/deribit/feed.py index 04357ef8..ca6a3f54 100644 --- a/piker/brokers/deribit/feed.py +++ b/piker/brokers/deribit/feed.py @@ -30,7 +30,7 @@ from fuzzywuzzy import process as fuzzy import numpy as np import tractor -from piker._cacheables import open_cached_client +from piker.brokers import open_cached_client from piker.log import get_logger, get_console_log from piker.data import ShmArray from piker.brokers._util import ( diff --git a/piker/brokers/kraken/feed.py b/piker/brokers/kraken/feed.py index 02b2866a..dc70672f 100644 --- a/piker/brokers/kraken/feed.py +++ b/piker/brokers/kraken/feed.py @@ -42,8 +42,10 @@ from piker.accounting._mktinfo import ( Asset, MktPair, ) -from piker._cacheables import ( +from piker.brokers import ( open_cached_client, +) +from piker._cacheables import ( async_lifo_cache, ) from piker.brokers._util import ( diff --git a/piker/brokers/kucoin.py b/piker/brokers/kucoin.py index eebbc5f7..b2953467 100755 --- a/piker/brokers/kucoin.py +++ b/piker/brokers/kucoin.py @@ -56,8 +56,10 @@ from piker.accounting._mktinfo import ( MktPair, ) from piker import config -from piker._cacheables import ( +from piker.brokers import ( open_cached_client, +) +from piker._cacheables import ( async_lifo_cache, ) from piker.log import get_logger diff --git a/piker/brokers/questrade.py b/piker/brokers/questrade.py index 1d447b23..31133f23 100644 --- a/piker/brokers/questrade.py +++ b/piker/brokers/questrade.py @@ -40,7 +40,8 @@ import wrapt import asks from ..calc import humanize, percent_change -from .._cacheables import open_cached_client, async_lifo_cache +from . import open_cached_client +from piker._cacheables import async_lifo_cache from .. import config from ._util import resproc, BrokerError, SymbolNotFound from ..log import ( diff --git a/piker/ui/_fsp.py b/piker/ui/_fsp.py index f00b1e3d..5202ea97 100644 --- a/piker/ui/_fsp.py +++ b/piker/ui/_fsp.py @@ -30,14 +30,14 @@ from typing import ( ) import msgspec -import tractor import pyqtgraph as pg +import tractor +from tractor.trionics import maybe_open_context import trio from trio_typing import TaskStatus from piker.data.types import Struct from ._axes import PriceAxis -from .._cacheables import maybe_open_context from ..calc import humanize from ..data._sharedmem import ( ShmArray,