mirror of
https://github.com/arsenetar/dupeguru.git
synced 2026-01-22 14:41:39 +00:00
Implement dialog and base classes for model/view
This commit is contained in:
@@ -26,11 +26,13 @@ from .pe.photo import get_delta_dimensions
|
||||
from .util import cmp_value, fix_surrogate_encoding
|
||||
from . import directories, results, export, fs, prioritize
|
||||
from .ignore import IgnoreList
|
||||
from .exclude import ExcludeList
|
||||
from .scanner import ScanType
|
||||
from .gui.deletion_options import DeletionOptions
|
||||
from .gui.details_panel import DetailsPanel
|
||||
from .gui.directory_tree import DirectoryTree
|
||||
from .gui.ignore_list_dialog import IgnoreListDialog
|
||||
from .gui.exclude_list_dialog import ExcludeListDialogCore
|
||||
from .gui.problem_dialog import ProblemDialog
|
||||
from .gui.stats_label import StatsLabel
|
||||
|
||||
@@ -140,6 +142,7 @@ class DupeGuru(Broadcaster):
|
||||
self.directories = directories.Directories()
|
||||
self.results = results.Results(self)
|
||||
self.ignore_list = IgnoreList()
|
||||
self.exclude_list = ExcludeList(self)
|
||||
# In addition to "app-level" options, this dictionary also holds options that will be
|
||||
# sent to the scanner. They don't have default values because those defaults values are
|
||||
# defined in the scanner class.
|
||||
@@ -155,6 +158,7 @@ class DupeGuru(Broadcaster):
|
||||
self.directory_tree = DirectoryTree(self)
|
||||
self.problem_dialog = ProblemDialog(self)
|
||||
self.ignore_list_dialog = IgnoreListDialog(self)
|
||||
self.exclude_list_dialog = ExcludeListDialogCore(self)
|
||||
self.stats_label = StatsLabel(self)
|
||||
self.result_table = None
|
||||
self.deletion_options = DeletionOptions()
|
||||
@@ -587,6 +591,9 @@ class DupeGuru(Broadcaster):
|
||||
p = op.join(self.appdata, "ignore_list.xml")
|
||||
self.ignore_list.load_from_xml(p)
|
||||
self.ignore_list_dialog.refresh()
|
||||
p = op.join(self.appdata, "exclude_list.xml")
|
||||
self.exclude_list.load_from_xml(p)
|
||||
self.exclude_list_dialog.refresh()
|
||||
|
||||
def load_from(self, filename):
|
||||
"""Start an async job to load results from ``filename``.
|
||||
@@ -773,6 +780,8 @@ class DupeGuru(Broadcaster):
|
||||
self.directories.save_to_file(op.join(self.appdata, "last_directories.xml"))
|
||||
p = op.join(self.appdata, "ignore_list.xml")
|
||||
self.ignore_list.save_to_xml(p)
|
||||
p = op.join(self.appdata, "exclude_list.xml")
|
||||
self.exclude_list.save_to_xml(p)
|
||||
self.notify("save_session")
|
||||
|
||||
def save_as(self, filename):
|
||||
|
||||
97
core/exclude.py
Normal file
97
core/exclude.py
Normal file
@@ -0,0 +1,97 @@
|
||||
# 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 .markable import Markable
|
||||
from xml.etree import ElementTree as ET
|
||||
from hscommon.util import FileOrPath
|
||||
|
||||
|
||||
class ExcludeList(Markable):
|
||||
"""Exclude list of regular expression strings to filter out directories
|
||||
and files that we want to avoid scanning."""
|
||||
|
||||
# ---Override
|
||||
def __init__(self, app):
|
||||
Markable.__init__(self)
|
||||
self.app = app
|
||||
self._excluded = [] # set of strings
|
||||
self._count = 0
|
||||
|
||||
def __iter__(self):
|
||||
for regex in self._excluded:
|
||||
yield self.is_marked(regex), regex
|
||||
|
||||
def __len__(self):
|
||||
return self._count
|
||||
|
||||
def _is_markable(self, row):
|
||||
return True
|
||||
|
||||
# ---Public
|
||||
def add(self, regex):
|
||||
self._excluded.insert(0, regex)
|
||||
self._count = len(self._excluded)
|
||||
|
||||
def isExcluded(self, regex):
|
||||
if regex in self._excluded:
|
||||
return True
|
||||
return False
|
||||
|
||||
def clear(self):
|
||||
self._excluded = []
|
||||
self._count = 0
|
||||
|
||||
def remove(self, regex):
|
||||
return self._excluded.remove(regex)
|
||||
|
||||
def rename(self, regex, newregex):
|
||||
if regex not in self._excluded:
|
||||
return
|
||||
marked = self.is_marked(regex)
|
||||
index = self._excluded.index(regex)
|
||||
self._excluded[index] = newregex
|
||||
if marked:
|
||||
# Not marked by default when added
|
||||
self.mark(self._excluded[index])
|
||||
|
||||
def change_index(self, regex, new_index):
|
||||
item = self._excluded.pop(regex)
|
||||
self._excluded.insert(new_index, item)
|
||||
|
||||
def load_from_xml(self, infile):
|
||||
"""Loads the ignore list from a XML created with save_to_xml.
|
||||
|
||||
infile can be a file object or a filename.
|
||||
"""
|
||||
try:
|
||||
root = ET.parse(infile).getroot()
|
||||
except Exception as e:
|
||||
print(f"Error while loading {infile}: {e}")
|
||||
return
|
||||
marked = set()
|
||||
exclude_elems = (e for e in root if e.tag == "exclude")
|
||||
for exclude_item in exclude_elems:
|
||||
regex_string = exclude_item.get("regex")
|
||||
if not regex_string:
|
||||
continue
|
||||
self.add(regex_string)
|
||||
if exclude_item.get("marked") == "y":
|
||||
marked.add(regex_string)
|
||||
for item in marked:
|
||||
# this adds item to the Markable "marked" set
|
||||
self.mark(item)
|
||||
|
||||
def save_to_xml(self, outfile):
|
||||
"""Create a XML file that can be used by load_from_xml.
|
||||
|
||||
outfile can be a file object or a filename.
|
||||
"""
|
||||
root = ET.Element("exclude_list")
|
||||
for regex in self._excluded:
|
||||
exclude_node = ET.SubElement(root, "exclude")
|
||||
exclude_node.set("regex", str(regex))
|
||||
exclude_node.set("marked", ("y" if self.is_marked(regex) else "n"))
|
||||
tree = ET.ElementTree(root)
|
||||
with FileOrPath(outfile, "wb") as fp:
|
||||
tree.write(fp, encoding="utf-8")
|
||||
64
core/gui/exclude_list_dialog.py
Normal file
64
core/gui/exclude_list_dialog.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# Created On: 2012/03/13
|
||||
# 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
|
||||
# http://www.gnu.org/licenses/gpl-3.0.html
|
||||
|
||||
# from hscommon.trans import tr
|
||||
from .exclude_list_table import ExcludeListTable
|
||||
|
||||
default_regexes = [".*thumbs", "\.DS.Store", "\.Trash", "Trash-Bin"]
|
||||
|
||||
|
||||
class ExcludeListDialogCore:
|
||||
# --- View interface
|
||||
# show()
|
||||
#
|
||||
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
self.exclude_list = self.app.exclude_list # Markable from exclude.py
|
||||
self.exclude_list_table = ExcludeListTable(self, app) # GUITable, this is the "model"
|
||||
|
||||
def restore_defaults(self):
|
||||
for _, regex in self.exclude_list:
|
||||
if regex not in default_regexes:
|
||||
self.exclude_list.unmark(regex)
|
||||
for default_regex in default_regexes:
|
||||
if not self.exclude_list.isExcluded(default_regex):
|
||||
self.exclude_list.add(default_regex)
|
||||
self.exclude_list.mark(default_regex)
|
||||
self.refresh()
|
||||
|
||||
def refresh(self):
|
||||
self.exclude_list_table.refresh()
|
||||
|
||||
def remove_selected(self):
|
||||
for row in self.exclude_list_table.selected_rows:
|
||||
self.exclude_list_table.remove(row)
|
||||
self.exclude_list.remove(row.regex)
|
||||
self.refresh()
|
||||
|
||||
def rename_selected(self, newregex):
|
||||
"""Renames the selected regex to ``newregex``.
|
||||
If there's more than one selected row, the first one is used.
|
||||
:param str newregex: The regex to rename the row's regex to.
|
||||
"""
|
||||
try:
|
||||
r = self.exclude_list_table.selected_rows[0]
|
||||
self.exclude_list.rename(r.regex, newregex)
|
||||
self.refresh()
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"dupeGuru Warning: {e}")
|
||||
return False
|
||||
|
||||
def add(self, regex):
|
||||
self.exclude_list.add(regex)
|
||||
self.exclude_list.mark(regex)
|
||||
# TODO make checks here before adding to GUI
|
||||
self.exclude_list_table.add(regex)
|
||||
|
||||
def show(self):
|
||||
self.view.show()
|
||||
117
core/gui/exclude_list_table.py
Normal file
117
core/gui/exclude_list_table.py
Normal file
@@ -0,0 +1,117 @@
|
||||
# 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 .base import DupeGuruGUIObject
|
||||
from hscommon.gui.table import GUITable, Row
|
||||
from hscommon.gui.column import Column, Columns
|
||||
from hscommon.trans import trget
|
||||
tr = trget("ui")
|
||||
|
||||
|
||||
class ExcludeListTable(GUITable, DupeGuruGUIObject):
|
||||
COLUMNS = [
|
||||
Column("marked", ""),
|
||||
Column("regex", tr("Regex"))
|
||||
]
|
||||
|
||||
def __init__(self, exclude_list_dialog, app):
|
||||
GUITable.__init__(self)
|
||||
DupeGuruGUIObject.__init__(self, app)
|
||||
# self.columns = Columns(self, prefaccess=app, savename="ExcludeTable")
|
||||
self.columns = Columns(self)
|
||||
self.dialog = exclude_list_dialog
|
||||
|
||||
def rename_selected(self, newname):
|
||||
row = self.selected_row
|
||||
if row is None:
|
||||
# There's all kinds of way the current row can be swept off during rename. When it
|
||||
# happens, selected_row will be None.
|
||||
return False
|
||||
row._data = None
|
||||
return self.dialog.rename_selected(newname)
|
||||
|
||||
# --- Virtual
|
||||
def _do_add(self, regex):
|
||||
"""(Virtual) Creates a new row, adds it in the table.
|
||||
Returns ``(row, insert_index)``.
|
||||
"""
|
||||
# Return index 0 to insert at the top
|
||||
return ExcludeListRow(self, self.dialog.exclude_list.is_marked(regex), regex), 0
|
||||
|
||||
def _do_delete(self):
|
||||
self.dalog.exclude_list.remove(self.selected_row.regex)
|
||||
|
||||
# --- Override
|
||||
def add(self, regex):
|
||||
row, insert_index = self._do_add(regex)
|
||||
self.insert(insert_index, row)
|
||||
# self.select([insert_index])
|
||||
self.view.refresh()
|
||||
|
||||
def _fill(self):
|
||||
for enabled, regex in self.dialog.exclude_list:
|
||||
self.append(ExcludeListRow(self, enabled, regex))
|
||||
|
||||
# def remove(self):
|
||||
# super().remove(super().selected_rows)
|
||||
|
||||
# def _update_selection(self):
|
||||
# # rows = self.selected_rows
|
||||
# # self.dialog._select_rows(list(map(attrgetter("_dupe"), rows)))
|
||||
# self.dialog.remove_selected()
|
||||
|
||||
def refresh(self, refresh_view=True):
|
||||
"""Override to avoid keeping previous selection in case of multiple rows
|
||||
selected previously."""
|
||||
self.cancel_edits()
|
||||
del self[:]
|
||||
self._fill()
|
||||
# sd = self._sort_descriptor
|
||||
# if sd is not None:
|
||||
# super().sort_by(self, column_name=sd.column, desc=sd.desc)
|
||||
if refresh_view:
|
||||
self.view.refresh()
|
||||
|
||||
|
||||
class ExcludeListRow(Row):
|
||||
def __init__(self, table, enabled, regex):
|
||||
Row.__init__(self, table)
|
||||
self._app = table.app
|
||||
self._data = None
|
||||
self.enabled_original = enabled
|
||||
self.regex_original = regex
|
||||
self.enabled = str(enabled)
|
||||
self.regex = str(regex)
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
def get_display_info(row):
|
||||
return {"marked": row.enabled, "regex": row.regex}
|
||||
|
||||
if self._data is None:
|
||||
self._data = get_display_info(self)
|
||||
return self._data
|
||||
|
||||
@property
|
||||
def markable(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def marked(self):
|
||||
return self._app.exclude_list.is_marked(self.regex)
|
||||
|
||||
@marked.setter
|
||||
def marked(self, value):
|
||||
if value:
|
||||
self._app.exclude_list.mark(self.regex)
|
||||
else:
|
||||
self._app.exclude_list.unmark(self.regex)
|
||||
|
||||
# @property
|
||||
# def regex(self):
|
||||
# return self.regex
|
||||
|
||||
# @regex.setter
|
||||
# def regex(self, value):
|
||||
# self._app.exclude_list.add(self._regex, value)
|
||||
@@ -17,7 +17,7 @@ class IgnoreListDialog:
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
self.ignore_list = self.app.ignore_list
|
||||
self.ignore_list_table = IgnoreListTable(self)
|
||||
self.ignore_list_table = IgnoreListTable(self) # GUITable
|
||||
|
||||
def clear(self):
|
||||
if not self.ignore_list:
|
||||
|
||||
Reference in New Issue
Block a user