Skip to content

Commit

Permalink
Tools: Remove unused disk_cahced utility (formerly used for OFP)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange05 committed Jan 10, 2025
1 parent b7dbf7c commit c33cac5
Showing 1 changed file with 2 additions and 47 deletions.
49 changes: 2 additions & 47 deletions loki/tools/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@

__all__ = [
'LokiTempdir', 'gettempdir', 'filehash', 'delete', 'find_paths',
'find_files', 'disk_cached', 'load_module',
'write_env_launch_script', 'local_loki_setup',
'local_loki_cleanup'
'find_files', 'load_module', 'write_env_launch_script',
'local_loki_setup', 'local_loki_cleanup'
]


Expand Down Expand Up @@ -183,50 +182,6 @@ def find_files(pattern, srcdir='.'):
for fname in fnames if rule.match(fname)]


def disk_cached(argname, suffix='cache'):
"""
A function that creates a decorator which will cache the result of a function
:param argname: Name of the argument that holds the filename
"""
def decorator(fn):

@wraps(fn)
def cached(*args, **kwargs):
"""
Wrapper that will cache the output of a function on disk.
The first argument is assumed to be the name of the file
that needs to be cached, and the cache will be put next
to that file with the suffix ``.cache``.
"""
filename = kwargs[argname]
cachefile = f'{filename}.{suffix}'

# Read cached file from disc if it's been cached before
if config['disk-cache'] and os.path.exists(cachefile):
# Only use cache if it is newer than the file
filetime = os.path.getmtime(filename)
cachetime = os.path.getmtime(cachefile)
if cachetime >= filetime:
with open(cachefile, 'rb') as cachehandle:
info(f'Loading cache: "{cachefile}"')
return pickle.load(cachehandle)

# Execute the function with all arguments passed
res = fn(*args, **kwargs)

# Write to cache file
if config['disk-cache']:
with open(cachefile, 'wb') as cachehandle:
info(f'Saving cache: "{cachefile}"')
pickle.dump(res, cachehandle)

return res
return cached
return decorator


def load_module(module, path=None):
"""
Handle import paths and load the compiled module
Expand Down

0 comments on commit c33cac5

Please sign in to comment.