Skip to content

Commit

Permalink
Tools: Remove unused disk_cached 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 730399b
Showing 1 changed file with 3 additions and 50 deletions.
53 changes: 3 additions & 50 deletions loki/tools/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,24 @@

import atexit
import fnmatch
from functools import wraps
from hashlib import md5
from importlib import import_module, reload, invalidate_caches
import os
from pathlib import Path
import pickle
import re
import shutil
import sys
import tempfile

from loki.logging import debug, info
from loki.logging import debug
from loki.tools.util import as_tuple, flatten
from loki.config import config


__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 +180,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 730399b

Please sign in to comment.