"""A PyQT4 dialog to enter app settings""" # # Copyright 2012-2014 ComicTagger Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import annotations import html import logging import os import pathlib import platform import shutil import urllib.parse from typing import Any, cast import settngs from PyQt5 import QtCore, QtGui, QtWidgets, uic import comictaggerlib.ui.talkeruigenerator from comicapi import merge, utils from comicapi.archivers.archiver import Archiver from comicapi.genericmetadata import md_test from comictaggerlib import ctsettings from comictaggerlib.ctsettings import ct_ns from comictaggerlib.ctsettings.plugin import group_for_plugin from comictaggerlib.filerenamer import FileRenamer, Replacement, Replacements from comictaggerlib.ui import ui_path from comictalker.comictalker import ComicTalker logger = logging.getLogger(__name__) windowsRarHelp = """
To write to CBR/RAR archives,
you will need to have the tools from
WINRar
installed. (ComicTagger only uses the command-line rar tool.)
A restart is needed for a new path to take effect.
To write to CBR/RAR archives,
you will need to have the shareware rar tool from RARLab installed.
Your package manager should have rar (e.g. "apt-get install rar"). If not, download it
here,
and install in your path.
A restart is needed for a new path to take effect.
To write to CBR/RAR archives, you will need the rar tool. The easiest way to get this is to install homebrew.
Once homebrew is installed, run: brew install rar{html.escape(template_tooltip)}") self.rename_error: Exception | None = None self.sources = comictaggerlib.ui.talkeruigenerator.generate_source_option_tabs( self.tComicTalkers, self.config, self.talkers ) self.cbFilenameParser.clear() self.cbFilenameParser.addItems(utils.Parser) for mode in merge.Mode: self.cbTagsMergeMode.addItem(mode.name.capitalize().replace("_", " "), mode) self.cbDownloadMergeMode.addItem(mode.name.capitalize().replace("_", " "), mode) self.connect_signals() self.settings_to_form() self.rename_test() self.dir_test() self.leFilenameParserTest.setText(self.lblRenameTest.text()) self.filename_parser_test() self.update_rar_path() dirs = self.config.values.Runtime_Options__config self.lbl_config_dir.setText( f"Config Dir: {dirs.user_config_dir}" ) self.lbl_cache_dir.setText( f"Cache Dir: {dirs.user_cache_dir}" ) self.lbl_log_dir.setText( f"Log Dir: {dirs.user_log_dir}" ) self.lbl_plugin_dir.setText( f"Plugin Dir: {dirs.user_plugin_dir}" ) # Set General as start tab self.tabWidget.setCurrentIndex(0) def connect_signals(self) -> None: self.btnBrowseRar.clicked.connect(self.select_rar) self.btnClearCache.clicked.connect(self.clear_cache) self.btnResetSettings.clicked.connect(self.reset_settings) self.btnTemplateHelp.clicked.connect(self.show_template_help) self.cbxMoveFiles.clicked.connect(self.dir_test) self.cbxMoveOnly.clicked.connect(self.move_only_clicked) self.leDirectory.textEdited.connect(self.dir_test) self.cbFilenameParser.currentIndexChanged.connect(self.switch_parser) self.leRarExePath.textEdited.connect(self.update_rar_path) self.btnAddLiteralReplacement.clicked.connect(self.addLiteralReplacement) self.btnAddValueReplacement.clicked.connect(self.addValueReplacement) self.btnRemoveLiteralReplacement.clicked.connect(self.removeLiteralReplacement) self.btnRemoveValueReplacement.clicked.connect(self.removeValueReplacement) self.leRenameTemplate.textEdited.connect(self.rename_test) self.cbxMoveFiles.clicked.connect(self.rename_test) self.cbxMoveOnly.clicked.connect(self.rename_test) self.cbxRenameStrict.clicked.connect(self.rename_test) self.cbxSmartCleanup.clicked.connect(self.rename_test) self.cbxChangeExtension.clicked.connect(self.rename_test) self.leIssueNumPadding.textEdited.connect(self.rename_test) self.twLiteralReplacements.cellChanged.connect(self.rename_test) self.twValueReplacements.cellChanged.connect(self.rename_test) self.leFilenameParserTest.textEdited.connect(self.filename_parser_test) self.cbxRemoveC2C.clicked.connect(self.filename_parser_test) self.cbxRemoveFCBD.clicked.connect(self.filename_parser_test) self.cbxRemovePublisher.clicked.connect(self.filename_parser_test) self.cbxProtofoliusIssueNumberScheme.clicked.connect(self.filename_parser_test) self.cbxProtofoliusIssueNumberScheme.clicked.connect(self.protofolius_clicked) self.cbxAllowIssueStartWithLetter.clicked.connect(self.filename_parser_test) self.cbxSplitWords.clicked.connect(self.filename_parser_test) def disconnect_signals(self) -> None: self.btnAddLiteralReplacement.clicked.disconnect() self.btnAddValueReplacement.clicked.disconnect() self.btnBrowseRar.clicked.disconnect() self.btnClearCache.clicked.disconnect() self.btnRemoveLiteralReplacement.clicked.disconnect() self.btnRemoveValueReplacement.clicked.disconnect() self.btnResetSettings.clicked.disconnect() self.btnTemplateHelp.clicked.disconnect() self.cbxChangeExtension.clicked.disconnect() self.cbFilenameParser.currentIndexChanged.disconnect() self.cbxMoveFiles.clicked.disconnect() self.cbxMoveOnly.clicked.disconnect() self.cbxRenameStrict.clicked.disconnect() self.cbxSmartCleanup.clicked.disconnect() self.leDirectory.textEdited.disconnect() self.leIssueNumPadding.textEdited.disconnect() self.leRenameTemplate.textEdited.disconnect() self.twLiteralReplacements.cellChanged.disconnect() self.twValueReplacements.cellChanged.disconnect() self.leFilenameParserTest.textEdited.disconnect() self.cbxRemoveC2C.clicked.disconnect() self.cbxRemoveFCBD.clicked.disconnect() self.cbxRemovePublisher.clicked.disconnect() self.cbxProtofoliusIssueNumberScheme.clicked.disconnect() self.cbxAllowIssueStartWithLetter.clicked.disconnect() self.cbxSplitWords.clicked.disconnect() def protofolius_clicked(self, *args: Any, **kwargs: Any) -> None: if self.cbxProtofoliusIssueNumberScheme.isChecked(): self.cbxAllowIssueStartWithLetter.setEnabled(False) self.cbxAllowIssueStartWithLetter.setChecked(True) else: self.cbxAllowIssueStartWithLetter.setEnabled(True) self.filename_parser_test() def filename_parser_test(self, *args: Any, **kwargs: Any) -> None: self._filename_parser_test(self.leFilenameParserTest.text()) def _filename_parser_test(self, filename: str) -> None: self.cbFilenameParser: QtWidgets.QComboBox filename_info = utils.parse_filename( filename=filename, parser=utils.Parser(self.cbFilenameParser.currentText()), remove_c2c=self.cbxRemoveC2C.isChecked(), remove_fcbd=self.cbxRemoveFCBD.isChecked(), remove_publisher=self.cbxRemovePublisher.isChecked(), split_words=self.cbxSplitWords.isChecked(), allow_issue_start_with_letter=self.cbxAllowIssueStartWithLetter.isChecked(), protofolius_issue_number_scheme=self.cbxProtofoliusIssueNumberScheme.isChecked(), ) report = "" for item in ( "series", "issue", "issue_count", "title", "volume", "volume_count", "year", "alternate", "publisher", "format", "archive", "remainder", "annual", "c2c", "fcbd", ): report += f"{item.title().replace('_', ' ')}: {dict(filename_info)[item]}\n" self.lblFilenameParserTest.setText(report) def addLiteralReplacement(self) -> None: self.insertRow(self.twLiteralReplacements, self.twLiteralReplacements.rowCount(), Replacement("", "", False)) def addValueReplacement(self) -> None: self.insertRow(self.twValueReplacements, self.twValueReplacements.rowCount(), Replacement("", "", False)) def removeLiteralReplacement(self) -> None: if self.twLiteralReplacements.currentRow() >= 0: self.twLiteralReplacements.removeRow(self.twLiteralReplacements.currentRow()) def removeValueReplacement(self) -> None: if self.twValueReplacements.currentRow() >= 0: self.twValueReplacements.removeRow(self.twValueReplacements.currentRow()) def insertRow(self, table: QtWidgets.QTableWidget, row: int, replacement: Replacement) -> None: find, replace, strict_only = replacement table.insertRow(row) table.setItem(row, 0, QtWidgets.QTableWidgetItem(find)) table.setItem(row, 1, QtWidgets.QTableWidgetItem(replace)) tmp = QtWidgets.QTableWidgetItem() if strict_only: tmp.setCheckState(QtCore.Qt.CheckState.Checked) else: tmp.setCheckState(QtCore.Qt.CheckState.Unchecked) table.setItem(row, 2, tmp) def rename_test(self, *args: Any, **kwargs: Any) -> None: self._rename_test(self.leRenameTemplate.text()) def move_only_clicked(self, *args: Any, **kwargs: Any) -> None: if self.cbxMoveOnly.isChecked(): self.cbxMoveFiles.setEnabled(False) self.cbxMoveFiles.setChecked(True) else: self.cbxMoveFiles.setEnabled(True) self.dir_test() def dir_test(self) -> None: self.lblDir.setText( str(pathlib.Path(self.leDirectory.text().strip()).resolve()) if self.cbxMoveFiles.isChecked() or self.cbxMoveOnly.isChecked() else "" ) def _rename_test(self, template: str) -> None: if not str(self.leIssueNumPadding.text()).isdigit(): self.leIssueNumPadding.setText("0") fr = FileRenamer( None, platform="universal" if self.cbxRenameStrict.isChecked() else "auto", replacements=self.get_replacements(), ) fr.set_metadata(md_test, "cory doctorow #1.cbz") fr.move_only = self.cbxMoveOnly.isChecked() fr.move = self.cbxMoveFiles.isChecked() fr.set_template(template) fr.set_issue_zero_padding(int(self.leIssueNumPadding.text())) fr.set_smart_cleanup(self.cbxSmartCleanup.isChecked()) try: self.lblRenameTest.setText(fr.determine_name(".cbz")) self.rename_error = None except Exception as e: self.rename_error = e self.lblRenameTest.setText(str(e)) def update_rar_path(self, *args: Any, **kwargs: Any) -> None: rar_path: Any = pathlib.Path(self.leRarExePath.text()) found_rar = "RAR not found" if rar_path and rar_path.is_absolute() and rar_path.is_file(): found_rar = str(rar_path) elif utils.which("rar"): found_rar = str(utils.which("rar")) self.lblRarFound.setText(f"RAR path: {found_rar}") def switch_parser(self) -> None: currentParser = utils.Parser(self.cbFilenameParser.currentText()) complicated = currentParser == utils.Parser.COMPLICATED self.cbxRemoveC2C.setEnabled(complicated) self.cbxRemoveFCBD.setEnabled(complicated) self.cbxRemovePublisher.setEnabled(complicated) self.cbxProtofoliusIssueNumberScheme.setEnabled(complicated) self.cbxAllowIssueStartWithLetter.setEnabled(complicated) self.filename_parser_test() def settings_to_form(self) -> None: self.disconnect_signals() # Copy values from settings to form archive_group = group_for_plugin(Archiver) if archive_group in self.config[1] and "rar" in self.config[1][archive_group].v: self.leRarExePath.setText(getattr(self.config[0], self.config[1][archive_group].v["rar"].internal_name)) else: self.leRarExePath.setEnabled(False) self.sbNameMatchIdentifyThresh.setValue(self.config[0].Issue_Identifier__series_match_identify_thresh) self.sbNameMatchSearchThresh.setValue(self.config[0].Issue_Identifier__series_match_search_thresh) self.tePublisherFilter.setPlainText("\n".join(self.config[0].Auto_Tag__publisher_filter)) self.cbxCheckForNewVersion.setChecked(self.config[0].General__check_for_new_version) self.cbxPromptOnSave.setChecked(self.config[0].General__prompt_on_save) self.cbFilenameParser.setCurrentText(self.config[0].Filename_Parsing__filename_parser) self.cbxRemoveC2C.setChecked(self.config[0].Filename_Parsing__remove_c2c) self.cbxRemoveFCBD.setChecked(self.config[0].Filename_Parsing__remove_fcbd) self.cbxRemovePublisher.setChecked(self.config[0].Filename_Parsing__remove_publisher) self.cbxProtofoliusIssueNumberScheme.setChecked( self.config[0].Filename_Parsing__protofolius_issue_number_scheme ) self.cbxAllowIssueStartWithLetter.setChecked(self.config[0].Filename_Parsing__allow_issue_start_with_letter) self.switch_parser() self.cbxUseFilter.setChecked(self.config[0].Auto_Tag__use_publisher_filter) self.cbxSortByYear.setChecked(self.config[0].Issue_Identifier__sort_series_by_year) self.cbxExactMatches.setChecked(self.config[0].Issue_Identifier__exact_series_matches_first) self.cbxClearFormBeforePopulating.setChecked(self.config[0].Auto_Tag__clear_tags) self.cbxAssumeLoneCreditIsPrimary.setChecked(self.config[0].Metadata_Options__assume_lone_credit_is_primary) self.cbxCopyCharactersToTags.setChecked(self.config[0].Metadata_Options__copy_characters_to_tags) self.cbxCopyTeamsToTags.setChecked(self.config[0].Metadata_Options__copy_teams_to_tags) self.cbxCopyLocationsToTags.setChecked(self.config[0].Metadata_Options__copy_locations_to_tags) self.cbxCopyStoryArcsToTags.setChecked(self.config[0].Metadata_Options__copy_storyarcs_to_tags) self.cbxCopyNotesToComments.setChecked(self.config[0].Metadata_Options__copy_notes_to_comments) self.cbxCopyWebLinkToComments.setChecked(self.config[0].Metadata_Options__copy_weblink_to_comments) self.cbxApplyCBLTransformOnCVIMport.setChecked(self.config[0].Metadata_Options__apply_transform_on_import) self.cbxApplyCBLTransformOnBatchOperation.setChecked( self.config[0].Metadata_Options__apply_transform_on_bulk_operation ) self.cbxRemoveHtmlTables.setChecked(self.config[0].Metadata_Options__remove_html_tables) self.cbTagsMergeMode.setCurrentIndex(self.cbTagsMergeMode.findData(self.config[0].Metadata_Options__tag_merge)) self.cbDownloadMergeMode.setCurrentIndex( self.cbDownloadMergeMode.findData(self.config[0].Metadata_Options__metadata_merge) ) self.cbxTagsMergeLists.setChecked(self.config[0].Metadata_Options__tag_merge_lists) self.cbxMergeListsMetadata.setChecked(self.config[0].Metadata_Options__metadata_merge_lists) self.cbxShortTagNames.setChecked(self.config[0].Metadata_Options__use_short_tag_names) self.cbxEnableCR.setChecked(self.config[0].Metadata_Options__cr) self.leRenameTemplate.setText(self.config[0].File_Rename__template) self.leIssueNumPadding.setText(str(self.config[0].File_Rename__issue_number_padding)) self.cbxSmartCleanup.setChecked(self.config[0].File_Rename__use_smart_string_cleanup) self.cbxChangeExtension.setChecked(self.config[0].File_Rename__auto_extension) self.cbxMoveFiles.setChecked(self.config[0].File_Rename__move) self.cbxMoveOnly.setChecked(self.config[0].File_Rename__only_move) self.leDirectory.setText(self.config[0].File_Rename__dir) self.cbxRenameStrict.setChecked(self.config[0].File_Rename__strict_filenames) for table, replacments in zip( (self.twLiteralReplacements, self.twValueReplacements), self.config[0].File_Rename__replacements ): table.clearContents() for i in reversed(range(table.rowCount())): table.removeRow(i) for row, replacement in enumerate(replacments): self.insertRow(table, row, replacement) # Set talker values comictaggerlib.ui.talkeruigenerator.settings_to_talker_form(self.sources, self.config) self.connect_signals() def get_replacements(self) -> Replacements: literal_replacements = [] value_replacements = [] for row in range(self.twLiteralReplacements.rowCount()): if self.twLiteralReplacements.item(row, 0).text(): literal_replacements.append( Replacement( self.twLiteralReplacements.item(row, 0).text(), self.twLiteralReplacements.item(row, 1).text(), self.twLiteralReplacements.item(row, 2).checkState() == QtCore.Qt.CheckState.Checked, ) ) for row in range(self.twValueReplacements.rowCount()): if self.twValueReplacements.item(row, 0).text(): value_replacements.append( Replacement( self.twValueReplacements.item(row, 0).text(), self.twValueReplacements.item(row, 1).text(), self.twValueReplacements.item(row, 2).checkState() == QtCore.Qt.CheckState.Checked, ) ) return Replacements(literal_replacements, value_replacements) def accept(self) -> None: self.rename_test() if self.rename_error is not None: if isinstance(self.rename_error, ValueError): logger.exception("Invalid format string: %s", self.config[0].File_Rename__template) QtWidgets.QMessageBox.critical( self, "Invalid format string!", "Your rename template is invalid!" + f"