From c3d08bcb4c269fdd39b7a83b6793952fdfae427e Mon Sep 17 00:00:00 2001 From: Sakamoto Date: Mon, 3 Jun 2024 17:08:36 -0500 Subject: [PATCH] fixed formatting --- comic2pdf.py | 108 +++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 51 deletions(-) diff --git a/comic2pdf.py b/comic2pdf.py index 488ec98..dc7ba2a 100755 --- a/comic2pdf.py +++ b/comic2pdf.py @@ -3,22 +3,28 @@ # version 1.0 -import os, re, zipfile, argparse, tqdm, img2pdf, pathlib, tempfile, patoolib +import os, re, argparse, tqdm, img2pdf, pathlib, tempfile, patoolib from tqdm import tqdm from send2trash import send2trash -parser = argparse.ArgumentParser(description='Converts comic book zipfiles to PDF.', - formatter_class=argparse.ArgumentDefaultsHelpFormatter) -parser.add_argument('-src', - help='location of file(s) to convert') -parser.add_argument('-dest', - help='destination of converted files') -parser.add_argument('-t','--trash-old', - action='store_true', - help='places converted cbz and cbr to the trash') -parser.add_argument('-d',"--delete-old", - action='store_true', - help='delete cbz and cbr that are converted') +parser = argparse.ArgumentParser( + description="Converts comic book zipfiles to PDF.", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, +) +parser.add_argument("-src", help="location of file(s) to convert") +parser.add_argument("-dest", help="destination of converted files") +parser.add_argument( + "-t", + "--trash-old", + action="store_true", + help="places converted cbz and cbr to the trash", +) +parser.add_argument( + "-d", + "--delete-old", + action="store_true", + help="delete cbz and cbr that are converted", +) args = parser.parse_args() @@ -27,20 +33,20 @@ dest = args.dest delete_old = args.delete_old trash_old = args.trash_old + class Comic2PDF: + def __init__(self, src, dest): + # huh? this needs to be fixed. I can't read it + # horrible use of OOP + src = self.check_src(src, dest) + dest = self.check_dest(src, dest) - def __init__(self,src,dest): - - src = self.check_src(src,dest) - dest = self.check_dest(src,dest) - - if src.endswith('.cbz') or src.endswith('.cbr'): - self.convert_single_file(src,dest) + if src.endswith(".cbz") or src.endswith(".cbr"): + self.convert_single_file(src, dest) else: - self.batch_convert(src,dest) + self.batch_convert(src, dest) - # check if source has no input or has input. - def check_src(self,src,dest): + def check_src(self, src, dest): if (src is None and dest is None) or (src is None and dest is not None): src = os.path.realpath(__file__) src = os.path.dirname(src) @@ -48,14 +54,13 @@ class Comic2PDF: else: return src - # check if destination has no input or has input. - def check_dest(self,src,dest): + def check_dest(self, src, dest): if dest is None and src is None: dest = os.path.realpath(__file__) dest = os.path.dirname(dest) return dest elif dest is None and src is not None: - if src.endswith('.cbz') or src.endswith('.cbr'): + if src.endswith(".cbz") or src.endswith(".cbr"): dest = pathlib.Path(src).parents[0] else: dest = pathlib.Path(src) @@ -63,67 +68,68 @@ class Comic2PDF: else: return dest - def scan_folder(self,src): + def scan_folder(self, src): filesIn_folder = [] for root, dirs, files in os.walk(src): for name in files: - if name.endswith((".png", ".jpg",".jpeg",".cbz",'.cbr')): - filesIn_folder.append(os.path.join(root,name)) - - # human sorting + if name.endswith((".png", ".jpg", ".jpeg", ".cbz", ".cbr")): + filesIn_folder.append(os.path.join(root, name)) + filesIn_folder.sort(key=natural_keys) return filesIn_folder - - def convert_to_pdf(self,src,dest,name): + + def convert_to_pdf(self, src, dest, name): imgs = [] imgs = self.scan_folder(src) pdf_path = f"{dest}/{name}.pdf" - with open(pdf_path,"wb") as f: + with open(pdf_path, "wb") as f: f.write(img2pdf.convert(imgs)) - def convert_single_file(self,src,dest): + def convert_single_file(self, src, dest): with tempfile.TemporaryDirectory() as temp_dict: - folder_parents = pathlib.Path(temp_dict) name = pathlib.Path(src).stem - patoolib.extract_archive(src,outdir=temp_dict) + patoolib.extract_archive(src, outdir=temp_dict) list_folderInside = next(os.walk(folder_parents))[1] try: - storage = f'{temp_dict}/{list_folderInside[0]}' + storage = f"{temp_dict}/{list_folderInside[0]}" except: - storage = f'{temp_dict}/' - - self.convert_to_pdf(src=storage,dest=dest,name=name) + storage = f"{temp_dict}/" + + self.convert_to_pdf(src=storage, dest=dest, name=name) if trash_old is True: - send2trash(f'{src}') + send2trash(f"{src}") if delete_old is True: - os.remove(f'{src}') + os.remove(f"{src}") - def batch_convert(self,src,dest): + def batch_convert(self, src, dest): for root, dir, files in os.walk(src): - pstat = [f for f in files if f.endswith('.cbr') or f.endswith('.cbz')] - pbar = tqdm(total = len(pstat)) + pstat = [f for f in files if f.endswith(".cbr") or f.endswith(".cbz")] + pbar = tqdm(total=len(pstat)) queue = 0 for name in files: - if name.endswith('.cbz') or name.endswith('.cbr'): + if name.endswith(".cbz") or name.endswith(".cbr"): pbar.set_description(f"Converting {name}") - self.convert_single_file(os.path.join(root,name),dest) + self.convert_single_file(os.path.join(root, name), dest) pbar.update(1) queue += 1 else: pass break -# organizes files with humanly order. +# exported from the internet; +# sorts folder name to human order def atoi(text): - return int(text) if text.isdigit() else text + return int(text) if text.isdigit() else text + def natural_keys(text): return [atoi(c) for c in re.split(r"(d )", text)] -Comic2PDF(src,dest) + +Comic2PDF(src, dest)