-
Notifications
You must be signed in to change notification settings - Fork 0
/
guidata_wrapper.py
364 lines (278 loc) · 12 KB
/
guidata_wrapper.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import guidata.dataset.datatypes as dt
import guidata.dataset.dataitems as di
import guidata.dataset.qtwidgets as qtw
from textwrap import shorten
from darcs.changes import get_local_changes_only
from darcs.common import Patch
from darcs.whatrevert import whatsnew
from pexpect_helper import SpawnAction, spawn
from config import Config
from collections import defaultdict
from releasetestutil import get_releasetest_issues
import os
try:
from PyQt5.QtWidgets import QMessageBox
except RuntimeError:
from PyQt4.QtGui import QMessageBox
from darcs.common import Prefixes, MyRedmine
class CancelDataset(dt.DataSet):
def edit(self, parent=None, apply=None, size=None):
res = super().edit(parent, apply, size)
if not res:
exit()
return res
def patchChoiceItem(patches):
# if not patches:
# patches = get_local_changes_only(cwd, Config.UPSTREAM_REPO, files, Config.MAX_PATCH_COUNT,
# author=Config.AUTHOR)[:Config.MAX_PATCH_SHOW]
titlecount = defaultdict(int)
for p in patches:
titlecount[p.title] += 1
def strf(p):
if titlecount[p.title] > 1:
return "{} ({}) \n\n".format(p.title, p.date)
return p.title
return di.ChoiceItem("patch", [(p, strf(p)) for p in patches])
def message(title, text, question=False, icon=None):
msg = QMessageBox()
icon = (
QMessageBox.Question if question else QMessageBox.Information) if icon is None else icon
msg.setIcon(icon)
msg.setText(text)
# msg.setInformativeText("This is additional information")
msg.setWindowTitle(title)
# msg.setDetailedText("The details are as follows:")
# msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
buttons = QMessageBox.Ok
if question:
buttons |= QMessageBox.Cancel
msg.setStandardButtons(buttons)
# msg.buttonClicked.connect(msgbtn)
return msg.exec_() == QMessageBox.Ok
def pickfrom(*items, title="Pick an item...", itemtitle="items", multiple=False, strf=str):
do_format = callable(strf)
choices = [strf(i) for i in items] if do_format else list(items)
kwargs = dict(label=itemtitle, choices=choices)
if multiple:
kwargs.update(default=range(len(items)))
widget = di.MultipleChoiceItem(**kwargs).vertical(2)
else:
if do_format:
kwargs.update(default=0)
kwargs.update(radio=True)
widget = di.ChoiceItem(**kwargs)
class Processing(CancelDataset):
selection = widget
p = Processing(title=title)
p.edit()
if not do_format:
return p.selection
if multiple:
return [items[i] for i in p.selection]
return items[int(p.selection)]
def SendPatch(cwd, files):
author = Config.AUTHOR
patches = get_local_changes_only(cwd, Config.UPSTREAM_REPO, files, Config.MAX_PATCH_COUNT,
author=author)[:Config.MAX_PATCH_SHOW]
if not patches:
raise Exception("No unapplied patcehs found by {}.".format(author))
def callback(dataset, item, value, parent):
dataset.output = Config.DIFF_STORE_DIR
class SendPatch(CancelDataset):
patch = patchChoiceItem(patches)
output = di.StringItem("file", default='').set_pos(col=0)
button = di.ButtonItem("default", callback).set_pos(col=1)
return SendPatch("Send Patch...")
def AmendPatch(cwd, files):
patches = get_local_changes_only(cwd, Config.UPSTREAM_REPO, files, Config.MAX_PATCH_COUNT,
author=Config.AUTHOR)[:Config.MAX_PATCH_SHOW]
if not patches:
message("no patches",
"Konnte keine Patches von {} finden, die nicht auch in {} sind.".format(
Config.AUTHOR,
Config.UPSTREAM_REPO))
exit()
filechanges, addfiles = whatsnew(cwd, files)
if addfiles:
files_item = di.MultipleChoiceItem("addfiles", [(f, str(f)) for f in addfiles]).vertical()
else:
files_item = None
class AmendPatch(CancelDataset):
patch = patchChoiceItem(patches)
files = files_item
askdeps = di.BoolItem("ask-deps", default=False)
return AmendPatch("Amend Patch...")
def RecordPatch(*args, **kwargs):
# raise_attr_exception=False
mrm = MyRedmine()
issueChoices = [(0, "None")] + [(i.id, "#{}: {}".format(i.id, i.subject)) for i in
mrm.my_issues]
releaseChoices = [(0, "None")] + [
(int(r['Nr']), "{}: {}".format(r['Nr'], shorten(r['Beschreibung'], 80))) for r in
get_releasetest_issues(assignee=Config.GSPREAD_ASSIGNEE)]
class RecordPatch(CancelDataset):
prefix = di.ChoiceItem('Prefix', [(p, p.name) for p in Prefixes])
tracker = di.ChoiceItem("Tracker", issueChoices, default=0)
rtest = di.ChoiceItem("Releasetest", releaseChoices, default=0)
name = di.StringItem("Name", notempty=True)
author = di.StringItem("Author", notempty=True, default=Config.AUTHOR)
@property
def Prefix(self):
return self.prefix.Name(self.tracker, self.rtest)
@property
def FormattedName(self):
return '{}: {}'.format(self.Prefix, self.name)
def check_prefix(self):
if not self.prefix.hasParameter:
return False
return self.tracker > 0 or self.rtest
def read(self, p: Patch):
prefix, name = p.title.split(': ', 1)
self.name = name
self.prefix, self.tracker, self.rtest = Prefixes.read(prefix)
return RecordPatch(*args, **kwargs)
def QuickEditIssue(*args, **kwargs):
# raise_attr_exception=False
mrm = MyRedmine()
issueChoices = [(i, "#{} ({}: {}%)".format(i, i.status, i.done_ratio)) for i in
mrm.my_issues_filtered(**kwargs)]
statusChoices = [(0, "Dont Change")] + [(s, s.name) for s in mrm.issue_statuses]
releaseChoices = [(int(r['Nr']), "{}: {}".format(r['Nr'], shorten(r['Beschreibung'], 50))) for
r in
get_releasetest_issues()]
if not issueChoices:
message("Nope", "There are no new Issues for you.")
return
class QuickIssue(CancelDataset):
issue = di.ChoiceItem("Issue", issueChoices)
rtest = di.ChoiceItem("Releasetest", releaseChoices)
status = di.ChoiceItem("Status", statusChoices, default=0)
progress = di.IntItem("Progress", min=0, max=100, slider=True, default=0)
comment = di.TextItem("Comment")
return QuickIssue(*args)
def QuickEditIssue2(issue, *args):
mrm = MyRedmine()
stati = [issue.status] + [s for s in mrm.issue_statuses if s.name != issue.status.name]
statusChoices = [(s, s.name) for s in stati]
class QuickIssue(CancelDataset):
status = di.ChoiceItem("Status", statusChoices)
progress = di.IntItem("Progress", min=0, max=100, slider=True, default=issue.done_ratio)
comment = di.TextItem("Comment")
qei = QuickIssue(*args)
qei.edit()
if qei.status:
issue.status_id = qei.status.id
if qei.comment:
issue.notes = qei.comment
save_progress = qei.progress != issue.done_ratio
if qei.progress < issue.done_ratio:
save_progress = message("are you sure?",
"you're changing the progress of the issue from {}% to {}%.".format(
issue.done_ratio,
qei.progress),
True)
if save_progress:
issue.done_ratio = qei.progress
issue.save()
return qei
class GraphOptions(CancelDataset):
"""
Generate a Dependency Graph
"""
limit = di.IntItem("How many Patches?", default=20, min=1)
filename = di.StringItem("file", notempty=True, default='/tmp/depgraph')
format = di.ChoiceItem('format', [('pdf', 'pdf'), ('svg', 'svg'), ('png', 'png')])
just_mine = di.BoolItem('only mine', help='show only my patches', default=False)
open = di.BoolItem('open', help='gnome-open file when done if checked', default=True)
color = di.ColorItem("My Local Patches", default="#00AA00")
acolor = di.ColorItem("My Applied Patches", default="#0000DD")
bcolor = di.ColorItem("Other Applied Patches", default="#999999")
cleanup = di.BoolItem('cleanup', help='cleanup the dot source file if checked', default=True)
# author = di.StringItem("Author", notempty=True, default=Config.AUTHOR)
class SaveDiff(dt.DataSet):
"""
Save selected Diff?
"""
name = di.FileSaveItem("name", basedir=Config.DIFF_STORE_DIR,
default=os.path.join(Config.DIFF_STORE_DIR, 'local diff'))
# di.StringItem("name", notempty=True, default='local diff')
def getint(title="How many...", itemlabel="items", default=None, min=None, max=None, nonzero=None,
unit='', even=None, slider=False, help='', check=True):
class Getint(CancelDataset):
selection = di.IntItem(itemlabel, default=default, min=min, max=max, nonzero=nonzero,
unit=unit, even=even,
slider=slider, help=help, check=check)
p = Getint(title=title)
p.edit()
return p.selection
class Ask(SpawnAction, str):
def spawnAction(self, sp: spawn):
_, m, *_ = yield self
msg = m.group() + '\n' + self
message('Continue?', msg, True)
class NewFileItem(di.FileSaveItem):
def check_value(self, value):
return super().check_value(value) and not os.path.exists(value)
class DictItem(di.ButtonItem):
"""
Construct a dictionary data item
* label [string]: name
* default [dict]: default value (optional)
* help [string]: text shown in tooltip (optional)
* check [bool]: if False, value is not checked (optional, default=True)
"""
def __init__(self, label, default=None, help='', check=True):
def dictedit(instance, item, value, parent):
try:
# Spyder 3
from spyder.widgets.variableexplorer \
import collectionseditor
Editor = collectionseditor.CollectionsEditor
except ImportError:
# Spyder 2
from spyderlib.widgets import dicteditor
Editor = dicteditor.DictEditor
editor = Editor(parent)
value_was_none = value is None
if value_was_none:
value = {}
editor.setup(value)
if editor.exec_():
return editor.get_value()
else:
if value_was_none:
return
return value
di.ButtonItem.__init__(self, label, dictedit, icon='dictedit.png',
default=default, help=help, check=check)
qtw.DataSetEditLayout.register(DictItem, qtw.ButtonWidget)
def AutoGui(object, grouping=None, title="Preferences", changed_hook=None):
class ObjectEditor(dt.DataSet):
def edit(self, parent=None, apply=None, size=None):
res = super().edit(parent, apply, size)
changed = False
if res:
for ds in self._items:
k = ds._name
v = getattr(self, ds._name)
if getattr(object, k) != v:
print("updated {}: {} ({})".format(ds._name, v, type(v)))
changed = True
setattr(object, k, v)
if changed and callable(changed_hook):
changed_hook()
return res
members = {}
for k, v in object.__dict__.items():
if k[0] == '_':
continue
elif isinstance(v, int):
members[k] = di.IntItem(k, v)
elif isinstance(v, bool):
members[k] = di.BoolItem(k, v)
elif isinstance(v, str):
members[k] = di.StringItem(k, v)
elif isinstance(v, dict):
members[k] = DictItem(k, v)
AutoGui = type("AutoGui", (ObjectEditor,), members)
return AutoGui(title)