Beispiel #1
0
    def _fill_cached_data(self, pkg: SoftwarePackage) -> bool:
        if os.path.exists(pkg.get_disk_data_path()):
            disk_path = pkg.get_disk_data_path()
            ext = disk_path.split('.')[-1]

            with open(disk_path) as f:
                if ext == 'json':
                    cached_data = json.loads(f.read())
                elif ext in {'yml', 'yaml'}:
                    cached_data = yaml.load(f.read())
                else:
                    raise Exception(
                        'The cached data file {} has an unsupported format'.
                        format(disk_path))

            if cached_data:
                pkg.fill_cached_data(cached_data)
                cache = self.cache_map.get(pkg.__class__)

                if cache:
                    cache.add_non_existing(str(pkg.id), cached_data)

                return True

        return False
Beispiel #2
0
    def _fill_cached_data(self, pkg: SoftwarePackage) -> bool:
        if self.enabled:
            if os.path.exists(pkg.get_disk_data_path()):
                with open(pkg.get_disk_data_path()) as f:
                    cached_data = json.loads(f.read())
                    if cached_data:
                        pkg.fill_cached_data(cached_data)
                        cache = self.cache_map.get(pkg.__class__)\

                        if cache:
                            cache.add_non_existing(pkg.id, cached_data)

                        return True
        return False
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
0
    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}")