示例#1
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)
示例#2
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)