From 2ed1b82ecfe7812c0bb21f9d3554643350fc0d83 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Tue, 24 May 2016 22:53:03 -0400 Subject: [PATCH] Push edition-specific scan option listing down to the core ... rather than have each UI layer repeat them. Did qt, but not cocoa yet. --- core/scanner.py | 14 +++++++++--- core_me/scanner.py | 27 ++++++++++++++++------- core_pe/scanner.py | 26 ++++++++++++++--------- core_se/app.py | 20 +++++++++--------- core_se/scanner.py | 18 ++++++++++++++++ qt/base/preferences_dialog.py | 30 ++++++++++++++++---------- qt/me/preferences_dialog.py | 40 ++++++----------------------------- qt/pe/preferences_dialog.py | 35 ++++++------------------------ qt/se/preferences_dialog.py | 37 ++++++-------------------------- 9 files changed, 112 insertions(+), 135 deletions(-) create mode 100644 core_se/scanner.py diff --git a/core/scanner.py b/core/scanner.py index 78468daf..32129486 100644 --- a/core/scanner.py +++ b/core/scanner.py @@ -1,6 +1,4 @@ -# Created By: Virgil Dupras -# Created On: 2006/03/03 -# Copyright 2015 Hardcoded Software (http://www.hardcoded.net) +# Copyright 2016 Hardcoded Software (http://www.hardcoded.net) # # This software is licensed under the "GPLv3" License as described in the "LICENSE" file, # which should be included with this package. The terms are also available at @@ -9,6 +7,7 @@ import logging import re import os.path as op +from collections import namedtuple from hscommon.jobprogress import job from hscommon.util import dedupe, rem_file_ext, get_file_ext @@ -34,6 +33,8 @@ class ScanType: FuzzyBlock = 10 ExifTimestamp = 11 +ScanOption = namedtuple('ScanOption', 'scan_type label') + SCANNABLE_TAGS = ['track', 'artist', 'album', 'title', 'genre', 'year'] RE_DIGIT_ENDING = re.compile(r'\d+|\(\d+\)|\[\d+\]|{\d+}') @@ -125,6 +126,13 @@ class Scanner: return True return len(dupe.path) > len(ref.path) + def get_scan_options(self): + """Returns a list of scanning options for this scanner. + + Returns a list of ``ScanOption``. + """ + raise NotImplementedError() + def get_dupe_groups(self, files, j=job.nulljob): j = j.start_subjob([8, 2]) for f in (f for f in files if not hasattr(f, 'is_ref')): diff --git a/core_me/scanner.py b/core_me/scanner.py index 6b45fd79..2e42170b 100644 --- a/core_me/scanner.py +++ b/core_me/scanner.py @@ -1,15 +1,26 @@ -# Created By: Virgil Dupras -# Created On: 2006/03/03 -# Copyright 2015 Hardcoded Software (http://www.hardcoded.net) -# -# This software is licensed under the "GPLv3" License as described in the "LICENSE" file, -# which should be included with this package. The terms are also available at +# Copyright 2016 Hardcoded Software (http://www.hardcoded.net) +# +# This software is licensed under the "GPLv3" License as described in the "LICENSE" file, +# which should be included with this package. The terms are also available at # http://www.gnu.org/licenses/gpl-3.0.html -from core.scanner import Scanner as ScannerBase +from hscommon.trans import tr + +from core.scanner import Scanner as ScannerBase, ScanOption, ScanType class ScannerME(ScannerBase): @staticmethod def _key_func(dupe): return (-dupe.bitrate, -dupe.size) - + + def get_scan_options(self): + return [ + ScanOption(ScanType.Filename, tr("Filename")), + ScanOption(ScanType.Fields, tr("Filename - Fields")), + ScanOption(ScanType.FieldsNoOrder, tr("Filename - Fields (No Order)")), + ScanOption(ScanType.Tag, tr("Tags")), + ScanOption(ScanType.Contents, tr("Contents")), + ScanOption(ScanType.ContentsAudio, tr("Audio Contents")), + ] + + diff --git a/core_pe/scanner.py b/core_pe/scanner.py index d154cea4..6fb4eb90 100644 --- a/core_pe/scanner.py +++ b/core_pe/scanner.py @@ -1,12 +1,12 @@ -# Created By: Virgil Dupras -# Created On: 2009-10-18 -# Copyright 2015 Hardcoded Software (http://www.hardcoded.net) -# -# This software is licensed under the "GPLv3" License as described in the "LICENSE" file, -# which should be included with this package. The terms are also available at +# Copyright 2016 Hardcoded Software (http://www.hardcoded.net) +# +# This software is licensed under the "GPLv3" License as described in the "LICENSE" file, +# which should be included with this package. The terms are also available at # http://www.gnu.org/licenses/gpl-3.0.html -from core.scanner import Scanner, ScanType +from hscommon.trans import tr + +from core.scanner import Scanner, ScanType, ScanOption from . import matchblock, matchexif from .cache import Cache @@ -15,7 +15,13 @@ class ScannerPE(Scanner): cache_path = None match_scaled = False threshold = 75 - + + def get_scan_options(self): + return [ + ScanOption(ScanType.FuzzyBlock, tr("Contents")), + ScanOption(ScanType.ExifTimestamp, tr("EXIF Timestamp")), + ] + def _getmatches(self, files, j): if self.scan_type == ScanType.FuzzyBlock: return matchblock.getmatches(files, self.cache_path, self.threshold, self.match_scaled, j) @@ -23,9 +29,9 @@ class ScannerPE(Scanner): return matchexif.getmatches(files, self.match_scaled, j) else: raise Exception("Invalid scan type") - + def clear_picture_cache(self): cache = Cache(self.cache_path) cache.clear() cache.close() - + diff --git a/core_se/app.py b/core_se/app.py index 7de03976..e90cad60 100644 --- a/core_se/app.py +++ b/core_se/app.py @@ -1,27 +1,27 @@ -# Created On: 2011/09/20 -# Copyright 2015 Hardcoded Software (http://www.hardcoded.net) -# -# This software is licensed under the "GPLv3" License as described in the "LICENSE" file, -# which should be included with this package. The terms are also available at +# Copyright 2016 Hardcoded Software (http://www.hardcoded.net) +# +# This software is licensed under the "GPLv3" License as described in the "LICENSE" file, +# which should be included with this package. The terms are also available at # http://www.gnu.org/licenses/gpl-3.0.html from core.app import DupeGuru as DupeGuruBase from core import prioritize -from . import __appname__, fs +from . import __appname__, fs, scanner from .result_table import ResultTable class DupeGuru(DupeGuruBase): NAME = __appname__ METADATA_TO_READ = ['size', 'mtime'] - + SCANNER_CLASS = scanner.ScannerSE + def __init__(self, view): DupeGuruBase.__init__(self, view) self.directories.fileclasses = [fs.File] self.directories.folderclass = fs.Folder - + def _prioritization_categories(self): return prioritize.all_categories() - + def _create_result_table(self): return ResultTable(self) - + diff --git a/core_se/scanner.py b/core_se/scanner.py new file mode 100644 index 00000000..aab8f8dd --- /dev/null +++ b/core_se/scanner.py @@ -0,0 +1,18 @@ +# Copyright 2016 Hardcoded Software (http://www.hardcoded.net) +# +# This software is licensed under the "GPLv3" License as described in the "LICENSE" file, +# which should be included with this package. The terms are also available at +# http://www.gnu.org/licenses/gpl-3.0.html + +from hscommon.trans import tr + +from core.scanner import Scanner as ScannerBase, ScanOption, ScanType + +class ScannerSE(ScannerBase): + def get_scan_options(self): + return [ + ScanOption(ScanType.Filename, tr("Filename")), + ScanOption(ScanType.Contents, tr("Contents")), + ScanOption(ScanType.Folders, tr("Folders")), + ] + diff --git a/qt/base/preferences_dialog.py b/qt/base/preferences_dialog.py index 1021ae98..d5f009a7 100644 --- a/qt/base/preferences_dialog.py +++ b/qt/base/preferences_dialog.py @@ -37,18 +37,17 @@ class PreferencesDialogBase(QDialog): self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) - def _setupScanTypeBox(self, labels): - self.scanTypeHLayout = QHBoxLayout() - self.scanTypeLabel = QLabel(self) - self.scanTypeLabel.setText(tr("Scan Type:")) - self.scanTypeLabel.setMinimumSize(QSize(100, 0)) - self.scanTypeLabel.setMaximumSize(QSize(100, 16777215)) - self.scanTypeHLayout.addWidget(self.scanTypeLabel) + def _setupScanTypeBox(self): + hl = QHBoxLayout() + label = QLabel(tr("Scan Type:"), self) + label.setMinimumSize(QSize(100, 0)) + label.setMaximumSize(QSize(100, 16777215)) + hl.addWidget(label) self.scanTypeComboBox = QComboBox(self) - for label in labels: - self.scanTypeComboBox.addItem(label) - self.scanTypeHLayout.addWidget(self.scanTypeComboBox) - self.widgetsVLayout.addLayout(self.scanTypeHLayout) + for scan_option in self.app.model.scanner.get_scan_options(): + self.scanTypeComboBox.addItem(scan_option.label) + hl.addWidget(self.scanTypeComboBox) + self.widgetsVLayout.addLayout(hl) def _setupFilterHardnessBox(self): self.filterHardnessHLayout = QHBoxLayout() @@ -141,10 +140,19 @@ class PreferencesDialogBase(QDialog): self.mainVLayout.removeWidget(self.ignoreHardlinkMatches) self.ignoreHardlinkMatches.setHidden(True) + def _load_scan_type(self, prefs): + SCAN_TYPE_ORDER = [so.scan_type for so in self.app.model.scanner.get_scan_options()] + scan_type_index = SCAN_TYPE_ORDER.index(prefs.scan_type) + self.scanTypeComboBox.setCurrentIndex(scan_type_index) + def _load(self, prefs, setchecked): # Edition-specific pass + def _save_scan_type(self, prefs): + scan_options = self.app.model.scanner.get_scan_options() + prefs.scan_type = scan_options[self.scanTypeComboBox.currentIndex()].scan_type + def _save(self, prefs, ischecked): # Edition-specific pass diff --git a/qt/me/preferences_dialog.py b/qt/me/preferences_dialog.py index 2fd87d55..d50f5ae3 100644 --- a/qt/me/preferences_dialog.py +++ b/qt/me/preferences_dialog.py @@ -1,16 +1,14 @@ # Created By: Virgil Dupras # Created On: 2009-04-29 -# Copyright 2015 Hardcoded Software (http://www.hardcoded.net) +# Copyright 2016 Hardcoded Software (http://www.hardcoded.net) # # This software is licensed under the "GPLv3" License as described in the "LICENSE" file, # which should be included with this package. The terms are also available at # http://www.gnu.org/licenses/gpl-3.0.html -import sys from PyQt5.QtCore import QSize from PyQt5.QtWidgets import ( QVBoxLayout, QHBoxLayout, QLabel, QSizePolicy, QSpacerItem, QWidget, - QApplication ) from hscommon.trans import trget @@ -21,15 +19,6 @@ from . import preferences tr = trget('ui') -SCAN_TYPE_ORDER = [ - ScanType.Filename, - ScanType.Fields, - ScanType.FieldsNoOrder, - ScanType.Tag, - ScanType.Contents, - ScanType.ContentsAudio, -] - class PreferencesDialog(PreferencesDialogBase): def __init__(self, parent, app): PreferencesDialogBase.__init__(self, parent, app) @@ -37,15 +26,7 @@ class PreferencesDialog(PreferencesDialogBase): self.scanTypeComboBox.currentIndexChanged[int].connect(self.scanTypeChanged) def _setupPreferenceWidgets(self): - scanTypeLabels = [ - tr("Filename"), - tr("Filename - Fields"), - tr("Filename - Fields (No Order)"), - tr("Tags"), - tr("Contents"), - tr("Audio Contents"), - ] - self._setupScanTypeBox(scanTypeLabels) + self._setupScanTypeBox() self._setupFilterHardnessBox() self.widgetsVLayout.addLayout(self.filterHardnessHLayout) self.widget = QWidget(self) @@ -91,8 +72,7 @@ class PreferencesDialog(PreferencesDialogBase): self._setupBottomPart() def _load(self, prefs, setchecked): - scan_type_index = SCAN_TYPE_ORDER.index(prefs.scan_type) - self.scanTypeComboBox.setCurrentIndex(scan_type_index) + self._load_scan_type(prefs) setchecked(self.tagTrackBox, prefs.scan_tag_track) setchecked(self.tagArtistBox, prefs.scan_tag_artist) setchecked(self.tagAlbumBox, prefs.scan_tag_album) @@ -103,7 +83,7 @@ class PreferencesDialog(PreferencesDialogBase): setchecked(self.wordWeightingBox, prefs.word_weighting) def _save(self, prefs, ischecked): - prefs.scan_type = SCAN_TYPE_ORDER[self.scanTypeComboBox.currentIndex()] + self._save_scan_type(prefs) prefs.scan_tag_track = ischecked(self.tagTrackBox) prefs.scan_tag_artist = ischecked(self.tagArtistBox) prefs.scan_tag_album = ischecked(self.tagAlbumBox) @@ -118,7 +98,8 @@ class PreferencesDialog(PreferencesDialogBase): #--- Events def scanTypeChanged(self, index): - scan_type = SCAN_TYPE_ORDER[self.scanTypeComboBox.currentIndex()] + scan_options = self.app.model.scanner.get_scan_options() + scan_type = scan_options[self.scanTypeComboBox.currentIndex()].scan_type word_based = scan_type in ( ScanType.Filename, ScanType.Fields, ScanType.FieldsNoOrder, ScanType.Tag @@ -134,12 +115,3 @@ class PreferencesDialog(PreferencesDialogBase): self.tagGenreBox.setEnabled(tag_based) self.tagYearBox.setEnabled(tag_based) - -if __name__ == '__main__': - from ..testapp import TestApp - app = QApplication([]) - dgapp = TestApp() - dialog = PreferencesDialog(None, dgapp) - dialog.show() - sys.exit(app.exec_()) - diff --git a/qt/pe/preferences_dialog.py b/qt/pe/preferences_dialog.py index 2aebc98e..0792cf42 100644 --- a/qt/pe/preferences_dialog.py +++ b/qt/pe/preferences_dialog.py @@ -1,14 +1,9 @@ -# Created By: Virgil Dupras -# Created On: 2009-04-29 -# Copyright 2015 Hardcoded Software (http://www.hardcoded.net) +# Copyright 2016 Hardcoded Software (http://www.hardcoded.net) # # This software is licensed under the "GPLv3" License as described in the "LICENSE" file, # which should be included with this package. The terms are also available at # http://www.gnu.org/licenses/gpl-3.0.html -import sys -from PyQt5.QtWidgets import QApplication - from hscommon.trans import trget from core.scanner import ScanType @@ -17,11 +12,6 @@ from . import preferences tr = trget('ui') -SCAN_TYPE_ORDER = [ - ScanType.FuzzyBlock, - ScanType.ExifTimestamp, -] - class PreferencesDialog(PreferencesDialogBase): def __init__(self, parent, app): PreferencesDialogBase.__init__(self, parent, app) @@ -29,11 +19,7 @@ class PreferencesDialog(PreferencesDialogBase): self.scanTypeComboBox.currentIndexChanged[int].connect(self.scanTypeChanged) def _setupPreferenceWidgets(self): - scanTypeLabels = [ - tr("Contents"), - tr("EXIF Timestamp"), - ] - self._setupScanTypeBox(scanTypeLabels) + self._setupScanTypeBox() self._setupFilterHardnessBox() self.widgetsVLayout.addLayout(self.filterHardnessHLayout) self._setupAddCheckbox('matchScaledBox', tr("Match pictures of different dimensions")) @@ -51,12 +37,11 @@ class PreferencesDialog(PreferencesDialogBase): self._setupBottomPart() def _load(self, prefs, setchecked): - scan_type_index = SCAN_TYPE_ORDER.index(prefs.scan_type) - self.scanTypeComboBox.setCurrentIndex(scan_type_index) + self._load_scan_type(prefs) setchecked(self.matchScaledBox, prefs.match_scaled) def _save(self, prefs, ischecked): - prefs.scan_type = SCAN_TYPE_ORDER[self.scanTypeComboBox.currentIndex()] + self._save_scan_type(prefs) prefs.match_scaled = ischecked(self.matchScaledBox) def resetToDefaults(self): @@ -64,16 +49,8 @@ class PreferencesDialog(PreferencesDialogBase): #--- Events def scanTypeChanged(self, index): - scan_type = SCAN_TYPE_ORDER[self.scanTypeComboBox.currentIndex()] + scan_options = self.app.model.scanner.get_scan_options() + scan_type = scan_options[self.scanTypeComboBox.currentIndex()].scan_type fuzzy_scan = scan_type == ScanType.FuzzyBlock self.filterHardnessSlider.setEnabled(fuzzy_scan) - -if __name__ == '__main__': - from ..testapp import TestApp - app = QApplication([]) - dgapp = TestApp() - dialog = PreferencesDialog(None, dgapp) - dialog.show() - sys.exit(app.exec_()) - diff --git a/qt/se/preferences_dialog.py b/qt/se/preferences_dialog.py index fe226cda..2c62c968 100644 --- a/qt/se/preferences_dialog.py +++ b/qt/se/preferences_dialog.py @@ -1,16 +1,12 @@ -# Created By: Virgil Dupras -# Created On: 2009-05-24 -# Copyright 2015 Hardcoded Software (http://www.hardcoded.net) +# Copyright 2016 Hardcoded Software (http://www.hardcoded.net) # # This software is licensed under the "GPLv3" License as described in the "LICENSE" file, # which should be included with this package. The terms are also available at # http://www.gnu.org/licenses/gpl-3.0.html -import sys from PyQt5.QtCore import QSize from PyQt5.QtWidgets import ( - QVBoxLayout, QHBoxLayout, QLabel, QSizePolicy, QSpacerItem, QWidget, - QLineEdit, QApplication + QVBoxLayout, QHBoxLayout, QLabel, QSizePolicy, QSpacerItem, QWidget, QLineEdit ) from hscommon.plat import ISWINDOWS, ISLINUX @@ -24,12 +20,6 @@ from . import preferences tr = trget('ui') -SCAN_TYPE_ORDER = [ - ScanType.Filename, - ScanType.Contents, - ScanType.Folders, -] - class PreferencesDialog(PreferencesDialogBase): def __init__(self, parent, app, **kwargs): super().__init__(parent, app, **kwargs) @@ -37,12 +27,7 @@ class PreferencesDialog(PreferencesDialogBase): self.scanTypeComboBox.currentIndexChanged[int].connect(self.scanTypeChanged) def _setupPreferenceWidgets(self): - scanTypeLabels = [ - tr("Filename"), - tr("Contents"), - tr("Folders"), - ] - self._setupScanTypeBox(scanTypeLabels) + self._setupScanTypeBox() self._setupFilterHardnessBox() self.widgetsVLayout.addLayout(self.filterHardnessHLayout) self.widget = QWidget(self) @@ -96,15 +81,14 @@ class PreferencesDialog(PreferencesDialogBase): self.resize(self.width(), 440) def _load(self, prefs, setchecked): - scan_type_index = SCAN_TYPE_ORDER.index(prefs.scan_type) - self.scanTypeComboBox.setCurrentIndex(scan_type_index) + self._load_scan_type(prefs) setchecked(self.matchSimilarBox, prefs.match_similar) setchecked(self.wordWeightingBox, prefs.word_weighting) setchecked(self.ignoreSmallFilesBox, prefs.ignore_small_files) self.sizeThresholdEdit.setText(str(prefs.small_file_threshold)) def _save(self, prefs, ischecked): - prefs.scan_type = SCAN_TYPE_ORDER[self.scanTypeComboBox.currentIndex()] + self._save_scan_type(prefs) prefs.match_similar = ischecked(self.matchSimilarBox) prefs.word_weighting = ischecked(self.wordWeightingBox) prefs.ignore_small_files = ischecked(self.ignoreSmallFilesBox) @@ -115,17 +99,10 @@ class PreferencesDialog(PreferencesDialogBase): #--- Events def scanTypeChanged(self, index): - scan_type = SCAN_TYPE_ORDER[self.scanTypeComboBox.currentIndex()] + scan_options = self.app.model.scanner.get_scan_options() + scan_type = scan_options[self.scanTypeComboBox.currentIndex()].scan_type word_based = scan_type == ScanType.Filename self.filterHardnessSlider.setEnabled(word_based) self.matchSimilarBox.setEnabled(word_based) self.wordWeightingBox.setEnabled(word_based) - -if __name__ == '__main__': - from ..testapp import TestApp - app = QApplication([]) - dgapp = TestApp() - dialog = PreferencesDialog(None, dgapp) - dialog.show() - sys.exit(app.exec_())