def read(files): # Clean temp directory clean() for file in files: try: _extract(file) except Exception as e: fatal("Can't open archive file!", e=e) try: # Find extracted folder from os import chdir, listdir chdir(temp) # Get list of files to include (and those ignored) files = [] skipped = [] chapters = listdir() chapters.sort(key=_natural_keys) for chapter in chapters: pages = listdir(chapter) pages.sort(key=_natural_keys) for page in pages: if '.jpg' in page or '.png' in page: files.append(chapter + "/" + page) else: skipped.append(chapter + "/" + page) return files, skipped except Exception as e: fatal("Can't read contents of archive file!", e=e)
def clean(): from shutil import rmtree label("Cleaning temp directory...") try: rmtree(temp) except Exception as e: if type(e) != FileNotFoundError: fatal("Can't access temp directory!", e=e) pass
def _extract(file): # Extract zip file (if fails prompt for password) label("Extracting archive...") from zipfile import ZipFile archive = ZipFile(file) password = "" while True: try: output = temp + file[file.rfind('/') + 1:file.rfind('.')] archive.extractall(output, pwd=bytes(password, 'utf-8')) break except: password = ask("Password", "The file is encrypted and needs a password:"******"Password is needed to extract archive!") pass
def task(file): try: update.check() except Exception as e: print(e) pass label("Abriendo archivo...") import os if len(file) < 2: fatal("No se encuentra el archivo") file = file[1] if not file.endswith('.basa'): fatal("No se encuentra el archivo") try: directory = file[:file.rindex('.basa')] except Exception as e: fatal("No se puede abrir el archivo:", e) try: i = directory.find(".xlsx") j = directory.find(".docx") k = directory.rfind("\\") label("Generando PDF...") if i > -1: outFile = directory[k+1:i] + directory[i+5:] + ".pdf" outFile = os.path.join(temp, outFile) xlsx(file, outFile) elif j > -1: outFile = directory[k+1:j] + directory[j+5:] + ".pdf" outFile = os.path.join(temp, outFile) docx(file, outFile) else: fatal("No se encuentra el archivo") os.startfile(outFile, 'open') clean() os._exit(1) except Exception as e: fatal("No se puede abrir el archivo:", e)
def convert(): # Check if app is in another process from subprocess import check_output from shutil import copy2 s = check_output('tasklist', shell=True) if s.count(b"pymanga") > 1: fatal("Pymanga is already running on another process!") # Prompt for archive archives = selectzip("Open archive(s) to use:") # Extract and read zip file files, skipped = zip.read(archives) if len(skipped): confirm("The following files will not be included:", str(skipped)) # Select output file from pathlib import Path from os.path import commonprefix outFile = path.basename(commonprefix(archives)) if "." in outFile: outFile = outFile.split(".")[0] if False: # confirm("Choose format", "Do you want to convert the .cbz to .pdf?") == "yes": label("Creating .pdf file...") command = [imagemagick] command.extend(files) # Add extension to output file outFile = path.join(temp, outFile + ".pdf") command.append(outFile) # Convert file using ImageMagick from subprocess import Popen, PIPE, STDOUT p = Popen(command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, encoding='UTF8') # Wait for process to finish from time import sleep while p.poll() is None: sleep(1) response = p.stdout.readline() if response != "": fatal("Can't convert to pdf!", response) else: label("Creating .cbz file...") # Add extension to output file outFile = path.join(temp, outFile + ".cbz") # Copy all images in order to root of temp folder order = 0 pages = [] fill = len(str(len(files))) + 1 for file in files: order += 1 page = str(order).zfill(fill) + file[file.rfind('.'):] copy2(file, page) pages.append(page) # Create .cbz file zip.create(pages, outFile) # Save output file to Desktop or open with default editor outFile2 = path.join(Path.home(), "Desktop", path.basename(outFile)) try: copy2(outFile, outFile2) except Exception as e: fatal("Error copying file!", e=e) success("File saved to the desktop successfully!") from os import _exit _exit(1)
import os import tempfile from src.ui import fatal, label tempHalfLife = 24 * 60 * 60 try: temp = os.path.join(tempfile.gettempdir(), "basa3pdf") if not os.path.exists(temp): os.makedirs(temp) except Exception as e: fatal("No se puede abrir el directorio temporal:", e=e) def clean(): label("Limpiando archivos viejos...") import time now = time.time() for f in os.listdir(temp): f = os.path.join(temp, f) if os.stat(f).st_mtime < now - tempHalfLife: try: os.remove(f) except: pass