def add(self, section: Section): if isinstance(section, Section): section.width = self.width section.padding = self.padding if section.padding is None else section.padding for item in section.items: item.padding = section.padding if item.padding is None else item.padding item.align = section.align if item.align is None else item.align section.set_item_widths() self._sections.append(section) else: raise TypeError(f"Expected type 'Section', got '{type(section).__name__}' instead.")
def _cli_packages_list(self, filter_text: str = None, sort_by: str = None): table = Table() section = Section() section.add( Header( "List of all packages in iGameLister WHDLoad Package data file." )) def get(col: str) -> str: for package in self.data: if col == "id": yield package.id if col == "name": yield package.name section.add(Column(get("id"), width=5, label="ID", align=Align.Right)) section.add(Column(get("name"), label="Package name")) table.add(section) table.draw()
def _package_info(info, table: Table): info_source = None if type(info) == HallOfLight: info_source = Source.HoL elif type(info) == LemonAmiga: info_source = Source.Lemon elif type(info) == ADA: info_source = Source.ADA elif type(info) == BitWorld: info_source = Source.BitWorld elif type(info) == Pouet: info_source = Source.Pouet labels = [] details = [] def add(label, value): labels.append(label) details.append(value) def get_details(): add("Name", info.igame_name) add("Genre", info.igame_genre) get_details() section = Section() chipsets = [x.name for x in info.chipsets] if len(chipsets) > 1: chipset_text = "chipsets" else: chipset_text = "chipset" section.add( Header( f"iGame information from {info_source.value} for {chipset_text}: {', '.join(chipsets)}." )) def column_text(source): for i in range(len(labels)): if source == "label": if type(details[i]) is str: for j in range(len(details[i].split("\n"))): if j > 0: yield "" else: yield labels[i] else: yield labels[i] elif source == "details": if type(details[i]) is str: for line in details[i].split("\n"): yield line else: yield details[i] section.add(Column(column_text("label"), width=14, align=Align.Right)) section.add(Column(column_text("details"))) table.add(section)
def _package_slave_details(slave: Slave, table: Table): labels = [] details = [] def add(label, value): labels.append(label) details.append(value) def get_details(): add("File date", slave.file_datetime.strftime("%Y-%m-%d")) if slave.file_size < 1000: value = f"{slave.file_size} Bytes" else: try: value = f"{slave.file_size / 1024} KiB" except ZeroDivisionError: value = f"{slave.file_size} Bytes" add("File size", value) add("Version", slave.version) add("Flags", ', '.join([str(x) for x in slave.flags])) try: value = f"{slave.base_mem_size / 1024} KiB" except ZeroDivisionError: value = f"{slave.base_mem_size} Bytes" add("Chip mem", value) if slave.version < 8: return try: value = f"{slave.exp_mem / 1024} KiB" except ZeroDivisionError: value = f"{slave.exp_mem} Bytes" add("Exp. mem", value) if slave.version < 10: return add("Name", slave.name) add("Copyright", slave.copy) add("Info", slave.info) if slave.version < 16: return add("Kickstarts", ", ".join([str(x) for x in slave.kickstarts])) try: value = f"{slave.kickstart_size / 1024} KiB" except ZeroDivisionError: value = f"{slave.kickstart_size} Bytes" add("Kick. size", value) if slave.version < 17: return add("Slave Config", slave.configuration) get_details() section = Section() section.add(Header(f"Details for Slave '{slave.file_name}'.")) def column_text(source): for i in range(len(labels)): if source == "label": if type(details[i]) is str: for j in range(len(details[i].split("\n"))): if j > 0: yield "" else: yield labels[i] else: yield labels[i] elif source == "details": if type(details[i]) is str: for line in details[i].split("\n"): yield line else: yield details[i] section.add(Column(column_text("label"), width=14, align=Align.Right)) section.add(Column(column_text("details"))) table.add(section)
def _cli_packages_details(self, package_id: int = None): labels = [] details = [] def add(label, value): labels.append(label) details.append(value) matches = [x for x in self.data if x.id == package_id] if len(matches) == 1: package = matches[0] elif len(matches) > 1: raise ValueError(f"Multiple matches found for ID {package_id}.") else: package = None def get_details(): add("ID", package.id) add("Category", package.category.name) add("Release date", package.release_date.strftime("%Y-%m-%d")) try: value = f"{package.size_bytes / 1024:.2f} KiB" except ZeroDivisionError: value = f"{package.size_bytes} Bytes" add("Package size", value) if package is not None: get_details() table = Table() section = Section() section.add( Header( f"Details for Package '{package.name}' (ID {package.id}).", align=Align.Center)) def column_text(source): for i in range(len(labels)): if source == "label": if type(details[i]) is str: for j in range(len(details[i].split("\n"))): if j > 0: yield "" else: yield labels[i] else: yield labels[i] elif source == "details": if type(details[i]) is str: for line in details[i].split("\n"): yield line else: yield details[i] section.add( Column(column_text("label"), width=14, align=Align.Right)) section.add(Column(column_text("details"))) table.add(section) for slave in package.slaves: self._package_slave_details(slave, table) for info in package.info: self._package_info(info, table) table.draw() else: click.echo( "No matching WHDLoad Slave found in iGameLister WHDLoad Slave data file." )
def _cli_slaves_list(self, filter_text: str = None, sort_by: str = None): table = Table() title_str = "List of all WHDLoad Slaves in iGameLister WHDLoad Slave data file." if filter_text is not None: title_str += f" containing '{filter_text}'" title_str += ", sorted by " if sort_by is None or sort_by == "id": sort_attr = "id" title_str += "ID" elif sort_by == "file": sort_attr = "file_name" title_str += "file name" elif sort_by == "date": sort_attr = "file_datetime" title_str += "file date" elif sort_by == "dir": sort_attr = "installed_dir" title_str += "installed directory" elif sort_by == "name": sort_attr = "name" title_str += "name" title_str += "." section = Section() section.add(Header(title_str, align=Align.Center)) def column_text(source): sort_key = attrgetter(sort_attr) if sort_attr == "id" or sort_attr == "file_datetime": sorted_data = sorted(self.data, key=lambda slv: sort_key(slv)) else: sorted_data = sorted(self.data, key=lambda slv: sort_key(slv).lower()) for slave in sorted_data: if filter_text is not None: if filter_text.lower() not in slave.installed_path.lower(): continue def slave_name(): if slave.name is not None: return slave.name.split("\n") else: return [""] if source == "id": for i in range(len(slave_name())): if i > 0: yield "" else: yield slave.id elif source == "file_name": for i in range(len(slave_name())): if i > 0: yield "" else: yield slave.file_name elif source == "installed_dir": for i in range(len(slave_name())): if i > 0: yield "" else: yield slave.installed_dir elif source == "name": for name in slave_name(): yield name elif source == "file_datetime": for i in range(len(slave_name())): if i > 0: yield "" else: yield slave.file_datetime.strftime("%Y-%m-%d") else: raise ValueError(f"Unhandled source '{source}'.") if shutil.get_terminal_size().columns >= 120: section.add( Column(column_text("id"), width=5, align=Align.Right, label="ID")) section.add( Column(column_text("file_datetime"), width=10, label="File date")) section.add( Column(column_text("installed_dir"), label="Installed directory")) section.add( Column(column_text("file_name"), width=40.0, label="File name")) else: section.add( Column(column_text("id"), width=5, align=Align.Right, label="ID")) section.add( Column(column_text("file_datetime"), width=10, label="File date")) section.add( Column(column_text("file_name"), width=30, label="File name")) section.add(Column(column_text("name"), label="Slave name")) table.add(section) table.draw()
def _cli_slaves_details(self, slave_id: int = None): labels = [] details = [] def add(label, value): labels.append(label) details.append(value) matches = [x for x in self.data if x.id == slave_id] if len(matches) == 1: slave = matches[0] elif len(matches) > 1: raise ValueError(f"Multiple matches found for ID {slave_id}.") else: slave = None def get_details(): add("ID", slave.id) add("Install path", slave.installed_path) add("File name", slave.file_name) add("File date", slave.file_datetime.strftime("%Y-%m-%d")) add("File size_bytes", slave.file_size) add("Version", slave.version) add("Flags", ', '.join([str(x) for x in slave.flags])) try: value = f"{slave.base_mem_size / 1024} KiB" except ZeroDivisionError: value = f"{slave.base_mem_size} Bytes" add("Chip mem", value) if slave.version < 8: return try: value = f"{slave.exp_mem / 1024} KiB" except ZeroDivisionError: value = f"{slave.exp_mem} Bytes" add("Exp. mem", value) if slave.version < 10: return add("Name", slave.name) add("Copyright", slave.copy) add("Info", slave.info) if slave.version < 16: return add("Kickstarts", ", ".join([str(x) for x in slave.kickstarts])) try: value = f"{slave.kickstart_size / 1024} KiB" except ZeroDivisionError: value = f"{slave.kickstart_size} Bytes" add("Kick. size_bytes", value) if slave.version < 17: return add("Slave Config", slave.configuration) if slave is not None: get_details() table = Table() section = Section() section.add( Header( f"Showing details for Slave '{slave.file_name}' (ID {slave.id}).", align=Align.Center)) def column_text(source): for i in range(len(labels)): if source == "label": if type(details[i]) is str: for j in range(len(details[i].split("\n"))): if j > 0: yield "" else: yield labels[i] else: yield labels[i] elif source == "details": if type(details[i]) is str: for line in details[i].split("\n"): yield line else: yield details[i] section.add( Column(column_text("label"), width=14, align=Align.Right)) section.add(Column(column_text("details"))) table.add(section) table.draw() else: click.echo( "No matching WHDLoad Slave found in WHDLoad Slave data file.")