Kiln » TortoiseHg » TortoiseHg
Clone URL:  
webconf.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
# webconf.py - Widget to show/edit hgweb config # # Copyright 2010 Yuya Nishihara <yuya@tcha.org> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2, incorporated herein by reference. import os from PyQt4.QtCore import * from PyQt4.QtGui import * from tortoisehg.util import hglib, wconfig from tortoisehg.hgqt import qtlib from tortoisehg.hgqt.i18n import _ from tortoisehg.hgqt.webconf_ui import Ui_WebconfForm _FILE_FILTER = _('Config files (*.conf *.config *.ini);;Any files (*)') class WebconfForm(QWidget): """Widget to show/edit webconf""" def __init__(self, parent=None, webconf=None): super(WebconfForm, self).__init__(parent, acceptDrops=True) self._qui = Ui_WebconfForm() self._qui.setupUi(self) self._initicons() self._qui.path_edit.currentIndexChanged.connect(self._updateview) self._qui.path_edit.currentIndexChanged.connect(self._updateform) self._qui.add_button.clicked.connect(self._addpathmap) self.setwebconf(webconf or wconfig.config()) self._updateform() def _initicons(self): def setstdicon(w, name): w.setIcon(self.style().standardIcon(name)) setstdicon(self._qui.open_button, QStyle.SP_DialogOpenButton) setstdicon(self._qui.save_button, QStyle.SP_DialogSaveButton) self._qui.add_button.setIcon(qtlib.geticon('fileadd')) self._qui.edit_button.setIcon(qtlib.geticon('edit-file')) self._qui.remove_button.setIcon(qtlib.geticon('filedelete')) def dragEnterEvent(self, event): if self._getlocalpath_from_dropevent(event): event.setDropAction(Qt.LinkAction) event.accept() def dropEvent(self, event): localpath = self._getlocalpath_from_dropevent(event) if localpath: event.setDropAction(Qt.LinkAction) event.accept() self._addpathmap(localpath=localpath) @staticmethod def _getlocalpath_from_dropevent(event): m = event.mimeData() if m.hasFormat('text/uri-list') and len(m.urls()) == 1: return unicode(m.urls()[0].toLocalFile()) def setwebconf(self, webconf): """set current webconf object""" path = hglib.tounicode(getattr(webconf, 'path', None) or '') i = self._qui.path_edit.findText(path) if i < 0: i = 0 self._qui.path_edit.insertItem(i, path, webconf) self._qui.path_edit.setCurrentIndex(i) @property def webconf(self): """current webconf object""" def curconf(w): i = w.currentIndex() _path, conf = unicode(w.itemText(i)), w.itemData(i).toPyObject() return conf return curconf(self._qui.path_edit) @property def _webconfmodel(self): """current model object of webconf""" return self._qui.repos_view.model() @pyqtSlot() def _updateview(self): m = WebconfModel(config=self.webconf, parent=self) self._qui.repos_view.setModel(m) self._qui.repos_view.selectionModel().currentChanged.connect( self._updateform) def _updateform(self): """Update availability of each widget""" self._qui.repos_view.setEnabled(hasattr(self.webconf, 'write')) self._qui.add_button.setEnabled(hasattr(self.webconf, 'write')) self._qui.edit_button.setEnabled( hasattr(self.webconf, 'write') and self._qui.repos_view.currentIndex().isValid()) self._qui.remove_button.setEnabled( hasattr(self.webconf, 'write') and self._qui.repos_view.currentIndex().isValid()) @pyqtSlot() def on_open_button_clicked(self): path = QFileDialog.getOpenFileName( self, _('Open hgweb config'), getattr(self.webconf, 'path', None) or '', _FILE_FILTER) if path: self.openwebconf(path) def openwebconf(self, path): """load the specified webconf file""" path = hglib.fromunicode(path) c = wconfig.readfile(path) c.path = os.path.abspath(path) self.setwebconf(c) @pyqtSlot() def on_save_button_clicked(self): path = QFileDialog.getSaveFileName( self, _('Save hgweb config'), getattr(self.webconf, 'path', None) or '', _FILE_FILTER) if path: self.savewebconf(path) def savewebconf(self, path): """save current webconf to the specified file""" path = hglib.fromunicode(path) wconfig.writefile(self.webconf, path) self.openwebconf(path) # reopen in case file path changed @pyqtSlot() def _addpathmap(self, path=None, localpath=None): path, localpath = _PathDialog.getaddpathmap( self, path=path, localpath=localpath, invalidpaths=self._webconfmodel.paths) if path: self._webconfmodel.addpathmap(path, localpath) @pyqtSlot() def on_edit_button_clicked(self): self.on_repos_view_doubleClicked(self._qui.repos_view.currentIndex()) @pyqtSlot(QModelIndex) def on_repos_view_doubleClicked(self, index): assert index.isValid() origpath, origlocalpath = self._webconfmodel.getpathmapat(index.row()) path, localpath = _PathDialog.geteditpathmap( self, path=origpath, localpath=origlocalpath, invalidpaths=set(self._webconfmodel.paths) - set([origpath])) if not path: return if path != origpath: # we cannot change config key without reordering self._webconfmodel.removepathmap(origpath) self._webconfmodel.addpathmap(path, localpath) else: self._webconfmodel.setpathmap(path, localpath) @pyqtSlot() def on_remove_button_clicked(self): index = self._qui.repos_view.currentIndex() assert index.isValid() path, _localpath = self._webconfmodel.getpathmapat(index.row()) self._webconfmodel.removepathmap(path) class _PathDialog(QDialog): """Dialog to add/edit path mapping""" def __init__(self, title, acceptlabel, path=None, localpath=None, invalidpaths=None, parent=None): super(_PathDialog, self).__init__(parent) self.setWindowFlags((self.windowFlags() | Qt.WindowMinimizeButtonHint) & ~Qt.WindowContextHelpButtonHint) self.resize(QFontMetrics(self.font()).width('M') * 50, self.height()) self.setWindowTitle(title) self._invalidpaths = set(invalidpaths or []) self.setLayout(QFormLayout(fieldGrowthPolicy=QFormLayout.ExpandingFieldsGrow)) self._initfields() self._initbuttons(acceptlabel) self._path_edit.setText(path or os.path.basename(localpath or '')) self._localpath_edit.setText(localpath or '') self._updateform() def _initfields(self): """initialize input fields""" def addfield(key, label, *extras): edit = QLineEdit(self) edit.textChanged.connect(self._updateform) if extras: field = QHBoxLayout() field.addWidget(edit) for e in extras: field.addWidget(e) else: field = edit self.layout().addRow(label, field) setattr(self, '_%s_edit' % key, edit) addfield('path', _('Path:')) self._localpath_browse_button = QToolButton( icon=self.style().standardIcon(QStyle.SP_DialogOpenButton)) addfield('localpath', _('Local Path:'), self._localpath_browse_button) self._localpath_browse_button.clicked.connect(self._browse_localpath) def _initbuttons(self, acceptlabel): """initialize dialog buttons""" self._buttons = QDialogButtonBox(self) self._accept_button = self._buttons.addButton(QDialogButtonBox.Ok) self._reject_button = self._buttons.addButton(QDialogButtonBox.Cancel) self._accept_button.setText(acceptlabel) self._buttons.accepted.connect(self.accept) self._buttons.rejected.connect(self.reject) self.layout().addRow(self._buttons) @property def path(self): """value of path field""" return unicode(self._path_edit.text()) @property def localpath(self): """value of localpath field""" return unicode(self._localpath_edit.text()) @pyqtSlot() def _browse_localpath(self): path = QFileDialog.getExistingDirectory(self, _('Select Repository'), self.localpath) if not path: return self._localpath_edit.setText(path) if not self.path: self._path_edit.setText(os.path.basename(unicode(path))) @pyqtSlot() def _updateform(self): """update availability of form elements""" self._accept_button.setEnabled(self._isacceptable()) def _isacceptable(self): return bool(self.path and self.localpath and self.path not in self._invalidpaths) @classmethod def getaddpathmap(cls, parent, path=None, localpath=None, invalidpaths=None): d = cls(title=_('Add Path to Serve'), acceptlabel=_('Add'), path=path, localpath=localpath, invalidpaths=invalidpaths, parent=parent) if d.exec_(): return d.path, d.localpath else: return None, None @classmethod def geteditpathmap(cls, parent, path=None, localpath=None, invalidpaths=None): d = cls(title=_('Edit Path to Serve'), acceptlabel=_('Edit'), path=path, localpath=localpath, invalidpaths=invalidpaths, parent=parent) if d.exec_(): return d.path, d.localpath else: return None, None class WebconfModel(QAbstractTableModel): """Wrapper for webconf object to be a Qt's model object""" _COLUMNS = [(_('Path'),), (_('Local Path'),)] def __init__(self, config, parent=None): super(WebconfModel, self).__init__(parent) self._config = config def data(self, index, role): if not index.isValid(): return None if role == Qt.DisplayRole: v = self._config.items('paths')[index.row()][index.column()] return hglib.tounicode(v) return None def rowCount(self, parent=QModelIndex()): if parent.isValid(): return 0 # no child return len(self._config['paths']) def columnCount(self, parent=QModelIndex()): if parent.isValid(): return 0 # no child return len(self._COLUMNS) def headerData(self, section, orientation, role): if role != Qt.DisplayRole or orientation != Qt.Horizontal: return None return self._COLUMNS[section][0] @property def paths(self): """return list of known paths""" return [hglib.tounicode(e) for e in self._config['paths']] def getpathmapat(self, row): """return pair of (path, localpath) at the specified index""" assert 0 <= row and row < self.rowCount() return tuple(hglib.tounicode(e) for e in self._config.items('paths')[row]) def addpathmap(self, path, localpath): """add path mapping to serve""" assert path not in self.paths self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount()) try: self._config.set('paths', hglib.fromunicode(path), hglib.fromunicode(localpath)) finally: self.endInsertRows() def setpathmap(self, path, localpath): """change path mapping at the specified index""" self._config.set('paths', hglib.fromunicode(path), hglib.fromunicode(localpath)) row = self._indexofpath(path) self.dataChanged.emit(self.index(row, 0), self.index(row, self.columnCount())) def removepathmap(self, path): """remove path from mapping""" row = self._indexofpath(path) self.beginRemoveRows(QModelIndex(), row, row) try: del self._config['paths'][hglib.fromunicode(path)] finally: self.endRemoveRows() def _indexofpath(self, path): path = hglib.fromunicode(path) assert path in self._config['paths'] return list(self._config['paths']).index(path)