diff --git a/core/app.py b/core/app.py index 887572cb..1de58221 100644 --- a/core/app.py +++ b/core/app.py @@ -353,6 +353,11 @@ class DupeGuru(RegistrableApplication, Broadcaster): logging.warning("dupeGuru Warning: %s" % str(e)) return False + def reprioritize_groups(self, sort_key): + for group in self.results.groups: + group.prioritize(key_func=sort_key) + self._results_changed() + def reveal_selected(self): if self.selected_dupes: self._reveal_path(self.selected_dupes[0].path) diff --git a/core/gui/prioritize_dialog.py b/core/gui/prioritize_dialog.py index 63287c81..e4704e83 100644 --- a/core/gui/prioritize_dialog.py +++ b/core/gui/prioritize_dialog.py @@ -21,10 +21,30 @@ class CriterionCategoryList(SelectableList): class PrioritizeDialog: def __init__(self, view, app): + self.app = app self.categories = [KindCategory(app.results)] self.category_list = CriterionCategoryList(self) - self.criteria_list = [] + self.criteria = [] + self.criteria_list = SelectableList() + self.prioritizations = [] + #--- Private + def _sort_key(self, dupe): + # Our sort key consists of a tuple of inverted bool values represented as ints. When a dupe + # 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 def select_category(self, category): - criteria = category.criteria_list() - self.criteria_list = [c.value for c in criteria] + self.criteria = category.criteria_list() + self.criteria_list[:] = [c.value for c in self.criteria] + + def add_selected(self): + # Add selected criteria in criteria_list to prioritization_list. + crit = self.criteria[self.criteria_list.selected_index] + self.prioritizations.append(crit) + + def perform_reprioritization(self): + self.app.reprioritize_groups(self._sort_key) diff --git a/core/prioritize.py b/core/prioritize.py index a95b3898..0f973121 100644 --- a/core/prioritize.py +++ b/core/prioritize.py @@ -29,6 +29,9 @@ class Criterion: self.category = category self.value = value + def test_dupe(self, dupe): + return self.category._extract_value(dupe) == self.value + @property def display(self): return "{} ({})".format(self.category, self.value) diff --git a/core/tests/prioritize_test.py b/core/tests/prioritize_test.py index 41d4dc01..a0e8f39d 100644 --- a/core/tests/prioritize_test.py +++ b/core/tests/prioritize_test.py @@ -40,4 +40,14 @@ def app_normal_results(): def test_kind_subcrit(app): # The subcriteria of the "Kind" criteria is a list of extensions contained in the dupes. app.select_pri_criterion("Kind") - eq_(app.pdialog.criteria_list, ['ext1', 'ext2']) \ No newline at end of file + eq_(app.pdialog.criteria_list[:], ['ext1', 'ext2']) + +@with_app(app_normal_results) +def test_perform_reprioritization(app): + # Just a simple test of the system as a whole. + # select a criterion, and perform re-prioritization and see if it worked. + app.select_pri_criterion("Kind") + app.pdialog.criteria_list.select([1]) # ext2 + app.pdialog.add_selected() + app.pdialog.perform_reprioritization() + eq_(app.rtable[0].data[0], 'foo2.ext2')