def sunpy_cache(mocker, tmp_path): """ Provide a way to add local files to the cache. This can be useful when mocking remote requests. """ from types import MethodType from sunpy.data.data_manager.cache import Cache from sunpy.data.data_manager.downloader import ParfiveDownloader from sunpy.data.data_manager.storage import InMemStorage cache = Cache(ParfiveDownloader(), InMemStorage(), tmp_path, None) def add(self, url, path): self._storage.store({ 'url': url, 'file_path': path, 'file_hash': 'none', # hash doesn't matter }) cache.add = MethodType(add, cache) def func(mocked): mocker.patch(mocked, cache) return cache yield func
import astropy.units as u import sunpy from sunpy import config from sunpy.data._sample import download_sample_data from sunpy.data.data_manager.cache import Cache from sunpy.data.data_manager.downloader import ParfiveDownloader from sunpy.data.data_manager.manager import DataManager from sunpy.data.data_manager.storage import SqliteStorage from sunpy.util.config import CACHE_DIR _download_dir = config.get('downloads', 'remote_data_manager_dir') manager = DataManager( Cache(ParfiveDownloader(), SqliteStorage(_download_dir + '/data_manager.db'), _download_dir)) cache = Cache(ParfiveDownloader(), SqliteStorage(CACHE_DIR + '/cache.db'), CACHE_DIR, expiry=int(config.get('downloads', 'cache_expiry')) * u.day) __all__ = ["download_sample_data", "manager", "cache"]