def _req_as_option(self, req: UpgradeRequirement, tooltip: bool = True, custom_tooltip: str = None, required_size: bool = True, display_sizes: bool = True, positive_size_symbol: bool = False) -> InputOption: if req.pkg.installed: icon_path = req.pkg.get_disk_icon_path() if not icon_path: icon_path = req.pkg.get_type_icon_path() elif not os.path.isfile(icon_path) or QIcon.fromTheme(icon_path).isNull(): icon_path = req.pkg.get_type_icon_path() else: icon_path = req.pkg.get_type_icon_path() size_str = None if display_sizes: storage_size = '?' if req.extra_size is None else get_human_size_str(req.extra_size, positive_size_symbol) size_str = f"{self.i18n['action.update.storage_size']}: {storage_size}" if required_size and req.extra_size != req.required_size: download_size = '?' if req.required_size is None else get_human_size_str(req.required_size) size_str += f" ({self.i18n['action.update.download_size']}: {download_size})" label = f"{req.pkg.name} {f' ({req.pkg.latest_version})' if req.pkg.latest_version else ''}" if size_str: label += f' | {size_str}' return InputOption(label=label, value=None, tooltip=custom_tooltip if custom_tooltip else (req.pkg.get_name_tooltip() if tooltip else None), read_only=True, icon_path=icon_path)
def _map_to_install( pkgs: Optional[Collection[DebianPackage]] ) -> Optional[Tuple[List[InputOption], str, str]]: if pkgs: download_size, install_size = 0, 0 views = [] for p in pkgs: if p.compressed_size is not None and p.compressed_size >= 0: compressed = get_human_size_str(p.compressed_size) download_size += p.compressed_size else: compressed = '?' if p.transaction_size is not None: install_size += p.transaction_size uncompressed = get_human_size_str(p.transaction_size) else: uncompressed = '?' views.append( InputOption( label=f"{p.name} ({uncompressed} | {compressed})", value=p.name, read_only=True, icon_path=DEBIAN_ICON_PATH, tooltip=p.description if p.description else '?')) dsize = get_human_size_str( download_size) if download_size > 0 else '?' isize = get_human_size_str( install_size) if install_size > 0 else '?' return views, isize, dsize
def get_info(self, pkg: SnapApplication) -> dict: info = { 'description': pkg.description, 'developer': pkg.developer, 'license': pkg.license, 'contact': pkg.contact, 'snap-id': pkg.id, 'name': pkg.name, 'publisher': pkg.publisher, 'revision': pkg.rev, 'tracking': pkg.tracking, 'channel': pkg.channel, 'type': pkg.type } if pkg.installed: commands = [ *{ c['name'] for c in SnapdClient(self.logger).list_commands(pkg.name) } ] commands.sort() info['commands'] = commands if pkg.installed_size: info['installed_size']: get_human_size_str(pkg.installed_size) elif pkg.download_size: info['download_size'] = get_human_size_str(pkg.download_size) return info
def get_content_length(self, url: str, session: bool = True) -> Optional[str]: size = self.get_content_length_in_bytes(url, session) if size: return get_human_size_str(size)
def get_info(self, pkg: DebianPackage) -> Optional[dict]: info = {'00.name': pkg.name, '01.version': pkg.version, "02.description": pkg.description} if pkg.installed and pkg.app: info['03.exec'] = pkg.app.exe_path extra_info = self.aptitude.show((f'{pkg.name}={pkg.version}',), verbose=True) if extra_info: extra_info = extra_info.get(pkg.name) if extra_info: ignored_fields = {'package', 'version', 'description'} for field, val in extra_info.items(): if field not in ignored_fields and not field.startswith('description'): final_val = get_human_size_str(val) if field in self.aptitude.size_attrs else val final_field = f'04.{field}' if final_field not in self._i18n: self._i18n.default[final_field] = field info[final_field] = final_val # for sorting return info
def _map_to_remove( self, pkgs: Optional[Collection[DebianPackage]] ) -> Optional[Tuple[List[InputOption], str]]: if pkgs: freed_space = 0 views = [] for p in pkgs: if p.transaction_size is not None: size = p.transaction_size * (-1 if p.transaction_size < 0 else 1) freed_space += size uncompressed = get_human_size_str(size) else: uncompressed = '?' views.append( InputOption( label=f"{p.name} (-{uncompressed})", value=p.name, read_only=True, icon_path=DEBIAN_ICON_PATH, tooltip=p.description if p.description else '?')) return views, f'-{get_human_size_str(freed_space)}' if freed_space > 0 else '?'
def _gen_to_remove_form(self, reqs: List[UpgradeRequirement]) -> FormComponent: opts = [self._req_as_option(r, False, r.reason, required_size=False) for r in reqs] comps = [MultipleSelectComponent(label='', options=opts, default_options=set(opts))] required_size, extra_size = self._sum_pkgs_size(reqs) lb = '{} ({}: {} | {}: {})'.format(self.i18n['action.update.label_to_remove'].capitalize(), self.i18n['amount'].capitalize(), len(opts), self.i18n['action.update.storage_size'], '?' if extra_size is None else get_human_size_str(-extra_size)) return FormComponent(label=lb, components=comps)
def confirm_removal(self, source_pkg: str, dependencies: Collection[DebianPackage], watcher: ProcessWatcher) -> bool: dep_views = [] freed_space = 0 for p in sorted(dependencies, key=attrgetter('name')): if p.transaction_size is not None: size = p.transaction_size * (-1 if p.transaction_size < 0 else 1) freed_space += size size_str = get_human_size_str(size) else: size_str = '?' dep_views.append( InputOption(label=f"{p.name}: -{size_str}", value=p.name, read_only=True, icon_path=DEBIAN_ICON_PATH, tooltip=p.description)) deps_container = MultipleSelectComponent(id_='deps', label='', options=dep_views, default_options={*dep_views}, max_width=537) freed_space_str = bold('-' + get_human_size_str(freed_space)) body_text = TextComponent(html=self._i18n['debian.remove_deps'].format( no=bold(str(len(dependencies))), pkg=bold(source_pkg), fspace=freed_space_str), min_width=653) return watcher.request_confirmation( title=self._i18n['debian.transaction.title'], components=[body_text, deps_container], confirmation_label=self._i18n['popup.button.continue'], deny_label=self._i18n['popup.button.cancel'], min_height=200, body=None)
def _gen_to_install_form(self, reqs: List[UpgradeRequirement]) -> Tuple[FormComponent, Tuple[int, int]]: opts = [self._req_as_option(r, custom_tooltip=r.reason, positive_size_symbol=True) for r in reqs] comps = [MultipleSelectComponent(label='', options=opts, default_options=set(opts))] required_size, extra_size = self._sum_pkgs_size(reqs) lb = '{} ({}: {} | {}: {} | {}: {})'.format(self.i18n['action.update.required_label'].capitalize(), self.i18n['amount'].capitalize(), len(opts), self.i18n['action.update.storage_size'], '?' if extra_size is None else get_human_size_str(extra_size, True), self.i18n['action.update.download_size'], '?' if required_size is None else get_human_size_str(required_size)) return FormComponent(label=lb, components=comps), (required_size, extra_size)
def test__must_not_concatenate_the_plus_sign_if_positive_sign_is_true_and_value_is_negative( self): self.assertEqual('-999 B', get_human_size_str(-999, positive_sign=True)) self.assertEqual('-1.00 kB', get_human_size_str(-1000, positive_sign=True))
def test__must_properly_convert_to_PB(self): self.assertEqual('1.00 PB', get_human_size_str(1000**5)) self.assertEqual('-1.00 PB', get_human_size_str(1000**5 * -1)) self.assertEqual('57.30 PB', get_human_size_str(1000**5 * 57.3))
def test__must_properly_convert_to_TB(self): self.assertEqual('1.00 TB', get_human_size_str(1000**4)) self.assertEqual('-1.00 TB', get_human_size_str(1000**4 * -1)) self.assertEqual('57.30 TB', get_human_size_str(1000**4 * 57.3))
def test__must_properly_convert_to_GB(self): self.assertEqual('1.00 GB', get_human_size_str(1000**3)) self.assertEqual('-1.00 GB', get_human_size_str(1000**3 * -1)) self.assertEqual('57.30 GB', get_human_size_str(1000**3 * 57.3))
def test__must_properly_convert_to_MB(self): self.assertEqual('1.00 MB', get_human_size_str(1000**2)) self.assertEqual('-1.00 MB', get_human_size_str(1000**2 * -1)) self.assertEqual('1.20 MB', get_human_size_str(1000**2 * 1.2)) self.assertEqual('1.50 MB', get_human_size_str(1000**2 * 1.5)) self.assertEqual('57.30 MB', get_human_size_str(1000**2 * 57.3))
def test__must_properly_convert_to_kB(self): self.assertEqual('1.00 kB', get_human_size_str(1000)) self.assertEqual('-1.00 kB', get_human_size_str(-1000)) self.assertEqual('1.50 kB', get_human_size_str(1500)) self.assertEqual('1.75 kB', get_human_size_str(1750)) self.assertEqual('57.30 kB', get_human_size_str(57300))
def test__must_properly_display_B(self): self.assertEqual('1 B', get_human_size_str(1)) self.assertEqual('999 B', get_human_size_str(999)) self.assertEqual('-999 B', get_human_size_str(-999))