File: exclude_list_table.py

package info (click to toggle)
dupeguru 4.3.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,604 kB
  • sloc: python: 16,846; ansic: 424; makefile: 123
file content (96 lines) | stat: -rw-r--r-- 3,032 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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 core.gui.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("Regular Expressions"))]

    def __init__(self, exclude_list_dialog, app):
        GUITable.__init__(self)
        DupeGuruGUIObject.__init__(self, app)
        self._columns = Columns(self)
        self.dialog = exclude_list_dialog

    def rename_selected(self, newname):
        row = self.selected_row
        if row is 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.dialog.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.view.refresh()

    def _fill(self):
        for enabled, regex in self.dialog.exclude_list:
            self.append(ExcludeListRow(self, enabled, regex))

    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()
        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 = str(enabled)
        self.regex = str(regex)
        self.highlight = False

    @property
    def data(self):
        if self._data is None:
            self._data = {"marked": self.enabled, "regex": self.regex}
        return self._data

    @property
    def markable(self):
        return self._app.exclude_list.is_markable(self.regex)

    @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 error(self):
        # This assumes error() returns an Exception()
        message = self._app.exclude_list.error(self.regex)
        if hasattr(message, "msg"):
            return self._app.exclude_list.error(self.regex).msg
        else:
            return message  # Exception object