From dfa13afe22546e93382f113b7655e11528f38f2c Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 17 Jul 2023 17:12:40 -0400 Subject: [PATCH] Allow backends to "bypass" symcache loading Some backends like `ib` don't have an obvious (nor practical) way to easily download the entire symbology set available from all its mkt venues. For such backends loading might require a non-std approach (like using the contract search from some input mkt-key set) and can't be expected to necessarily be supported out of the box. As such, allow annotating a broker sub-pkg module with a `_no_symcache: bool = True` attr which will make `open_symcache()` yield early with an empty `SymbologyCache` instance for use by the caller to fill in the mkt and assets tables in whatever ad-hoc way desired. --- piker/data/_symcache.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/piker/data/_symcache.py b/piker/data/_symcache.py index ee67ce6a..1745d293 100644 --- a/piker/data/_symcache.py +++ b/piker/data/_symcache.py @@ -303,11 +303,25 @@ class SymbologyCache(Struct): _caches: dict[str, SymbologyCache] = {} +def mk_cachefile( + provider: str, +) -> Path: + cachedir: Path = config.get_conf_dir() / '_cache' + if not cachedir.is_dir(): + log.info(f'Creating `nativedb` director: {cachedir}') + cachedir.mkdir() + + cachefile: Path = cachedir / f'{str(provider)}.symcache.toml' + cachefile.touch() + return cachefile + + @acm async def open_symcache( mod_or_name: ModuleType | str, + reload: bool = False, - only_from_memcache: bool = False, + only_from_memcache: bool = False, # no API req ) -> SymbologyCache: @@ -317,6 +331,18 @@ async def open_symcache( mod: ModuleType = mod_or_name provider: str = mod.name + cachefile: Path = mk_cachefile(provider) + + # NOTE: certain backends might not support a symbology cache + # (easily) and thus we allow for an empty instance to be loaded + # and manually filled in at the whim of the caller presuming + # the backend pkg-module is annotated appropriately. + if getattr(mod, '_no_symcache', False): + yield SymbologyCache( + mod=mod, + fp=cachefile, + ) + return # actor-level cache-cache XD global _caches @@ -332,13 +358,6 @@ async def open_symcache( else: log.warning(msg) - cachedir: Path = config.get_conf_dir() / '_cache' - if not cachedir.is_dir(): - log.info(f'Creating `nativedb` director: {cachedir}') - cachedir.mkdir() - - cachefile: Path = cachedir / f'{str(provider)}.symcache.toml' - # if no cache exists or an explicit reload is requested, load # the provider API and call appropriate endpoints to populate # the mkt and asset tables.