Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 2.0, 2.0.1, and 2.0.2

stable repotreemodel: add function to iterate RepoItem from thg-reporegistry.xml

Changeset 5bd340383171

Parent c8e316f1c1d7

by Yuya Nishihara

Changes to 3 files · Browse files at 5bd340383171 Showing diff from parent c8e316f1c1d7 Diff from another changeset...

Change 1 of 1 Show Entire File tests/​fixtures/​reporegistry.xml Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?> +<reporegistry> + <treeitem> + <allgroup> + <repo root="/thg" shortname="thg" basenode="bac32db38e52fd49acb62b94730a55f4f4b0cdee"/> + <repo root="/mercurial" shortname="hg" basenode="9117c6561b0bd7792fa13b50d28239d51b78e51f"/> + </allgroup> + <group name="bar"> + <repo root="/python-vcs" shortname="python-vcs" basenode="b986218ba1c9b0d6a259fac9b050b1724ed8e545"/> + </group> + </treeitem> +</reporegistry>
Change 1 of 1 Show Entire File tests/​qt_repotreemodel_test.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@@ -0,0 +1,21 @@
+import os +from nose.tools import * +from mercurial import node +from tortoisehg.hgqt.repotreemodel import * +from tests import FIXTURES_DIR + +def openfixture(name, mode=QIODevice.ReadOnly): + path = os.path.join(FIXTURES_DIR, name) + f = QFile(path) + f.open(mode) + return f + +def test_iterrepoitemfromxml(): + f = openfixture('reporegistry.xml') + repos = list(iterRepoItemFromXml(f)) + f.close() + assert_equals(['/thg', '/mercurial', '/python-vcs'], + map(lambda e: e.rootpath(), repos)) + assert_equals('thg', repos[0].shortname()) + assert_equals('bac32db38e52fd49acb62b94730a55f4f4b0cdee', + node.hex(repos[0].basenode()))
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
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
 # repotreemodel.py - model for the reporegistry  #  # Copyright 2010 Adrian Buehlmann <adrian@cadifra.com>  #  # This software may be used and distributed according to the terms of the  # GNU General Public License version 2 or any later version.    from mercurial import error    from tortoisehg.hgqt import thgrepo  from tortoisehg.util import hglib  from tortoisehg.hgqt.i18n import _    from repotreeitem import undumpObject, AllRepoGroupItem, RepoGroupItem  from repotreeitem import RepoItem, RepoTreeItem    from PyQt4.QtCore import *  from PyQt4.QtGui import *      extractXmlElementName = 'reporegextract'  reporegistryXmlElementName = 'reporegistry'    repoRegMimeType = 'application/thg-reporegistry'      def writeXml(target, item, rootElementName):   xw = QXmlStreamWriter(target)   xw.setAutoFormatting(True)   xw.setAutoFormattingIndent(2)   xw.writeStartDocument()   xw.writeStartElement(rootElementName)   item.dumpObject(xw)   xw.writeEndElement()   xw.writeEndDocument()    def readXml(source, rootElementName, model):   itemread = None   xr = QXmlStreamReader(source)   if xr.readNextStartElement():   ele = str(xr.name().toString())   if ele != rootElementName:   print "unexpected xml element '%s' "\   "(was looking for %s)" % (ele, rootElementName)   return   if xr.hasError():   print str(xr.errorString())   if xr.readNextStartElement():   itemread = undumpObject(xr, model)   xr.skipCurrentElement()   if xr.hasError():   print str(xr.errorString())   return itemread   +def iterRepoItemFromXml(source, model=None): + xr = QXmlStreamReader(source) + while not xr.atEnd(): + t = xr.readNext() + if t == QXmlStreamReader.StartElement and xr.name() == 'repo': + yield undumpObject(xr, model)    class RepoTreeModel(QAbstractItemModel):   def __init__(self, openrepofunc, filename=None, parent=None):   QAbstractItemModel.__init__(self, parent)     self.openrepofunc = openrepofunc     root = None   all = None     if filename:   f = QFile(filename)   if f.open(QIODevice.ReadOnly):   root = readXml(f, reporegistryXmlElementName, self)   f.close()   if root:   for c in root.childs:   if isinstance(c, AllRepoGroupItem):   all = c   break     if not root:   root = RepoTreeItem(self)   all = AllRepoGroupItem(self)   root.appendChild(all)     self.rootItem = root   self.allrepos = all     # see http://doc.qt.nokia.com/4.6/model-view-model-subclassing.html     # overrides from QAbstractItemModel     def index(self, row, column, parent):   if not self.hasIndex(row, column, parent):   return QModelIndex()   if (not parent.isValid()):   parentItem = self.rootItem   else:   parentItem = parent.internalPointer()   childItem = parentItem.child(row)   if childItem:   return self.createIndex(row, column, childItem)   else:   return QModelIndex()     def parent(self, index):   if not index.isValid():   return QModelIndex()   childItem = index.internalPointer()   parentItem = childItem.parent()   if parentItem is self.rootItem:   return QModelIndex()   return self.createIndex(parentItem.row(), 0, parentItem)     def rowCount(self, parent):   if parent.column() > 0:   return 0   if not parent.isValid():   parentItem = self.rootItem;   else:   parentItem = parent.internalPointer()   return parentItem.childCount()     def columnCount(self, parent):   if parent.isValid():   return parent.internalPointer().columnCount()   else:   return self.rootItem.columnCount()     def data(self, index, role):   if not index.isValid():   return QVariant()   if role not in (Qt.DisplayRole, Qt.EditRole, Qt.DecorationRole):   return QVariant()   item = index.internalPointer()   return item.data(index.column(), role)     def headerData(self, section, orientation, role):   if role == Qt.DisplayRole:   if orientation == Qt.Horizontal:   if section == 1:   return QString(_('Path'))   return QVariant()     def flags(self, index):   if not index.isValid():   return Qt.NoItemFlags   item = index.internalPointer()   return item.flags()     def supportedDropActions(self):   return Qt.CopyAction | Qt.MoveAction | Qt.LinkAction     def removeRows(self, row, count, parent):   item = parent.internalPointer()   if item is None:   item = self.rootItem   self.beginRemoveRows(parent, row, row+count-1)   res = item.removeRows(row, count)   self.endRemoveRows()   return res     def mimeTypes(self):   return QStringList(repoRegMimeType)     def mimeData(self, indexes):   i = indexes[0]   item = i.internalPointer()   buf = QByteArray()   writeXml(buf, item, extractXmlElementName)   d = QMimeData()   d.setData(repoRegMimeType, buf)   d.setUrls([QUrl.fromLocalFile(hglib.tounicode(item.rootpath()))])   return d     def dropMimeData(self, data, action, row, column, parent):   d = str(data.data(repoRegMimeType))   itemread = readXml(d, extractXmlElementName, self)   group = parent.internalPointer()   if row < 0:   row = 0   self.beginInsertRows(parent, row, row)   group.insertChild(row, itemread)   self.endInsertRows()   return True     def setData(self, index, value, role):   if not index.isValid() or role != Qt.EditRole:   return False   s = value.toString()   if s.isEmpty():   return False   item = index.internalPointer()   if item.setData(index.column(), value):   self.emit(SIGNAL('dataChanged(index, index)'), index, index)   return True   return False     # functions not defined in QAbstractItemModel     def allreposIndex(self):   return self.createIndex(0, 0, self.allrepos)     def addRepo(self, group, repo):   if not repo:   caption = _('Select repository directory to add')   FD = QFileDialog   path = FD.getExistingDirectory(caption=caption,   options=FD.ShowDirsOnly | FD.ReadOnly)   if path:   try:   lpath = hglib.fromunicode(path)   repo = thgrepo.repository(None, path=lpath)   except error.RepoError:   QMessageBox.warning(self, _('Failed to add repository'),   _('%s is not a valid repository') % path)   return   else:   return   grp = group   if grp == None:   grp = self.allreposIndex()   rgi = grp.internalPointer()   cc = rgi.childCount()   self.beginInsertRows(grp, cc, cc + 1)   rgi.appendChild(RepoItem(self, repo))   self.endInsertRows()     def getRepoItem(self, reporoot):   return self.rootItem.getRepoItem(reporoot)     def addGroup(self, name):   ri = self.rootItem   cc = ri.childCount()   self.beginInsertRows(QModelIndex(), cc, cc + 1)   ri.appendChild(RepoGroupItem(self, name, ri))   self.endInsertRows()     def write(self, fn):   f = QFile(fn)   f.open(QIODevice.WriteOnly)   writeXml(f, self.rootItem, reporegistryXmlElementName)   f.close()