mirror of
https://github.com/arsenetar/dupeguru.git
synced 2025-03-10 05:34:36 +00:00
[#138] Added FolderCategory and SizeCategory.
This commit is contained in:
parent
9c30486f14
commit
2dc588e0fd
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
from hscommon.gui.selectable_list import SelectableList
|
from hscommon.gui.selectable_list import SelectableList
|
||||||
|
|
||||||
from ..prioritize import KindCategory
|
from ..prioritize import all_categories
|
||||||
|
|
||||||
class CriterionCategoryList(SelectableList):
|
class CriterionCategoryList(SelectableList):
|
||||||
def __init__(self, dialog):
|
def __init__(self, dialog):
|
||||||
@ -22,29 +22,27 @@ class CriterionCategoryList(SelectableList):
|
|||||||
class PrioritizeDialog:
|
class PrioritizeDialog:
|
||||||
def __init__(self, view, app):
|
def __init__(self, view, app):
|
||||||
self.app = app
|
self.app = app
|
||||||
self.categories = [KindCategory(app.results)]
|
self.categories = [cat(app.results) for cat in all_categories()]
|
||||||
self.category_list = CriterionCategoryList(self)
|
self.category_list = CriterionCategoryList(self)
|
||||||
self.criteria = []
|
self.criteria = []
|
||||||
self.criteria_list = SelectableList()
|
self.criteria_list = SelectableList()
|
||||||
self.prioritizations = []
|
self.prioritizations = []
|
||||||
|
self.prioritization_list = SelectableList()
|
||||||
|
|
||||||
#--- Private
|
#--- Private
|
||||||
def _sort_key(self, dupe):
|
def _sort_key(self, dupe):
|
||||||
# Our sort key consists of a tuple of inverted bool values represented as ints. When a dupe
|
return tuple(crit.sort_key(dupe) for crit in self.prioritizations)
|
||||||
# fits a criteria, we want it at the top of the listm and thus we'll give it the value 0.
|
|
||||||
# When the dupe doesn't fit a criteria, we ant it at the bottom, and we give the value 1.
|
|
||||||
result = (crit.test_dupe(dupe) for crit in self.prioritizations)
|
|
||||||
return tuple((0 if value else 1) for value in result)
|
|
||||||
|
|
||||||
#--- Public
|
#--- Public
|
||||||
def select_category(self, category):
|
def select_category(self, category):
|
||||||
self.criteria = category.criteria_list()
|
self.criteria = category.criteria_list()
|
||||||
self.criteria_list[:] = [c.value for c in self.criteria]
|
self.criteria_list[:] = [c.display_value for c in self.criteria]
|
||||||
|
|
||||||
def add_selected(self):
|
def add_selected(self):
|
||||||
# Add selected criteria in criteria_list to prioritization_list.
|
# Add selected criteria in criteria_list to prioritization_list.
|
||||||
crit = self.criteria[self.criteria_list.selected_index]
|
crit = self.criteria[self.criteria_list.selected_index]
|
||||||
self.prioritizations.append(crit)
|
self.prioritizations.append(crit)
|
||||||
|
self.prioritization_list[:] = [crit.display for crit in self.prioritizations]
|
||||||
|
|
||||||
def perform_reprioritization(self):
|
def perform_reprioritization(self):
|
||||||
self.app.reprioritize_groups(self._sort_key)
|
self.app.reprioritize_groups(self._sort_key)
|
||||||
|
@ -15,30 +15,76 @@ class CriterionCategory:
|
|||||||
self.results = results
|
self.results = results
|
||||||
|
|
||||||
#--- Virtual
|
#--- Virtual
|
||||||
def _extract_value(self, dupe):
|
def extract_value(self, dupe):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def format_value(self, value):
|
||||||
|
return value
|
||||||
|
|
||||||
#--- Public
|
#--- Public
|
||||||
|
def sort_key(self, dupe, crit_value):
|
||||||
|
# Use this sort key when the order in the list depends on whether or not the dupe meets the
|
||||||
|
# criteria. If it does, we return 0 (top of the list), if it doesn't, we return 1.
|
||||||
|
if self.extract_value(dupe) == crit_value:
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
return 1
|
||||||
|
|
||||||
def criteria_list(self):
|
def criteria_list(self):
|
||||||
dupes = flatten(g[:] for g in self.results.groups)
|
dupes = flatten(g[:] for g in self.results.groups)
|
||||||
values = dedupe(self._extract_value(d) for d in dupes)
|
values = dedupe(self.extract_value(d) for d in dupes)
|
||||||
return [Criterion(self, value) for value in values]
|
return [Criterion(self, value) for value in values]
|
||||||
|
|
||||||
class Criterion:
|
class Criterion:
|
||||||
def __init__(self, category, value):
|
def __init__(self, category, value):
|
||||||
self.category = category
|
self.category = category
|
||||||
self.value = value
|
self.value = value
|
||||||
|
self.display_value = category.format_value(value)
|
||||||
|
|
||||||
def test_dupe(self, dupe):
|
def sort_key(self, dupe):
|
||||||
return self.category._extract_value(dupe) == self.value
|
return self.category.sort_key(dupe, self.value)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def display(self):
|
def display(self):
|
||||||
return "{} ({})".format(self.category, self.value)
|
return "{} ({})".format(self.category.NAME, self.value)
|
||||||
|
|
||||||
|
|
||||||
class KindCategory(CriterionCategory):
|
class KindCategory(CriterionCategory):
|
||||||
NAME = "Kind"
|
NAME = "Kind"
|
||||||
|
|
||||||
def _extract_value(self, dupe):
|
def extract_value(self, dupe):
|
||||||
return dupe.extension
|
return dupe.extension
|
||||||
|
|
||||||
|
class FolderCategory(CriterionCategory):
|
||||||
|
NAME = "Folder"
|
||||||
|
|
||||||
|
def extract_value(self, dupe):
|
||||||
|
return dupe.folder_path
|
||||||
|
|
||||||
|
def format_value(self, value):
|
||||||
|
return str(value)
|
||||||
|
|
||||||
|
class NumericalCategory(CriterionCategory):
|
||||||
|
HIGHEST = 0
|
||||||
|
LOWEST = 1
|
||||||
|
|
||||||
|
def format_value(self, value):
|
||||||
|
return "Highest" if value == self.HIGHEST else "Lowest"
|
||||||
|
|
||||||
|
def sort_key(self, dupe, crit_value):
|
||||||
|
value = self.extract_value(dupe)
|
||||||
|
if crit_value == self.HIGHEST: # we want highest values on top
|
||||||
|
value *= -1
|
||||||
|
return value
|
||||||
|
|
||||||
|
def criteria_list(self):
|
||||||
|
return [Criterion(self, self.HIGHEST), Criterion(self, self.LOWEST)]
|
||||||
|
|
||||||
|
class SizeCategory(NumericalCategory):
|
||||||
|
NAME = "Size"
|
||||||
|
|
||||||
|
def extract_value(self, dupe):
|
||||||
|
return dupe.size
|
||||||
|
|
||||||
|
def all_categories():
|
||||||
|
return [KindCategory, FolderCategory, SizeCategory]
|
||||||
|
@ -42,8 +42,8 @@ class NamedObject:
|
|||||||
def __init__(self, name="foobar", with_words=False, size=1, folder=None):
|
def __init__(self, name="foobar", with_words=False, size=1, folder=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
if folder is None:
|
if folder is None:
|
||||||
folder = Path('basepath')
|
folder = 'basepath'
|
||||||
self._folder = folder
|
self._folder = Path(folder)
|
||||||
self.size = size
|
self.size = size
|
||||||
self.md5partial = name
|
self.md5partial = name
|
||||||
self.md5 = name
|
self.md5 = name
|
||||||
|
@ -32,7 +32,9 @@ def app_with_dupes(dupes):
|
|||||||
def app_normal_results():
|
def app_normal_results():
|
||||||
# Just some results, with different extensions and size, for good measure.
|
# Just some results, with different extensions and size, for good measure.
|
||||||
dupes = [
|
dupes = [
|
||||||
[no('foo1.ext1', size=1), no('foo2.ext2', size=2)],
|
[
|
||||||
|
no('foo1.ext1', size=1, folder='folder1'),
|
||||||
|
no('foo2.ext2', size=2, folder='folder2')],
|
||||||
]
|
]
|
||||||
return app_with_dupes(dupes)
|
return app_with_dupes(dupes)
|
||||||
|
|
||||||
@ -43,7 +45,7 @@ def test_kind_subcrit(app):
|
|||||||
eq_(app.pdialog.criteria_list[:], ['ext1', 'ext2'])
|
eq_(app.pdialog.criteria_list[:], ['ext1', 'ext2'])
|
||||||
|
|
||||||
@with_app(app_normal_results)
|
@with_app(app_normal_results)
|
||||||
def test_perform_reprioritization(app):
|
def test_kind_reprioritization(app):
|
||||||
# Just a simple test of the system as a whole.
|
# Just a simple test of the system as a whole.
|
||||||
# select a criterion, and perform re-prioritization and see if it worked.
|
# select a criterion, and perform re-prioritization and see if it worked.
|
||||||
app.select_pri_criterion("Kind")
|
app.select_pri_criterion("Kind")
|
||||||
@ -51,3 +53,44 @@ def test_perform_reprioritization(app):
|
|||||||
app.pdialog.add_selected()
|
app.pdialog.add_selected()
|
||||||
app.pdialog.perform_reprioritization()
|
app.pdialog.perform_reprioritization()
|
||||||
eq_(app.rtable[0].data[0], 'foo2.ext2')
|
eq_(app.rtable[0].data[0], 'foo2.ext2')
|
||||||
|
|
||||||
|
@with_app(app_normal_results)
|
||||||
|
def test_folder_subcrit(app):
|
||||||
|
app.select_pri_criterion("Folder")
|
||||||
|
eq_(app.pdialog.criteria_list[:], ['folder1', 'folder2'])
|
||||||
|
|
||||||
|
@with_app(app_normal_results)
|
||||||
|
def test_folder_reprioritization(app):
|
||||||
|
app.select_pri_criterion("Folder")
|
||||||
|
app.pdialog.criteria_list.select([1]) # folder2
|
||||||
|
app.pdialog.add_selected()
|
||||||
|
app.pdialog.perform_reprioritization()
|
||||||
|
eq_(app.rtable[0].data[0], 'foo2.ext2')
|
||||||
|
|
||||||
|
@with_app(app_normal_results)
|
||||||
|
def test_prilist_display(app):
|
||||||
|
# The prioritization list displays selected criteria correctly.
|
||||||
|
app.select_pri_criterion("Kind")
|
||||||
|
app.pdialog.criteria_list.select([1]) # ext2
|
||||||
|
app.pdialog.add_selected()
|
||||||
|
app.select_pri_criterion("Folder")
|
||||||
|
app.pdialog.criteria_list.select([1]) # folder2
|
||||||
|
app.pdialog.add_selected()
|
||||||
|
expected = [
|
||||||
|
"Kind (ext2)",
|
||||||
|
"Folder (folder2)",
|
||||||
|
]
|
||||||
|
eq_(app.pdialog.prioritization_list[:], expected)
|
||||||
|
|
||||||
|
@with_app(app_normal_results)
|
||||||
|
def test_size_subcrit(app):
|
||||||
|
app.select_pri_criterion("Size")
|
||||||
|
eq_(app.pdialog.criteria_list[:], ['Highest', 'Lowest'])
|
||||||
|
|
||||||
|
@with_app(app_normal_results)
|
||||||
|
def test_size_reprioritization(app):
|
||||||
|
app.select_pri_criterion("Size")
|
||||||
|
app.pdialog.criteria_list.select([0]) # highest
|
||||||
|
app.pdialog.add_selected()
|
||||||
|
app.pdialog.perform_reprioritization()
|
||||||
|
eq_(app.rtable[0].data[0], 'foo2.ext2')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user