/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/dbdialog.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/UI/dbdialog.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1625 - (show annotations)
Thu Aug 21 16:02:23 2003 UTC (21 years, 6 months ago) by bh
Original Path: trunk/thuban/Thuban/UI/dbdialog.py
File MIME type: text/x-python
File size: 17634 byte(s)
Make postgis support really optional including insensitive menu
items.

* Thuban/Model/postgisdb.py (has_postgis_support): New. Return
whether the postgis is supported.

* Thuban/UI/dbdialog.py: Put the psycopg import into try..except
to make postgis support optional

* Thuban/UI/mainwindow.py (_has_postgis_support): New. Context
version of Thuban.Model.postgisdb.has_postgis_support
(database_management command): Make it only sensitive if postgis
is supported.

1 # Copyright (c) 2001, 2003 by Intevation GmbH
2 # Authors:
3 # Martin Mueller <[email protected]>
4 # Bernhard Herzog <[email protected]>
5 #
6 # This program is free software under the GPL (>=v2)
7 # Read the file COPYING coming with Thuban for details.
8
9
10 """Dialogs to manage database connections"""
11
12 import sys, traceback
13
14 from wxPython.wx import *
15
16 try:
17 import psycopg
18 except ImportError:
19 psycopg = None
20
21 from Thuban import _
22 from Thuban.UI.dialogs import NonModalDialog
23 from Thuban.Model.postgisdb import PostGISConnection
24 from Thuban.Model.messages import DBCONN_ADDED, DBCONN_REMOVED
25 from messages import SESSION_REPLACED
26
27 ID_DBADD_OK = 9001
28 ID_DBADD_CANCEL = 9002
29
30 ID_DB_ADD = 9101
31 ID_DB_REMOVE = 9102
32 ID_DB_EDIT = 9103
33
34 ID_DBCHOOSE_RETRIEVE = 9201
35 ID_DBCHOOSE_OK = 9202
36 ID_DBCHOOSE_CANCEL = 9203
37 ID_LB_DCLICK = 9204
38
39 ID_DBPASSWD_OK = 9301
40 ID_DBPASSWD_CANCEL = 9202
41
42
43 class DBPwdDlg(wxDialog):
44
45 def __init__(self, user, *args, **kwds):
46 kwds["style"] = wxDIALOG_MODAL|wxCAPTION
47 wxDialog.__init__(self, *args, **kwds)
48 self.DBPWD_label = wxStaticText(self, -1,
49 _("Please enter the password:"))
50 self.DBPWD_label_user = wxStaticText(self, -1, _("User:"))
51 self.DBPWD_user = wxTextCtrl(self, -1, user)
52 self.DBPWD_label_passwd = wxStaticText(self, -1, _("Password:"))
53 self.DBPWD_passwd = wxTextCtrl(self, -1, "", style=wxTE_PASSWORD)
54 self.DBPWD_button_ok = wxButton(self, ID_DBPASSWD_OK, _("OK"))
55 self.DBPWD_button_cancel = wxButton(self, ID_DBPASSWD_CANCEL,
56 _("Cancel"))
57
58 EVT_BUTTON(self, ID_DBPASSWD_OK, self.OnOK)
59 EVT_BUTTON(self, ID_DBPASSWD_CANCEL, self.OnCancel)
60
61 self.__set_properties()
62 self.__do_layout()
63
64 def __set_properties(self):
65 self.SetTitle(_("Enter Password"))
66 self.DBPWD_button_ok.SetDefault()
67
68 def __do_layout(self):
69 grid_sizer_1 = wxFlexGridSizer(3, 1, 0, 0)
70 grid_sizer_3 = wxFlexGridSizer(1, 2, 0, 0)
71 grid_sizer_2 = wxFlexGridSizer(2, 2, 0, 0)
72 grid_sizer_1.Add(self.DBPWD_label, 0, wxALL, 4)
73 grid_sizer_2.Add(self.DBPWD_label_user, 0, wxALL, 4)
74 grid_sizer_2.Add(self.DBPWD_user, 0, wxALL, 4)
75 grid_sizer_2.Add(self.DBPWD_label_passwd, 0, wxALL, 4)
76 grid_sizer_2.Add(self.DBPWD_passwd, 0, wxALL, 4)
77 grid_sizer_1.Add(grid_sizer_2, 1, wxALIGN_CENTER_HORIZONTAL, 0)
78 grid_sizer_3.Add(self.DBPWD_button_ok, 0,
79 wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4)
80 grid_sizer_3.Add(self.DBPWD_button_cancel, 0,
81 wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4)
82 grid_sizer_1.Add(grid_sizer_3, 1,
83 wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4)
84 self.SetAutoLayout(1)
85 self.SetSizer(grid_sizer_1)
86 grid_sizer_1.Fit(self)
87 grid_sizer_1.SetSizeHints(self)
88 self.Layout()
89
90 def OnOK(self, event):
91 self.EndModal(wxID_OK)
92 self.Show(false)
93
94 def OnCancel(self, event):
95 self.EndModal(wxID_CANCEL)
96 self.Show(false)
97
98 def GetUser(self):
99 return self.DBPWD_user.GetValue()
100
101 def GetPasswd(self):
102 return self.DBPWD_passwd.GetValue()
103
104
105 class ChooseDBTableDialog(wxDialog):
106
107 def __init__(self, session, *args, **kwds):
108 kwds["style"] = wxDIALOG_MODAL|wxCAPTION
109 wxDialog.__init__(self, *args, **kwds)
110 self.session = session
111 self.dbconns = self.session.DBConnections()
112 self.tables = []
113 self.list_box_4 = wxListBox(self, -1)
114 for i in range(len(self.dbconns)):
115 self.list_box_4.Append(self.dbconns[i].BriefDescription())
116 self.DB_CHOOSE_RETRIEVE = wxButton(self, ID_DBCHOOSE_RETRIEVE,
117 _("Retrieve"))
118 self.list_box_5 = wxListBox(self, ID_LB_DCLICK)
119 self.DB_CHOOSE_OK = wxButton(self, ID_DBCHOOSE_OK, _("OK"))
120 self.DB_CHOOSE_CANCEL = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel"))
121 self.__set_properties()
122 self.__do_layout()
123
124 EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK)
125 EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel)
126 EVT_BUTTON(self, ID_DBCHOOSE_RETRIEVE, self.OnRetrieve)
127 EVT_LISTBOX_DCLICK(self, ID_LB_DCLICK, self.OnLBDClick)
128
129
130 def __set_properties(self):
131 self.SetTitle(_("Choose layer from database"))
132 self.list_box_4.SetSelection(0)
133 self.list_box_5.SetSelection(0)
134
135 def __do_layout(self):
136 grid_sizer_1 = wxFlexGridSizer(2, 1, 0, 0)
137 grid_sizer_3 = wxFlexGridSizer(1, 2, 0, 0)
138 grid_sizer_2 = wxFlexGridSizer(1, 3, 0, 0)
139 sizer_4 = wxStaticBoxSizer(wxStaticBox(self, -1, _("Tables")),
140 wxHORIZONTAL)
141 grid_sizer_4 = wxFlexGridSizer(3, 1, 0, 0)
142 sizer_3 = wxStaticBoxSizer(wxStaticBox(self, -1, _("Databases")),
143 wxHORIZONTAL)
144 sizer_3.Add(self.list_box_4, 0, wxEXPAND, 0)
145 grid_sizer_2.Add(sizer_3, 1, wxEXPAND, 0)
146 grid_sizer_4.Add(20, 80, 0, wxEXPAND, 0)
147 grid_sizer_4.Add(self.DB_CHOOSE_RETRIEVE, 0, wxALL
148 |wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4)
149 grid_sizer_4.Add(20, 80, 0, wxEXPAND, 0)
150 grid_sizer_2.Add(grid_sizer_4, 1, wxEXPAND, 0)
151 sizer_4.Add(self.list_box_5, 0, wxEXPAND, 0)
152 grid_sizer_2.Add(sizer_4, 1, wxEXPAND, 0)
153 grid_sizer_1.Add(grid_sizer_2, 1, wxEXPAND, 0)
154 grid_sizer_3.Add(self.DB_CHOOSE_OK, 0, wxALL|wxALIGN_RIGHT, 4)
155 grid_sizer_3.Add(self.DB_CHOOSE_CANCEL, 0, wxALL, 4)
156 grid_sizer_1.Add(grid_sizer_3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 4)
157 self.SetAutoLayout(1)
158 self.SetSizer(grid_sizer_1)
159 grid_sizer_1.Fit(self)
160 grid_sizer_1.SetSizeHints(self)
161 self.Layout()
162
163 def GetTable(self):
164 i = self.list_box_5.GetSelection()
165 if i >= 0:
166 return self.selected_conn, self.tables[i]
167 return None
168
169 def OnRetrieve(self, event):
170 i = self.list_box_4.GetSelection()
171 if i >= 0:
172 self.selected_conn = self.dbconns[i]
173 self.tables = self.selected_conn.GeometryTables()
174 self.list_box_5.Set(self.tables)
175
176 def OnLBDClick(self, event):
177 if self.list_box_5.GetSelection() >= 0:
178 self.EndModal(wxID_OK)
179 self.Show(false)
180
181 def OnOK(self, event):
182 self.EndModal(wxID_OK)
183 self.Show(false)
184
185 def OnCancel(self, event):
186 self.EndModal(wxID_CANCEL)
187 self.Show(false)
188
189
190 class DBDialog(wxDialog):
191
192 """Dialog for the parameters of a database connection"""
193
194 def __init__(self, selection=None, *args, **kwds):
195 kwds["style"] = wxDEFAULT_DIALOG_STYLE
196 wxDialog.__init__(self, *args, **kwds)
197 self.DBAdd_Label_Host = wxStaticText(self, -1, _("Hostname:"))
198 self.DBAdd_Host = wxTextCtrl(self, -1, "")
199 self.DBAdd_Label_Port = wxStaticText(self, -1, _("Port:"))
200 self.DBAdd_Port = wxTextCtrl(self, -1, "")
201 self.DBAdd_Label_Database = wxStaticText(self, -1, _("Database:"))
202 self.DBAdd_Database = wxTextCtrl(self, -1, "")
203 self.DBAdd_Label_User = wxStaticText(self, -1, _("User:"))
204 self.DBAdd_User = wxTextCtrl(self, -1, "")
205 self.DBAdd_Label_Pwd = wxStaticText(self, -1, _("Password:"))
206 self.DBAdd_Pwd = wxTextCtrl(self, -1, "", style=wxTE_PASSWORD)
207 self.DBAdd_OK = wxButton(self, ID_DBADD_OK, _("OK"))
208 self.DBAdd_Cancel = wxButton(self, ID_DBADD_CANCEL, _("Cancel"))
209 if selection != None:
210 self.DBAdd_Host.SetEditable(false)
211 self.DBAdd_Host.SetValue(selection.host)
212 self.DBAdd_Port.SetValue(selection.port)
213 self.DBAdd_Database.SetEditable(false)
214 self.DBAdd_Database.SetValue(selection.dbname())
215 self.DBAdd_User.SetValue(selection.user)
216 self.__set_properties()
217 self.__do_layout()
218 EVT_BUTTON(self, ID_DBADD_OK, self.OnOK)
219 EVT_BUTTON(self, ID_DBADD_CANCEL, self.OnCancel)
220
221 def __set_properties(self):
222 self.SetTitle(_("Add database"))
223 self.SetSize((480, 185))
224 self.DBAdd_Label_Host.SetSize((60, 16))
225 self.DBAdd_Host.SetSize((200, 22))
226 self.DBAdd_Label_Database.SetSize((60, 16))
227 self.DBAdd_Label_User.SetSize((60, 16))
228
229 def __do_layout(self):
230 grid_sizer_1 = wxFlexGridSizer(4, 1, 0, 0)
231 grid_sizer_2 = wxGridSizer(1, 2, 0, 0)
232 grid_sizer_5 = wxFlexGridSizer(1, 5, 0, 0)
233 grid_sizer_6 = wxFlexGridSizer(1, 5, 0, 0)
234 grid_sizer_3 = wxFlexGridSizer(1, 5, 0, 0)
235 grid_sizer_3.Add(self.DBAdd_Label_Host, 0,
236 wxALL|wxALIGN_CENTER_VERTICAL, 4)
237 grid_sizer_3.Add(self.DBAdd_Host, 0, wxALL, 4)
238 grid_sizer_3.Add(20, 20, 0, wxALL|wxEXPAND, 4)
239 grid_sizer_3.Add(self.DBAdd_Label_Port, 0,
240 wxALL|wxALIGN_CENTER_VERTICAL, 4)
241 grid_sizer_3.Add(self.DBAdd_Port, 0, wxALL, 4)
242 grid_sizer_1.Add(grid_sizer_3, 1, wxALL|wxEXPAND, 4)
243 grid_sizer_6.Add(self.DBAdd_Label_Database, 0,
244 wxALL|wxALIGN_CENTER_VERTICAL, 4)
245 grid_sizer_6.Add(self.DBAdd_Database, 0, wxALL, 4)
246 grid_sizer_6.Add(20, 20, 0, wxALL|wxEXPAND, 4)
247 grid_sizer_1.Add(grid_sizer_6, 1, wxALL|wxEXPAND, 4)
248 grid_sizer_5.Add(self.DBAdd_Label_User, 0,
249 wxALL|wxALIGN_CENTER_VERTICAL, 4)
250 grid_sizer_5.Add(self.DBAdd_User, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4)
251 grid_sizer_5.Add(20, 20, 0, wxALL|wxEXPAND, 4)
252 grid_sizer_5.Add(self.DBAdd_Label_Pwd, 0,
253 wxALL|wxALIGN_CENTER_VERTICAL, 4)
254 grid_sizer_5.Add(self.DBAdd_Pwd, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4)
255 grid_sizer_1.Add(grid_sizer_5, 1, wxALL|wxEXPAND, 4)
256 grid_sizer_2.Add(self.DBAdd_OK, 0,
257 wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 4)
258 grid_sizer_2.Add(self.DBAdd_Cancel, 0, wxALL|wxALIGN_CENTER_VERTICAL,
259 4)
260 grid_sizer_1.Add(grid_sizer_2, 1, wxALL|wxEXPAND, 4)
261 self.SetAutoLayout(1)
262 self.SetSizer(grid_sizer_1)
263 self.Layout()
264
265
266 def GetHostname(self):
267 return self.DBAdd_Host.GetValue()
268
269 def GetPort(self):
270 return self.DBAdd_Port.GetValue()
271
272 def GetDatabase(self):
273 return self.DBAdd_Database.GetValue()
274
275 def GetUser(self):
276 return self.DBAdd_User.GetValue()
277
278 def GetPassword(self):
279 return self.DBAdd_Pwd.GetValue()
280
281 def OnOK(self, event):
282 self.EndModal(wxID_OK)
283 self.Show(false)
284
285 def OnCancel(self, event):
286 self.EndModal(wxID_CANCEL)
287 self.Show(false)
288
289
290 class DBFrame(NonModalDialog):
291
292 """Databse connection management dialog"""
293
294 def __init__(self, parent, name, session, *args, **kwds):
295 kwds["style"] = wxICONIZE|wxCAPTION|wxMINIMIZE
296 NonModalDialog.__init__(self, parent, name, "")
297 self.session = session
298 self.app = self.parent.application
299
300 self.app.Subscribe(SESSION_REPLACED, self.session_replaced)
301 self.subscribe_session()
302
303 self.DB_ListBox = wxListBox(self, -1,
304 style=wxLB_SINGLE|wxLB_HSCROLL|wxLB_ALWAYS_SB)
305 self.DB_Add = wxButton(self, ID_DB_ADD, _("Add"))
306 self.DB_Remove = wxButton(self, ID_DB_REMOVE, _("Remove"))
307 #self.DB_Edit = wxButton(self, ID_DB_EDIT, _("Edit"))
308 self.DB_CLOSE = wxButton(self, wxID_CLOSE, _("Close"))
309 self.__set_properties()
310 self.__do_layout()
311 EVT_BUTTON(self, ID_DB_ADD, self.OnAdd)
312 EVT_BUTTON(self, ID_DB_REMOVE, self.OnRemove)
313 #EVT_BUTTON(self, ID_DB_EDIT, self.OnEdit)
314 EVT_BUTTON(self, wxID_CLOSE, self.OnClose)
315
316 self.conns_changed()
317
318 def __set_properties(self):
319 self.SetTitle(_("Database Management"))
320 self.DB_ListBox.SetSize((200, 157))
321 self.DB_ListBox.SetSelection(0)
322
323 def __do_layout(self):
324 top = wxBoxSizer(wxVERTICAL)
325
326 box = wxBoxSizer(wxHORIZONTAL)
327
328 box.Add(self.DB_ListBox, 1, wxALL|wxEXPAND
329 |wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4)
330
331 buttons = wxBoxSizer(wxVERTICAL)
332 buttons.Add(self.DB_Add, 0, wxALL, 4)
333 buttons.Add(self.DB_Remove, 0, wxALL, 4)
334 #buttons.Add(self.DB_Edit, 0, wxALL, 4)
335
336 box.Add(buttons, 0, wxEXPAND)
337 top.Add(box, 1, wxEXPAND)
338
339 buttons = wxBoxSizer(wxHORIZONTAL)
340 buttons.Add(self.DB_CLOSE, 1, wxALL|wxALIGN_RIGHT, 4)
341 top.Add(buttons, 0, wxALIGN_RIGHT)
342
343 self.SetAutoLayout(1)
344 self.SetSizer(top)
345 top.Fit(self)
346 top.SetSizeHints(self)
347
348 def subscribe_session(self):
349 """Internal: Subscribe to some session messages"""
350 self.session.Subscribe(DBCONN_ADDED, self.conns_changed)
351 self.session.Subscribe(DBCONN_REMOVED, self.conns_changed)
352
353 def unsubscribe_session(self):
354 """Internal: Subscribe from messages subscribe in subscribe_session"""
355 self.session.Subscribe(DBCONN_ADDED, self.conns_changed)
356 self.session.Subscribe(DBCONN_REMOVED, self.conns_changed)
357
358 def session_replaced(self, *args):
359 """Internal: resubscribe the session messages
360
361 This method is a subscriber for the application's
362 SESSION_REPLACED messages.
363 """
364 self.unsubscribe_session()
365 self.session = self.app.Session()
366 self.subscribe_session()
367 self.conns_changed()
368
369 def conns_changed(self, *args):
370 """Internal: update the db connection list box
371
372 Subscribed to the DBCONN_REMOVED and DBCONN_REMOVED.
373 """
374 self.DB_ListBox.Clear()
375 for conn in self.session.DBConnections():
376 self.DB_ListBox.Append(conn.BriefDescription(), conn)
377
378 def OnClose(self, event):
379 self.unsubscribe_session()
380 self.app.Unsubscribe(SESSION_REPLACED, self.session_replaced)
381 NonModalDialog.OnClose(self, event)
382
383 def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION):
384 """Run a modal message box with the given text, title and flags
385 and return the result"""
386 dlg = wxMessageDialog(self, text, title, flags)
387 dlg.CenterOnParent()
388 result = dlg.ShowModal()
389 dlg.Destroy()
390 return result
391
392 def OnAdd(self, event):
393 adddialog = DBDialog(None, self, -1, "")
394 while 1:
395 if adddialog.ShowModal() == wxID_OK:
396 host = adddialog.GetHostname()
397 port = adddialog.GetPort()
398 database = adddialog.GetDatabase()
399 user = adddialog.GetUser()
400 password = adddialog.GetPassword()
401 for conn in self.session.DBConnections():
402 if (host == conn.host and
403 database == conn.dbname):
404 self.RunMessageBox(_("Add Database"),
405 _("Connection to '%s' already exists")
406 % database)
407 break
408 try:
409 conn = PostGISConnection(database, host = host, port=port,
410 user = user, password = password)
411 self.session.AddDBConnection(conn)
412 break
413 except psycopg.OperationalError, strerror:
414 self.RunMessageBox(_("Error"), str(strerror))
415 except:
416 self.RunMessageBox(_("Add Database"),
417 _("Can't establish connection to '%s'."
418 % database))
419 else:
420 break
421 adddialog.Destroy()
422
423 def OnRemove(self, event):
424 i = self.DB_ListBox.GetSelection()
425 if i >= 0:
426 self.session.RemoveDBConnection(self.DB_ListBox.GetClientData(i))
427
428 def OnEdit(self, event):
429 i = self.DB_ListBox.GetSelection()
430 if i >= 0:
431 editdialog = DBDialog(self.DB_ListBox.GetClientData(i), self, -1,
432 "")
433 while 1:
434 if editdialog.ShowModal() == wxID_OK:
435 host = editdialog.GetHostname()
436 database = editdialog.GetDatabase()
437 port = editdialog.GetPort()
438 user = editdialog.GetUser()
439 password = editdialog.GetPassword()
440 try:
441 conn = PostGISConnection(database, host = host,
442 port = port, user = user,
443 password = password)
444 self.session.ChangeDBConnection(conn)
445 break
446 except psycopg.OperationalError, r:
447 self.RunMessageBox(_("Add Database"),
448 _("An exception occurred: '%s'."
449 % r))
450 except:
451 self.RunMessageBox(_("Add Database"),
452 _("Can't establish connection to '%s'."
453 % database))
454 else:
455 break
456 editdialog.Destroy()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26