Esempio n. 1
0
    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)
Esempio n. 2
0
    def serialize_to_disk(self, pkg: SoftwarePackage, icon_bytes: bytes,
                          only_icon: bool):
        """
        Sames as above, but does not check if disk cache is enabled or supported by the package instance
        :param pkg:
        :param icon_bytes:
        :param only_icon:
        :return:
        """
        if not only_icon:
            Path(pkg.get_disk_cache_path()).mkdir(parents=True, exist_ok=True)
            data = pkg.get_data_to_cache()

            if data:
                disk_path = pkg.get_disk_data_path()
                ext = disk_path.split('.')[-1]

                if ext == 'json':
                    with open(disk_path, 'w+') as f:
                        f.write(json.dumps(data))
                elif ext in ('yml', 'yaml'):
                    with open(disk_path, 'w+') as f:
                        f.write(yaml.dump(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)
Esempio n. 3
0
 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())