1 from xml
.dom
import XMLNS_NAMESPACE
, XML_NAMESPACE
6 fixed_ns
= {'xml': XML_NAMESPACE
, 'xmlns': XMLNS_NAMESPACE
}
8 class Namespaces(g
.GenericTreeModel
):
10 g
.GenericTreeModel
.__init
__(self
)
11 self
.uri
= dict(fixed_ns
)
14 def update_list(self
):
15 self
.list = self
.uri
.keys()
17 pre
= self
.prefix
= {}
18 for p
, u
in self
.uri
.iteritems():
21 def on_get_n_columns(self
):
24 def on_get_iter(self
, path
):
28 def on_get_value(self
, iter, column
):
30 return self
.list[iter]
31 return self
.uri
[self
.list[iter]]
33 def on_iter_nth_child(self
, iter, n
):
38 def on_get_column_type(self
, col
):
41 def on_iter_has_child(self
, iter):
44 def on_iter_next(self
, iter):
45 if iter < len(self
.list) - 1:
48 def __setitem__(self
, prefix
, uri
):
49 if prefix
in self
.uri
and self
.uri
[prefix
] == uri
:
53 if prefix
in fixed_ns
:
54 raise Exception('That namespace prefix cannot be changed')
56 modifed
= prefix
in self
.uri
58 self
.uri
[prefix
] = uri
60 path
= (self
.list.index(prefix
),)
63 self
.emit('row-changed', path
, self
.get_iter(path
))
65 self
.emit('row-inserted', path
, self
.get_iter(path
))
67 def __delitem__(self
, iter):
68 prefix
= self
[iter][0]
69 if prefix
in fixed_ns
:
70 raise Exception('This is a built-in namespace and cannot be deleted')
71 path
= (self
.list.index(prefix
),)
74 self
.emit('row-deleted', path
)
76 def ensure_ns(self
, suggested_prefix
, uri
):
77 """Return the prefix for this URI. If none is set choose one (using suggested_prefix
80 return self
.prefix
[uri
]
82 if not suggested_prefix
:
83 suggested_prefix
= 'ns'
84 if suggested_prefix
in self
.uri
:
86 while suggested_prefix
+ `x`
in self
.uri
: x
+= 1
87 suggested_prefix
+= `x`
88 self
[suggested_prefix
] = uri
89 print "Added", suggested_prefix
, uri
90 return suggested_prefix
92 class GUI(rox
.Dialog
):
93 def __init__(self
, model
):
94 rox
.Dialog
.__init
__(self
)
97 self
.add_button(g
.STOCK_ADD
, 1)
98 self
.add_button(g
.STOCK_DELETE
, 2)
99 self
.add_button(g
.STOCK_CLOSE
, g
.RESPONSE_OK
)
101 tree
= g
.TreeView(model
.namespaces
)
103 def response(dialog
, resp
):
105 dialog
.add_new(model
.namespaces
)
107 dialog
.delete_selected(tree
.get_selection())
110 self
.connect('response', response
)
111 self
.set_position(g
.WIN_POS_MOUSE
)
112 self
.set_title('Namespaces for ' + `model
.uri`
)
116 frame
.set_shadow_type(g
.SHADOW_IN
)
118 cell
= g
.CellRendererText()
119 column
= g
.TreeViewColumn('Prefix', cell
, text
= 0)
120 tree
.append_column(column
)
122 column
= g
.TreeViewColumn('URI', cell
, text
= 1)
123 tree
.append_column(column
)
127 self
.vbox
.pack_start(frame
, True, True)
128 self
.set_default_size(400, 200)
130 self
.set_has_separator(False)
132 def delete_selected(self
, sel
):
133 model
, iter = sel
.get_selected()
135 rox
.alert('Select a namespace binding to delete')
140 rox
.report_exception()
142 def add_new(self
, ns
):
145 hbox
= g
.HBox(False, 2)
146 d
.vbox
.pack_start(hbox
, False, True)
147 hbox
.pack_start(g
.Label('Prefix:'), False, True, 0)
149 hbox
.pack_start(prefix
, True, True, 0)
151 hbox
= g
.HBox(False, 2)
152 d
.vbox
.pack_start(hbox
, False, True)
153 hbox
.pack_start(g
.Label('Namespace URI:'), False, True, 0)
155 hbox
.pack_start(uri
, True, True, 0)
156 uri
.set_activates_default(True)
159 d
.set_position(g
.WIN_POS_MOUSE
)
163 d
.add_button(g
.STOCK_CANCEL
, g
.RESPONSE_CANCEL
)
164 d
.add_button(g
.STOCK_OK
, g
.RESPONSE_OK
)
166 d
.set_default_response(g
.RESPONSE_OK
)
168 if d
.run() != g
.RESPONSE_OK
:
172 ns
[prefix
.get_text()] = uri
.get_text()
174 rox
.report_exception()