-
Notifications
You must be signed in to change notification settings - Fork 4
/
key_metadata_model.py
291 lines (233 loc) · 8.02 KB
/
key_metadata_model.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Git-Annex-Metadata-Gui
# Copyright (C) 2017 Alper Nebi Yasak
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import bisect
import logging
from PyQt5 import Qt
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from .utils import parse_as_set
from .utils import AutoConsumed
logger = logging.getLogger(__name__)
class AnnexedKeyItem(QtGui.QStandardItem):
def __init__(self, key_obj):
super().__init__()
self._obj = key_obj
self.setText(self.key)
self.setToolTip(self.key)
font = QtGui.QFontDatabase.FixedFont
font = QtGui.QFontDatabase().systemFont(font)
self.setFont(font)
icon = QtWidgets.QFileIconProvider.File
icon = QtWidgets.QFileIconProvider().icon(icon)
self.setIcon(icon)
self.setSelectable(True)
self.setEditable(False)
self.setEnabled(True)
self.setFlags(self.flags() | Qt.Qt.ItemNeverHasChildren)
@property
def metadata(self):
return self._obj.metadata
@property
def key(self):
return self._obj.key
@property
def contentlocation(self):
return self._obj.contentlocation
def type(self):
return QtGui.QStandardItem.UserType + 1
def __lt__(self, other):
if other is None:
return True
elif isinstance(other, AnnexedKeyItem):
lhs = self.data(role=Qt.Qt.DisplayRole)
rhs = other.data(role=Qt.Qt.DisplayRole)
lhs_pre, _, lhs_name = lhs.partition('--')
lhs_backend, *lhs_fields = lhs_pre.split('-')
lhs_fields = [(f[0], int(f[1:])) for f in lhs_fields]
lhs = (lhs_backend, *lhs_fields, lhs_name)
rhs_pre, _, rhs_name = rhs.partition('--')
rhs_backend, *rhs_fields = rhs_pre.split('-')
rhs_fields = [(f[0], int(f[1:])) for f in rhs_fields]
rhs = (rhs_backend, *rhs_fields, rhs_name)
try:
return lhs > rhs
except TypeError:
return super().__lt__(other)
else:
return NotImplemented
def __repr__(self):
return "{name}.{cls}({args})".format(
name=__name__,
cls=self.__class__.__name__,
args=self._obj.key,
)
class AnnexedFieldItem(QtGui.QStandardItem):
def __init__(self, key_item, field):
super().__init__()
self._item = key_item
self._field = field
self.setSelectable(True)
self.setEditable(True)
self.setEnabled(True)
self.setFlags(self.flags() | Qt.Qt.ItemNeverHasChildren)
@property
def key(self):
return self._item.key
@property
def contentlocation(self):
return self._item.contentlocation
@property
def metadata(self):
return self._item.metadata.get(self._field, set())
@metadata.setter
def metadata(self, value):
self._item.metadata[self._field] = value
self.emitDataChanged()
def type(self):
return QtGui.QStandardItem.UserType + 2
def data(self, role=Qt.Qt.DisplayRole):
if role == Qt.Qt.DisplayRole:
data = self.metadata
if len(data) == 0:
return None
if len(data) == 1:
return data.pop()
else:
return "<{n} values>".format(n=len(data))
elif role == Qt.Qt.EditRole:
data = self.metadata
if data:
return str(data)
elif role == Qt.Qt.ToolTipRole:
data = self.metadata
if data:
return str(data)
elif role == Qt.Qt.UserRole:
return self.metadata
else:
return super().data(role=role)
def setData(self, value, role=Qt.Qt.EditRole):
if role == Qt.Qt.DisplayRole:
return False
elif role == Qt.Qt.EditRole:
try:
self.metadata = parse_as_set(value)
except:
fmt = "Cannot parse '{}' as a set object."
msg = fmt.format(value)
logger.error(msg)
return
elif role == Qt.Qt.UserRole:
try:
self.metadata = value
except:
fmt = "Cannot parse '{}' as a set object."
msg = fmt.format(value)
logger.error(msg)
return
else:
super().setData(value, role=role)
def __lt__(self, other):
if other is None:
return True
elif isinstance(other, AnnexedFieldItem):
lhs = self.data(role=Qt.Qt.UserRole)
rhs = other.data(role=Qt.Qt.UserRole)
if len(lhs) == 0:
return False
elif len(rhs) == 0:
return True
elif len(lhs) == len(rhs) == 1:
lhs_, rhs_ = lhs.pop(), rhs.pop()
try:
return int(lhs_) > int(rhs_)
except ValueError:
return super().__lt__(other)
else:
return len(lhs) > len(rhs)
else:
return NotImplemented
def __repr__(self):
return "{name}.{cls}({args})".format(
name=__name__,
cls=self.__class__.__name__,
args={
'item': self._item,
'field': self._field,
}
)
class AnnexedKeyMetadataModel(QtGui.QStandardItemModel):
key_inserted = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.repo = None
def setRepo(self, repo):
if self._populate.running():
self._populate.stop()
msg = "Aborted loading previous key model."
logger.info(msg)
self.repo = repo
self.fields = ['Git-Annex Key']
self.key_items = {}
self._pending = iter(self.repo.annex.values())
self.clear()
self.setHorizontalHeaderLabels(self.fields)
self._populate.start()
@QtCore.pyqtSlot()
@AutoConsumed
def _populate(self):
msg = "Loading key model..."
logger.info(msg)
for obj in self._pending:
self.insert_key(obj)
yield
msg = "Key model fully loaded."
logger.info(msg)
def insert_key(self, key_obj):
key_item = AnnexedKeyItem(key_obj)
field_items = (
AnnexedFieldItem(key_obj, field)
for field in self.fields[1:]
)
self.appendRow([key_item, *field_items])
self.key_items[key_item.key] = key_item
new_fields = set(key_item.metadata) - set(self.fields)
for field in new_fields:
QtCore.QMetaObject.invokeMethod(
self, 'insert_field',
Qt.Qt.QueuedConnection,
QtCore.Q_ARG(str, field)
)
self.key_inserted.emit(key_obj.key)
@QtCore.pyqtSlot(str)
def insert_field(self, field):
if field in self.fields:
return
col = bisect.bisect(self.fields, field, lo=1)
items = [
AnnexedFieldItem(self.item(row, 0), field)
for row in range(self.rowCount())
]
self.fields.insert(col, field)
self.insertColumn(col, items)
self.setHorizontalHeaderLabels(self.fields)
def __repr__(self):
return "{name}.{cls}({args})".format(
name=__name__,
cls=self.__class__.__name__,
args=self.repo,
)