def cache_to_disk(self, pkg: SoftwarePackage, icon_bytes: bytes, only_icon: bool): """ Saves the package data to the hard disk. :param pkg: :param icon_bytes: :param only_icon: if only the icon should be saved :return: """ if self.context.disk_cache and pkg.supports_disk_cache(): if not only_icon: Path(pkg.get_disk_cache_path()).mkdir(parents=True, exist_ok=True) data = pkg.get_data_to_cache() if data: with open(pkg.get_disk_data_path(), 'w+') as f: f.write(json.dumps(data)) if icon_bytes: Path(pkg.get_disk_cache_path()).mkdir(parents=True, exist_ok=True) with open(pkg.get_disk_icon_path(), 'wb+') as f: f.write(icon_bytes)
def read(self, pkg: SoftwarePackage) -> Optional[Dict[str, Any]]: if pkg and pkg.supports_disk_cache(): data_path = pkg.get_disk_data_path() if data_path and os.path.isfile(data_path): ext = data_path.split('.')[-1] try: with open(data_path) as f: file_content = f.read() except FileNotFoundError: return if file_content: if ext == 'json': cached_data = json.loads(file_content) elif ext in {'yml', 'yaml'}: cached_data = yaml.load(file_content) else: raise Exception( f'The cached data file {data_path} has an unsupported format' ) if cached_data: return cached_data else: self.logger.warning( f"No cached content in file {data_path}")
def fill(self, pkg: SoftwarePackage): """ Adds a package which data must be read from the disk to a queue. :param pkg: :return: """ if pkg and pkg.supports_disk_cache(): self.pkgs.append(pkg)
def clean_cache_for(self, pkg: SoftwarePackage): """ Cleans cached package cached data. This default implementation only cleans the cached data from the heard disk :param pkg: :return: """ if pkg.supports_disk_cache() and os.path.exists(pkg.get_disk_cache_path()): shutil.rmtree(pkg.get_disk_cache_path())
def cache_to_disk(self, pkg: SoftwarePackage, icon_bytes: bytes, only_icon: bool): if pkg.supports_disk_cache(): man = self._get_manager_for(pkg) if man: return man.cache_to_disk(pkg, icon_bytes=icon_bytes, only_icon=only_icon)
def cache_to_disk(self, pkg: SoftwarePackage, icon_bytes: Optional[bytes], only_icon: bool): """ Saves the package data to the hard disk. :param pkg: :param icon_bytes: :param only_icon: if only the icon should be saved :return: """ if pkg.supports_disk_cache(): self.serialize_to_disk(pkg, icon_bytes, only_icon)
def fill(self, pkg: SoftwarePackage, sync: bool = False): """ Adds a package which data must be read from the disk to a queue (if not sync) :param pkg: :param sync: :return: """ if pkg and pkg.supports_disk_cache(): if sync or not self._working: self._fill_cached_data(pkg) else: self.pkgs.append(pkg)