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.
account_tests
Tyler Goodlet 2023-07-17 17:12:40 -04:00
parent 912f1bc635
commit dfa13afe22
1 changed files with 27 additions and 8 deletions

View File

@ -303,11 +303,25 @@ class SymbologyCache(Struct):
_caches: dict[str, SymbologyCache] = {} _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 @acm
async def open_symcache( async def open_symcache(
mod_or_name: ModuleType | str, mod_or_name: ModuleType | str,
reload: bool = False, reload: bool = False,
only_from_memcache: bool = False, only_from_memcache: bool = False, # no API req
) -> SymbologyCache: ) -> SymbologyCache:
@ -317,6 +331,18 @@ async def open_symcache(
mod: ModuleType = mod_or_name mod: ModuleType = mod_or_name
provider: str = mod.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 # actor-level cache-cache XD
global _caches global _caches
@ -332,13 +358,6 @@ async def open_symcache(
else: else:
log.warning(msg) 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 # if no cache exists or an explicit reload is requested, load
# the provider API and call appropriate endpoints to populate # the provider API and call appropriate endpoints to populate
# the mkt and asset tables. # the mkt and asset tables.