import json import os cache_path = 'build/meadow-cache.json' cache_data = None class Cache: def load(): global cache_data if not os.path.exists(cache_path): cache_data = {} return with open(cache_path, mode='r') as f: cache_data = json.load(f) def reset(): global cache_data cache_data = {} def save(): global cache_data with open(cache_path, mode='w') as f: json.dump(cache_data, f, indent=4) def get(path): parts = path.split('.') container = cache_data for p in parts[:-1]: if p not in container: return None if not isinstance(container[p], dict): print('Err: element "{}" in cache key "{}" is not a dictionary'.format(p, path)) return container = container[p] if parts[-1] in container: return container[parts[-1]] return None def set(path, value): parts = path.split('.') container = cache_data for p in parts[:-1]: if p not in container: container[p] = {} if not isinstance(container[p], dict): print('Err: element "{}" in cache key "{}" is not a dictionary'.format(p, path)) return container = container[p] container[parts[-1]] = value