From da29ef8d6ae6848a8c0c0df277a2da0de337b532 Mon Sep 17 00:00:00 2001 From: toonn Date: Mon, 23 Dec 2019 23:57:00 +0100 Subject: Add unique filter to filter_stack Extracted helper function to `group_by_hash` for both duplicate and unique filters. --- ranger/core/filter_stack.py | 83 +++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/ranger/core/filter_stack.py b/ranger/core/filter_stack.py index a479b6ed..ca8810e8 100644 --- a/ranger/core/filter_stack.py +++ b/ranger/core/filter_stack.py @@ -98,10 +98,42 @@ class HashFilter(BaseFilter, FileManagerAware): return "".format(self.filepath) +def group_by_hash(fsobjects): + hashes = {} + for fobj in fsobjects: + chunks = hash_chunks(fobj.path) + chunk = next(chunks) + while chunk in hashes: + for dup in hashes[chunk]: + _, dup_chunks = dup + try: + hashes[next(dup_chunks)] = [dup] + hashes[chunk].remove(dup) + except StopIteration: + pass + try: + chunk = next(chunks) + except StopIteration: + hashes[chunk].append((fobj, chunks)) + break + else: + hashes[chunk] = [(fobj, chunks)] + + groups = [] + for dups in hashes.values(): + group = [] + for (dup, _) in dups: + group.append(dup) + if group: + groups.append(group) + + return groups + + @stack_filter("duplicate") class DuplicateFilter(BaseFilter, FileManagerAware): def __init__(self, _): - self.duplicates = self.get_duplicates(self.fm.thisdir.files_all) + self.duplicates = self.get_duplicates() def __call__(self, fobj): return fobj in self.duplicates @@ -109,36 +141,35 @@ class DuplicateFilter(BaseFilter, FileManagerAware): def __str__(self): return "" - def get_duplicates(self, fsobjects): - hashes = {} - for fobj in fsobjects: - chunks = hash_chunks(fobj.path) - chunk = next(chunks) - while chunk in hashes: - for dup in hashes[chunk]: - _, dup_chunks = dup - try: - hashes[next(dup_chunks)] = [dup] - hashes[chunk].remove(dup) - except StopIteration: - pass - try: - chunk = next(chunks) - except StopIteration: - hashes[chunk].append((fobj, chunks)) - break - else: - hashes[chunk] = [(fobj, chunks)] - + def get_duplicates(self): duplicates = set() - for dups in hashes.values(): + for dups in group_by_hash(self.fm.thisdir.files_all): if len(dups) >= 2: - for (dup, _) in dups: - duplicates.add(dup) - + duplicates.update(dups) return duplicates +@stack_filter("unique") +class UniqueFilter(BaseFilter, FileManagerAware): + def __init__(self, _): + self.unique = self.get_unique() + + def __call__(self, fobj): + return fobj in self.unique + + def __str__(self): + return "" + + def get_unique(self): + unique = set() + for dups in group_by_hash(self.fm.thisdir.files_all): + try: + unique.add(min(dups, key=lambda fobj: fobj.stat.st_ctime)) + except ValueError: + pass + return unique + + @stack_filter("type") class TypeFilter(BaseFilter): type_to_function = { -- cgit 1.4.1-2-gfad0