From d0f72bf269cdfc68831c883684b526c73904c6a6 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 26 Jul 2023 12:27:26 -0400 Subject: [PATCH] Wrap symcache loading into `.from_scratch()` Since we need it both when explicitly reloading **and** whenever either the file or data in the file doesn't exist. --- piker/data/_symcache.py | 49 +++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/piker/data/_symcache.py b/piker/data/_symcache.py index 3bf30d48..e6c94fa6 100644 --- a/piker/data/_symcache.py +++ b/piker/data/_symcache.py @@ -274,6 +274,28 @@ class SymbologyCache(Struct): ) return cache + @staticmethod + async def from_scratch( + mod: ModuleType, + fp: Path, + **kwargs, + + ) -> SymbologyCache: + ''' + Generate (a) new symcache (contents) entirely from scratch + including all (TOML) serialized data and file. + + ''' + log.info(f'GENERATING symbology cache for `{mod.name}`') + cache = SymbologyCache( + mod=mod, + fp=fp, + **kwargs, + ) + await cache.load() + cache.write_config() + return cache + def search( self, pattern: str, @@ -370,17 +392,11 @@ async def open_symcache( reload or not cachefile.is_file() ): - cache = SymbologyCache( + cache = await SymbologyCache.from_scratch( mod=mod, fp=cachefile, ) - log.info(f'GENERATING symbology cache for `{mod.name}`') - await cache.load() - - # NOTE: only (re-)write if explicit reload or non-existing - cache.write_config() - else: log.info( f'Loading EXISTING `{mod.name}` symbology cache:\n' @@ -392,11 +408,20 @@ async def open_symcache( data: dict[str, dict] = tomllib.load(existing_fp) log.runtime(f'SYMCACHE TOML LOAD TIME: {time.time() - now}') - cache = SymbologyCache.from_dict( - data, - mod=mod, - fp=cachefile, - ) + # if there's an empty file for some reason we need + # to do a full reload as well! + if not data: + cache = await SymbologyCache.from_scratch( + mod=mod, + fp=cachefile, + ) + else: + cache = SymbologyCache.from_dict( + data, + mod=mod, + fp=cachefile, + ) + # TODO: use a real profiling sys.. # https://github.com/pikers/piker/issues/337