refactor kindlegen check

This commit is contained in:
Alex Xu
2026-09-15 18:25:32 -07:00
parent a310d4c2b1
commit aa652ff6c0
3 changed files with 37 additions and 40 deletions
+1
View File
@@ -69,6 +69,7 @@ def modify_path():
os.path.expandvars('%LOCALAPPDATA%\\Amazon\\KC2'), os.path.expandvars('%LOCALAPPDATA%\\Amazon\\KC2'),
os.path.expandvars('%LOCALAPPDATA%\\Amazon\\Kindle Previewer 3\\lib\\fc\\bin\\'), os.path.expandvars('%LOCALAPPDATA%\\Amazon\\Kindle Previewer 3\\lib\\fc\\bin\\'),
os.path.expandvars('%UserProfile%\\Kindle Previewer 3\\lib\\fc\\bin\\'), os.path.expandvars('%UserProfile%\\Kindle Previewer 3\\lib\\fc\\bin\\'),
'C:\\Tools',
'C:\\Program Files\\WindowsApps\\KindlePreviewerApp_4.0.0.0_x64__ek06e0aw29nma\\KindlePreviewerApp\\resources\\KFXGen\\bin', 'C:\\Program Files\\WindowsApps\\KindlePreviewerApp_4.0.0.0_x64__ek06e0aw29nma\\KindlePreviewerApp\\resources\\KFXGen\\bin',
'C:\\Apps\\Kindle Previewer 3\\lib\\fc\\bin', 'C:\\Apps\\Kindle Previewer 3\\lib\\fc\\bin',
'D:\\Apps\\Kindle Previewer 3\\lib\\fc\\bin', 'D:\\Apps\\Kindle Previewer 3\\lib\\fc\\bin',
+7 -32
View File
@@ -32,7 +32,6 @@ import sys
from urllib.parse import unquote from urllib.parse import unquote
from time import sleep from time import sleep
from shutil import move, rmtree from shutil import move, rmtree
from subprocess import STDOUT, PIPE, CalledProcessError
import requests import requests
from xml.sax.saxutils import escape from xml.sax.saxutils import escape
@@ -45,9 +44,9 @@ from PIL.Image import Dither
from .KCC_spread_label import LabelSpreadsDialog from .KCC_spread_label import LabelSpreadsDialog
from .shared import HTMLStripper, sanitizeTrace, walkLevel, subprocess_run from .shared import HTMLStripper, sanitizeTrace, walkLevel
from .comicarchive import SEVENZIP, TAR, available_archive_tools from .comicarchive import SEVENZIP, TAR, available_archive_tools
from .comic2ebook import OS_SORT_KEY, flattenTree, getWorkFolder, removeNonImages, sanitizeTree from .comic2ebook import OS_SORT_KEY, flattenTree, getWorkFolder, removeNonImages, sanitizeTree, detectKindleGen
from . import __version__ from . import __version__
from . import comic2ebook from . import comic2ebook
from . import metadata from . import metadata
@@ -1255,8 +1254,8 @@ class KCCGUI(KCC_ui.Ui_mainWindow):
self.needClean = True self.needClean = True
return return
if 'MOBI' in GUI.formats[str(GUI.formatBox.currentText())]['format'] and not self.kindleGen: if 'MOBI' in GUI.formats[str(GUI.formatBox.currentText())]['format'] and not self.kindleGen:
self.detectKindleGen() if not detectKindleGen(GUI):
if not self.kindleGen: self.progress.stop()
GUI.jobList.clear() GUI.jobList.clear()
self.display_kindlegen_missing() self.display_kindlegen_missing()
self.needClean = True self.needClean = True
@@ -1384,32 +1383,6 @@ class KCCGUI(KCC_ui.Ui_mainWindow):
self.saveSettings(None) self.saveSettings(None)
sys.exit(0) sys.exit(0)
def detectKindleGen(self, startup=False):
if not sys.platform.startswith('win'):
try:
os.chmod('/usr/local/bin/kindlegen', 0o755)
except Exception:
pass
try:
versionCheck = subprocess_run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, encoding='UTF-8', errors='ignore', check=True)
self.kindleGen = True
for line in versionCheck.stdout.splitlines():
if 'Amazon kindlegen' in line:
versionCheck = line.split('V')[1].split(' ')[0]
if Version(versionCheck) < Version('2.9'):
self.addMessage('Your <a href="https://www.amazon.com/b?node=23496309011">KindleGen</a>'
' is outdated! MOBI conversion might fail.', 'warning')
break
except (FileNotFoundError, CalledProcessError):
self.kindleGen = False
if startup:
self.display_kindlegen_missing()
except OSError as e:
self.kindleGen = False
if startup:
error = f"kindlegen: {e.strerror}\n\n Re-install or re-open Rosetta/Kindle Previewer/other Intel app?"
self.showDialog(error, 'error')
def __init__(self, kccapp, kccwindow): def __init__(self, kccapp, kccwindow):
global APP, MW, GUI global APP, MW, GUI
APP = kccapp APP = kccapp
@@ -1442,8 +1415,11 @@ class KCCGUI(KCC_ui.Ui_mainWindow):
except Exception: except Exception:
self.options = default_options self.options = default_options
self.worker = WorkerThread() self.worker = WorkerThread()
self.worker.setObjectName('worker')
self.versionCheck = VersionThread(self.startNumber2) self.versionCheck = VersionThread(self.startNumber2)
self.versionCheck.setObjectName('version')
self.progress = ProgressThread() self.progress = ProgressThread()
self.progress.setObjectName('progress')
self.tray = SystemTrayIcon() self.tray = SystemTrayIcon()
self.conversionAlive = False self.conversionAlive = False
self.needClean = True self.needClean = True
@@ -1674,7 +1650,6 @@ class KCCGUI(KCC_ui.Ui_mainWindow):
if not any([self.tar, self.sevenzip]): if not any([self.tar, self.sevenzip]):
self.addMessage('<a href="https://github.com/ciromattia/kcc#7-zip">Install 7z (link)</a>' self.addMessage('<a href="https://github.com/ciromattia/kcc#7-zip">Install 7z (link)</a>'
' to enable CBZ/CBR/ZIP/etc processing.', 'warning') ' to enable CBZ/CBR/ZIP/etc processing.', 'warning')
self.detectKindleGen(True)
APP.messageFromOtherInstance.connect(self.handleMessage) APP.messageFromOtherInstance.connect(self.handleMessage)
GUI.defaultOutputFolderButton.clicked.connect(self.selectDefaultOutputFolder) GUI.defaultOutputFolderButton.clicked.connect(self.selectDefaultOutputFolder)
+29 -8
View File
@@ -40,6 +40,7 @@ from shutil import move, copytree, rmtree
from multiprocessing import Pool, cpu_count from multiprocessing import Pool, cpu_count
from uuid import uuid4 from uuid import uuid4
from natsort import os_sort_keygen, os_sorted from natsort import os_sort_keygen, os_sorted
from packaging.version import Version
from slugify import slugify as slugify_ext from slugify import slugify as slugify_ext
from PIL import Image, ImageFile, ImageOps from PIL import Image, ImageFile, ImageOps
from pathlib import Path from pathlib import Path
@@ -1749,16 +1750,36 @@ def checkTools(source):
print('ERROR: 7z is missing!') print('ERROR: 7z is missing!')
sys.exit(1) sys.exit(1)
if options.format == 'MOBI': if options.format == 'MOBI':
try: if not detectKindleGen():
subprocess_run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, check=True)
except (FileNotFoundError, CalledProcessError):
print('ERROR: KindleGen is missing!')
sys.exit(1)
except OSError as e:
print(f"kindlegen: {e.strerror}")
print('Re-install or re-open Rosetta/Kindle Previewer/other Intel app?')
sys.exit(1) sys.exit(1)
def detectKindleGen(GUI=None):
if not sys.platform.startswith('win'):
try:
os.chmod('/usr/local/bin/kindlegen', 0o755)
except Exception:
pass
try:
versionCheck = subprocess_run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, encoding='UTF-8', errors='ignore', check=True)
for line in versionCheck.stdout.splitlines():
if 'Amazon kindlegen' in line:
versionCheck = line.split('V')[1].split(' ')[0]
if Version(versionCheck) < Version('2.9'):
if GUI:
GUI.addMessage('Your <a href="https://www.amazon.com/b?node=23496309011">KindleGen</a>'
' is outdated! MOBI conversion might fail.', 'warning')
break
return True
except (FileNotFoundError, CalledProcessError):
print('ERROR: KindleGen is missing!')
return False
except OSError as e:
print(f"kindlegen: {e.strerror}")
print('Re-install or re-open Rosetta/Kindle Previewer/other Intel app?')
if GUI:
error = f"kindlegen: {e.strerror}\n\n Re-install or re-open Rosetta/Kindle Previewer/other Intel app?"
GUI.showDialog(error, 'error')
return False
def checkPre(source='KCC-'): def checkPre(source='KCC-'):
# Make sure that all temporary files are gone # Make sure that all temporary files are gone