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
def sqlmanager(tmp_path, downloader, sqlstorage, mocker): sqlmanager = DataManager(Cache(downloader, sqlstorage, tmp_path)) sqlmanager._tempdir = str(tmp_path) m = mock.Mock() m.headers = {'Content-Disposition': 'test_file'} mocker.patch('sunpy.data.data_manager.cache.urlopen', return_value=m) yield sqlmanager
def module_patched_manager(tmp_path, downloader, storage, mocker): def _get_module(func): return 'fake_module.' module_patched_manager = DataManager(Cache(downloader, storage, tmp_path)) module_patched_manager._tempdir = str(tmp_path) module_patched_manager._get_module = _get_module m = mock.Mock() m.headers = {'Content-Disposition': 'test_file'} mocker.patch('sunpy.data.data_manager.cache.urlopen', return_value=m) yield module_patched_manager
def cache(tmp_path, downloader, storage, mocker): m = mock.Mock() m.headers = {'Content-Disposition': 'test_file'} mocker.patch('sunpy.data.data_manager.cache.urlopen', return_value=m) cache = Cache(downloader, storage, tmp_path) yield cache
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"]