2009-08-05 08:59:46 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2006/02/23
|
2010-01-01 20:11:34 +00:00
|
|
|
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
|
2009-08-05 08:59:46 +00:00
|
|
|
#
|
2010-09-30 10:17:41 +00:00
|
|
|
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
2009-08-05 08:59:46 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2010-09-30 10:17:41 +00:00
|
|
|
# http://www.hardcoded.net/licenses/bsd_license
|
2009-08-05 08:59:46 +00:00
|
|
|
|
2009-08-31 15:33:44 +00:00
|
|
|
import logging
|
2009-06-01 09:55:11 +00:00
|
|
|
import re
|
2010-08-15 12:42:55 +00:00
|
|
|
from xml.etree import ElementTree as ET
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
from . import engine
|
2010-11-20 11:42:15 +00:00
|
|
|
from jobprogress.job import nulljob
|
2010-07-13 06:08:18 +00:00
|
|
|
from hscommon.markable import Markable
|
2011-01-11 12:36:05 +00:00
|
|
|
from hscommon.util import flatten, nonone, FileOrPath, format_size
|
2011-01-18 16:33:33 +00:00
|
|
|
from hscommon.trans import tr
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
class Results(Markable):
|
|
|
|
#---Override
|
|
|
|
def __init__(self, data_module):
|
|
|
|
super(Results, self).__init__()
|
|
|
|
self.__groups = []
|
|
|
|
self.__group_of_duplicate = {}
|
|
|
|
self.__groups_sort_descriptor = None # This is a tuple (key, asc)
|
|
|
|
self.__dupes = None
|
|
|
|
self.__dupes_sort_descriptor = None # This is a tuple (key, asc, delta)
|
|
|
|
self.__filters = None
|
|
|
|
self.__filtered_dupes = None
|
|
|
|
self.__filtered_groups = None
|
|
|
|
self.__recalculate_stats()
|
|
|
|
self.__marked_size = 0
|
|
|
|
self.data = data_module
|
2010-04-12 10:21:01 +00:00
|
|
|
self.problems = [] # (dupe, error_msg)
|
2010-08-13 09:37:45 +00:00
|
|
|
self.is_modified = False
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def _did_mark(self, dupe):
|
|
|
|
self.__marked_size += dupe.size
|
|
|
|
|
|
|
|
def _did_unmark(self, dupe):
|
|
|
|
self.__marked_size -= dupe.size
|
|
|
|
|
|
|
|
def _get_markable_count(self):
|
|
|
|
return self.__total_count
|
|
|
|
|
|
|
|
def _is_markable(self, dupe):
|
|
|
|
if dupe.is_ref:
|
|
|
|
return False
|
|
|
|
g = self.get_group_of_duplicate(dupe)
|
|
|
|
if not g:
|
|
|
|
return False
|
|
|
|
if dupe is g.ref:
|
|
|
|
return False
|
|
|
|
if self.__filtered_dupes and dupe not in self.__filtered_dupes:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
#---Private
|
|
|
|
def __get_dupe_list(self):
|
|
|
|
if self.__dupes is None:
|
|
|
|
self.__dupes = flatten(group.dupes for group in self.groups)
|
2009-09-01 12:42:43 +00:00
|
|
|
if None in self.__dupes:
|
|
|
|
# This is debug logging to try to figure out #44
|
|
|
|
logging.warning("There is a None value in the Results' dupe list. dupes: %r groups: %r", self.__dupes, self.groups)
|
2009-06-01 09:55:11 +00:00
|
|
|
if self.__filtered_dupes:
|
|
|
|
self.__dupes = [dupe for dupe in self.__dupes if dupe in self.__filtered_dupes]
|
|
|
|
sd = self.__dupes_sort_descriptor
|
|
|
|
if sd:
|
|
|
|
self.sort_dupes(sd[0], sd[1], sd[2])
|
|
|
|
return self.__dupes
|
|
|
|
|
|
|
|
def __get_groups(self):
|
|
|
|
if self.__filtered_groups is None:
|
|
|
|
return self.__groups
|
|
|
|
else:
|
|
|
|
return self.__filtered_groups
|
|
|
|
|
|
|
|
def __get_stat_line(self):
|
|
|
|
if self.__filtered_dupes is None:
|
|
|
|
mark_count = self.mark_count
|
|
|
|
marked_size = self.__marked_size
|
|
|
|
total_count = self.__total_count
|
|
|
|
total_size = self.__total_size
|
|
|
|
else:
|
|
|
|
mark_count = len([dupe for dupe in self.__filtered_dupes if self.is_marked(dupe)])
|
|
|
|
marked_size = sum(dupe.size for dupe in self.__filtered_dupes if self.is_marked(dupe))
|
|
|
|
total_count = len([dupe for dupe in self.__filtered_dupes if self.is_markable(dupe)])
|
|
|
|
total_size = sum(dupe.size for dupe in self.__filtered_dupes if self.is_markable(dupe))
|
|
|
|
if self.mark_inverted:
|
|
|
|
marked_size = self.__total_size - marked_size
|
2011-01-18 16:33:33 +00:00
|
|
|
result = tr("%d / %d (%s / %s) duplicates marked.") % (
|
2009-06-01 09:55:11 +00:00
|
|
|
mark_count,
|
|
|
|
total_count,
|
|
|
|
format_size(marked_size, 2),
|
|
|
|
format_size(total_size, 2),
|
|
|
|
)
|
|
|
|
if self.__filters:
|
2011-01-18 16:33:33 +00:00
|
|
|
result += tr(" filter: %s") % ' --> '.join(self.__filters)
|
2009-06-01 09:55:11 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
def __recalculate_stats(self):
|
|
|
|
self.__total_size = 0
|
|
|
|
self.__total_count = 0
|
|
|
|
for group in self.groups:
|
|
|
|
markable = [dupe for dupe in group.dupes if self._is_markable(dupe)]
|
|
|
|
self.__total_count += len(markable)
|
|
|
|
self.__total_size += sum(dupe.size for dupe in markable)
|
|
|
|
|
|
|
|
def __set_groups(self, new_groups):
|
|
|
|
self.mark_none()
|
|
|
|
self.__groups = new_groups
|
|
|
|
self.__group_of_duplicate = {}
|
|
|
|
for g in self.__groups:
|
|
|
|
for dupe in g:
|
|
|
|
self.__group_of_duplicate[dupe] = g
|
|
|
|
if not hasattr(dupe, 'is_ref'):
|
|
|
|
dupe.is_ref = False
|
2011-01-14 14:12:02 +00:00
|
|
|
self.is_modified = bool(self.__groups)
|
2009-06-01 09:55:11 +00:00
|
|
|
old_filters = nonone(self.__filters, [])
|
|
|
|
self.apply_filter(None)
|
|
|
|
for filter_str in old_filters:
|
|
|
|
self.apply_filter(filter_str)
|
|
|
|
|
|
|
|
#---Public
|
|
|
|
def apply_filter(self, filter_str):
|
|
|
|
''' Applies a filter 'filter_str' to self.groups
|
|
|
|
|
|
|
|
When you apply the filter, only dupes with the filename matching 'filter_str' will be in
|
|
|
|
in the results. To cancel the filter, just call apply_filter with 'filter_str' to None,
|
|
|
|
and the results will go back to normal.
|
|
|
|
|
|
|
|
If call apply_filter on a filtered results, the filter will be applied
|
|
|
|
*on the filtered results*.
|
|
|
|
|
|
|
|
'filter_str' is a string containing a regexp to filter dupes with.
|
|
|
|
'''
|
|
|
|
if not filter_str:
|
|
|
|
self.__filtered_dupes = None
|
|
|
|
self.__filtered_groups = None
|
|
|
|
self.__filters = None
|
|
|
|
else:
|
|
|
|
if not self.__filters:
|
|
|
|
self.__filters = []
|
2009-09-29 11:15:36 +00:00
|
|
|
try:
|
|
|
|
filter_re = re.compile(filter_str, re.IGNORECASE)
|
|
|
|
except re.error:
|
|
|
|
return # don't apply this filter.
|
2009-06-01 09:55:11 +00:00
|
|
|
self.__filters.append(filter_str)
|
|
|
|
if self.__filtered_dupes is None:
|
|
|
|
self.__filtered_dupes = flatten(g[:] for g in self.groups)
|
2010-08-11 14:39:06 +00:00
|
|
|
self.__filtered_dupes = set(dupe for dupe in self.__filtered_dupes if filter_re.search(str(dupe.path)))
|
2009-06-01 09:55:11 +00:00
|
|
|
filtered_groups = set()
|
|
|
|
for dupe in self.__filtered_dupes:
|
|
|
|
filtered_groups.add(self.get_group_of_duplicate(dupe))
|
|
|
|
self.__filtered_groups = list(filtered_groups)
|
|
|
|
self.__recalculate_stats()
|
|
|
|
sd = self.__groups_sort_descriptor
|
|
|
|
if sd:
|
|
|
|
self.sort_groups(sd[0], sd[1])
|
|
|
|
self.__dupes = None
|
|
|
|
|
|
|
|
def get_group_of_duplicate(self, dupe):
|
|
|
|
try:
|
|
|
|
return self.__group_of_duplicate[dupe]
|
|
|
|
except (TypeError, KeyError):
|
|
|
|
return None
|
|
|
|
|
|
|
|
is_markable = _is_markable
|
|
|
|
|
|
|
|
def load_from_xml(self, infile, get_file, j=nulljob):
|
2010-03-01 11:21:43 +00:00
|
|
|
def do_match(ref_file, other_files, group):
|
|
|
|
if not other_files:
|
|
|
|
return
|
|
|
|
for other_file in other_files:
|
|
|
|
group.add_match(engine.get_match(ref_file, other_file))
|
|
|
|
do_match(other_files[0], other_files[1:], group)
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
self.apply_filter(None)
|
|
|
|
try:
|
2010-08-15 12:42:55 +00:00
|
|
|
root = ET.parse(infile).getroot()
|
2010-03-01 11:21:43 +00:00
|
|
|
except Exception:
|
2009-06-01 09:55:11 +00:00
|
|
|
return
|
2010-08-15 12:42:55 +00:00
|
|
|
group_elems = list(root.getiterator('group'))
|
2010-03-01 11:21:43 +00:00
|
|
|
groups = []
|
|
|
|
marked = set()
|
|
|
|
for group_elem in j.iter_with_progress(group_elems, every=100):
|
|
|
|
group = engine.Group()
|
|
|
|
dupes = []
|
2010-08-15 12:42:55 +00:00
|
|
|
for file_elem in group_elem.getiterator('file'):
|
2010-03-01 11:21:43 +00:00
|
|
|
path = file_elem.get('path')
|
|
|
|
words = file_elem.get('words', '')
|
|
|
|
if not path:
|
|
|
|
continue
|
|
|
|
file = get_file(path)
|
|
|
|
if file is None:
|
|
|
|
continue
|
|
|
|
file.words = words.split(',')
|
|
|
|
file.is_ref = file_elem.get('is_ref') == 'y'
|
|
|
|
dupes.append(file)
|
|
|
|
if file_elem.get('marked') == 'y':
|
|
|
|
marked.add(file)
|
2010-08-15 12:42:55 +00:00
|
|
|
for match_elem in group_elem.getiterator('match'):
|
2010-03-01 11:21:43 +00:00
|
|
|
try:
|
|
|
|
attrs = match_elem.attrib
|
|
|
|
first_file = dupes[int(attrs['first'])]
|
|
|
|
second_file = dupes[int(attrs['second'])]
|
|
|
|
percentage = int(attrs['percentage'])
|
|
|
|
group.add_match(engine.Match(first_file, second_file, percentage))
|
|
|
|
except (IndexError, KeyError, ValueError): # Covers missing attr, non-int values and indexes out of bounds
|
|
|
|
pass
|
|
|
|
if (not group.matches) and (len(dupes) >= 2):
|
|
|
|
do_match(dupes[0], dupes[1:], group)
|
|
|
|
group.prioritize(lambda x: dupes.index(x))
|
|
|
|
if len(group):
|
|
|
|
groups.append(group)
|
|
|
|
j.add_progress()
|
|
|
|
self.groups = groups
|
|
|
|
for dupe_file in marked:
|
2009-06-01 09:55:11 +00:00
|
|
|
self.mark(dupe_file)
|
2010-08-13 09:37:45 +00:00
|
|
|
self.is_modified = False
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def make_ref(self, dupe):
|
|
|
|
g = self.get_group_of_duplicate(dupe)
|
|
|
|
r = g.ref
|
|
|
|
self._remove_mark_flag(dupe)
|
|
|
|
g.switch_ref(dupe);
|
|
|
|
if not r.is_ref:
|
|
|
|
self.__total_count += 1
|
|
|
|
self.__total_size += r.size
|
|
|
|
if not dupe.is_ref:
|
|
|
|
self.__total_count -= 1
|
|
|
|
self.__total_size -= dupe.size
|
|
|
|
self.__dupes = None
|
2010-08-13 09:37:45 +00:00
|
|
|
self.is_modified = True
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def perform_on_marked(self, func, remove_from_results):
|
2010-04-12 10:21:01 +00:00
|
|
|
# Performs `func` on all marked dupes. If an EnvironmentError is raised during the call,
|
|
|
|
# the problematic dupe is added to self.problems.
|
|
|
|
self.problems = []
|
|
|
|
to_remove = []
|
|
|
|
marked = (dupe for dupe in self.dupes if self.is_marked(dupe))
|
|
|
|
for dupe in marked:
|
|
|
|
try:
|
|
|
|
func(dupe)
|
|
|
|
to_remove.append(dupe)
|
|
|
|
except EnvironmentError as e:
|
2010-08-11 14:39:06 +00:00
|
|
|
self.problems.append((dupe, str(e)))
|
2009-06-01 09:55:11 +00:00
|
|
|
if remove_from_results:
|
|
|
|
self.remove_duplicates(to_remove)
|
|
|
|
self.mark_none()
|
2010-04-12 10:21:01 +00:00
|
|
|
for dupe, _ in self.problems:
|
|
|
|
self.mark(dupe)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def remove_duplicates(self, dupes):
|
|
|
|
'''Remove 'dupes' from their respective group, and remove the group is it ends up empty.
|
|
|
|
'''
|
|
|
|
affected_groups = set()
|
|
|
|
for dupe in dupes:
|
|
|
|
group = self.get_group_of_duplicate(dupe)
|
|
|
|
if dupe not in group.dupes:
|
|
|
|
return
|
|
|
|
group.remove_dupe(dupe, False)
|
|
|
|
self._remove_mark_flag(dupe)
|
|
|
|
self.__total_count -= 1
|
|
|
|
self.__total_size -= dupe.size
|
|
|
|
if not group:
|
|
|
|
self.__groups.remove(group)
|
|
|
|
if self.__filtered_groups:
|
|
|
|
self.__filtered_groups.remove(group)
|
|
|
|
else:
|
|
|
|
affected_groups.add(group)
|
|
|
|
for group in affected_groups:
|
2009-09-05 14:58:35 +00:00
|
|
|
group.discard_matches()
|
2009-06-01 09:55:11 +00:00
|
|
|
self.__dupes = None
|
2011-01-26 10:49:30 +00:00
|
|
|
self.is_modified = bool(self.__groups)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-09-01 14:05:00 +00:00
|
|
|
def save_to_xml(self, outfile):
|
2009-06-01 09:55:11 +00:00
|
|
|
self.apply_filter(None)
|
2010-08-15 12:42:55 +00:00
|
|
|
root = ET.Element('results')
|
2010-03-01 11:21:43 +00:00
|
|
|
# writer = XMLGenerator(outfile, 'utf-8')
|
2009-06-01 09:55:11 +00:00
|
|
|
for g in self.groups:
|
2010-08-15 12:42:55 +00:00
|
|
|
group_elem = ET.SubElement(root, 'group')
|
2009-06-01 09:55:11 +00:00
|
|
|
dupe2index = {}
|
|
|
|
for index, d in enumerate(g):
|
|
|
|
dupe2index[d] = index
|
|
|
|
try:
|
|
|
|
words = engine.unpack_fields(d.words)
|
|
|
|
except AttributeError:
|
|
|
|
words = ()
|
2010-08-15 12:42:55 +00:00
|
|
|
file_elem = ET.SubElement(group_elem, 'file')
|
2010-07-14 07:19:34 +00:00
|
|
|
try:
|
2010-08-11 14:39:06 +00:00
|
|
|
file_elem.set('path', str(d.path))
|
2010-07-14 07:19:34 +00:00
|
|
|
file_elem.set('words', ','.join(words))
|
|
|
|
except ValueError: # If there's an invalid character, just skip the file
|
|
|
|
file_elem.set('path', '')
|
2010-03-01 11:21:43 +00:00
|
|
|
file_elem.set('is_ref', ('y' if d.is_ref else 'n'))
|
|
|
|
file_elem.set('marked', ('y' if self.is_marked(d) else 'n'))
|
2009-06-01 09:55:11 +00:00
|
|
|
for match in g.matches:
|
2010-08-15 12:42:55 +00:00
|
|
|
match_elem = ET.SubElement(group_elem, 'match')
|
2010-08-11 14:39:06 +00:00
|
|
|
match_elem.set('first', str(dupe2index[match.first]))
|
|
|
|
match_elem.set('second', str(dupe2index[match.second]))
|
|
|
|
match_elem.set('percentage', str(int(match.percentage)))
|
2010-08-15 12:42:55 +00:00
|
|
|
tree = ET.ElementTree(root)
|
2010-03-01 11:21:43 +00:00
|
|
|
with FileOrPath(outfile, 'wb') as fp:
|
|
|
|
tree.write(fp, encoding='utf-8')
|
2010-08-13 09:37:45 +00:00
|
|
|
self.is_modified = False
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def sort_dupes(self, key, asc=True, delta=False):
|
|
|
|
if not self.__dupes:
|
|
|
|
self.__get_dupe_list()
|
|
|
|
self.__dupes.sort(key=lambda d: self.data.GetDupeSortKey(d, lambda: self.get_group_of_duplicate(d), key, delta))
|
|
|
|
if not asc:
|
|
|
|
self.__dupes.reverse()
|
|
|
|
self.__dupes_sort_descriptor = (key,asc,delta)
|
|
|
|
|
|
|
|
def sort_groups(self,key,asc=True):
|
|
|
|
self.groups.sort(key=lambda g: self.data.GetGroupSortKey(g, key))
|
|
|
|
if not asc:
|
|
|
|
self.groups.reverse()
|
|
|
|
self.__groups_sort_descriptor = (key,asc)
|
|
|
|
|
|
|
|
#---Properties
|
|
|
|
dupes = property(__get_dupe_list)
|
|
|
|
groups = property(__get_groups, __set_groups)
|
|
|
|
stat_line = property(__get_stat_line)
|