pgconsole/app.py
author Radek Brich <radek.brich@devl.cz>
Wed, 09 Jul 2014 18:03:54 +0200
changeset 104 d8ff52a0390f
parent 76 3a41b351b122
permissions -rw-r--r--
Rename to pydbkit.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
     1
from gi.repository import GObject
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
     2
from gi.repository import Gtk, Gdk
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
     3
from gi.repository import GdkPixbuf
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
     4
from gi.repository import cairo, Pango
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
     5
from gi.repository import GtkSource
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
import psycopg2
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
import psycopg2.extensions
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
import psycopg2.extras
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    11
import time
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    12
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
from pgconsole.config import cfg
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
from pgconsole.editor import Editor
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
from pgconsole.dataview import DataView
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
from pgconsole.database import Database, BadConnectionError, DatabaseError
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
from pgconsole.settings import Settings
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
from pgconsole.panedext import HPanedExt, VPanedExt
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
class PgConsoleApp:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
    def __init__(self):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
        self.db = Database()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
        self.conn = None
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    26
        win = Gtk.Window()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
        self.win = win
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
        win.set_title('PostgreSQL Console')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
        win.set_size_request(300, 200)  # minimal size
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
        win.set_default_size(800, 600)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
        self.restore_window_size()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
        win.connect("destroy", self.destroy)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
        win.connect("key_press_event", self.keypress)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
        win.connect('configure-event', self.on_configure)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
        # toolbar
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    37
        toolbar = Gtk.Toolbar()
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    38
        toolbar.set_style(Gtk.ToolbarStyle.ICONS)
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    39
        toolbar.set_property("icon-size", Gtk.IconSize.SMALL_TOOLBAR)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    41
        tb = Gtk.ToolButton(Gtk.STOCK_PREFERENCES)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
        tb.set_label('Settings')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
        tb.set_tooltip_text('Settings')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
        tb.connect('clicked', self.settings)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
        self.tb_settings = tb
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
        toolbar.add(tb)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    48
        self.cb_server = Gtk.ComboBoxText.new_with_entry()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
        self.cb_server.set_tooltip_text('Server')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
        self.cb_server.get_child().set_property("editable", False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
        self.cb_server.set_property("add_tearoffs", True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
        #self.cb_server.set_property("focus-on-click", False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        self.cb_server.set_property("can-focus", True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
        self.cb_server.connect('changed', self.on_server_changed)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
        self.cb_server.connect("key_press_event", self.toolbar_server_keypress)
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    56
        ti = Gtk.ToolItem()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
        ti.add(self.cb_server)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
        toolbar.add(ti)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    60
        self.cb_dbname = Gtk.ComboBoxText.new_with_entry()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
        self.cb_dbname.set_tooltip_text('Database')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
        self.cb_dbname.get_child().set_property("editable", False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
        self.cb_dbname.set_property("add_tearoffs", True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
        #self.cb_dbname.set_property("focus-on-click", False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
        self.cb_dbname.set_property("can-focus", True)
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    66
        ti = Gtk.ToolItem()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
        ti.add(self.cb_dbname)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
        toolbar.add(ti)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    70
        tb = Gtk.ToolButton(Gtk.STOCK_CONNECT)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
        tb.set_label('Connect')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
        tb.set_tooltip_text('Connect')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
        tb.connect('clicked', self.connect)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
        toolbar.add(tb)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
        self.tb_connect = tb
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    77
        sep = Gtk.SeparatorToolItem()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
        toolbar.add(sep)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    80
        tb = Gtk.ToolButton(Gtk.STOCK_EXECUTE)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
        tb.set_label('Execute')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
        tb.set_tooltip_text('Execute')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
        tb.connect('clicked', self.execute)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
        toolbar.add(tb)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
        tb.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
        self.tb_execute = tb
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    87
        tb = Gtk.ToolButton(Gtk.STOCK_NEW)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
        tb.set_label('Begin transaction')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
        tb.set_tooltip_text('Begin transaction')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
        tb.connect('clicked', self.begin)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
        tb.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
        toolbar.add(tb)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
        self.tb_begin = tb
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    94
        tb = Gtk.ToolButton(Gtk.STOCK_APPLY)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
        tb.set_label('Commit')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
        tb.set_tooltip_text('Commit')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
        tb.connect('clicked', self.commit)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
        tb.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
        self.tb_commit = tb
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
        toolbar.add(tb)
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   101
        tb = Gtk.ToolButton(Gtk.STOCK_CANCEL)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
        tb.set_label('Rollback')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
        tb.set_tooltip_text('Rollback')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
        tb.connect('clicked', self.rollback)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
        tb.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
        self.tb_rollback = tb
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
        toolbar.add(tb)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   109
        sep = Gtk.SeparatorToolItem()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
        toolbar.add(sep)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   112
        # editor
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   113
        self.editor = Editor()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   114
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
        # data view
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
        self.dataview = DataView()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   117
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   119
        vbox = Gtk.VBox(False, 2)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   121
        sep = Gtk.SeparatorToolItem()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   122
        sep.set_expand(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
        sep.set_draw(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   124
        toolbar.add(sep)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   125
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   126
        self.throbber_anim = GdkPixbuf.PixbufAnimation.new_from_file('data/Throbber.gif')
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   127
        self.throbber = Gtk.Image()
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   128
        tb = Gtk.ToolItem()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   129
        tb.add(self.throbber)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   130
        toolbar.add(tb)
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   131
        sep = Gtk.SeparatorToolItem()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
        sep.set_draw(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   133
        toolbar.add(sep)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   134
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   135
        vbox.pack_start(toolbar, False, False, 0)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   136
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   137
        vpaned = VPanedExt()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   138
        vpaned.set_border_width(5)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   139
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   140
        hpaned = HPanedExt()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   141
        hpaned.set_border_width(0)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   142
        hpaned.add1(self.editor)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   143
        hpaned.child_set_property(self.editor, 'shrink', False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   144
        hpaned.set_snap2(80)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
        hpaned.set_property('position', 500)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   146
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   147
        vpaned.add1(hpaned)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   148
        vpaned.set_snap1(80)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
        vpaned.set_property('position', 300)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   151
        vpaned.add2(self.dataview)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   152
        vpaned.set_snap2(80)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   154
        self.vpaned = vpaned
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
        self.hpaned = hpaned
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
        # log
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   158
        self.logbuf = Gtk.TextBuffer()
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   159
        view = Gtk.TextView.new_with_buffer(self.logbuf)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
        view.set_editable(False)
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   161
        font_desc = Pango.FontDescription('monospace')
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
        if font_desc:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   163
            view.modify_font(font_desc)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   164
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   165
        sw = Gtk.ScrolledWindow()
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   166
        sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   167
        sw.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   168
        sw.add(view)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   169
        hpaned.add2(sw)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   170
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   171
        vbox.pack_start(vpaned, expand=True, fill=True, padding=0)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   172
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   173
        win.add(vbox)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   174
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   175
        self.editor.view.grab_focus()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   176
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   177
        self.reload_server_list(0)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   178
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   179
        win.show_all()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   180
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   181
        self.restore_win_state()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   182
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   183
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   184
    def main(self):
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   185
        Gtk.main()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   186
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   187
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   188
    def destroy(self, widget, data=None):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   189
        self.save_win_state()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   190
        cfg.save()
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   191
        Gtk.main_quit()
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   192
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   193
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   194
    def on_configure(self, w, ev):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   195
        cfg.root.window.size.width = ev.width
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   196
        cfg.root.window.size.height = ev.height
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   197
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   198
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   199
    def restore_window_size(self):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   200
        self.win.resize(cfg.root.window.size.width, cfg.root.window.size.height)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   201
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   202
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   203
    def save_win_state(self):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   204
        cfg.root.window.dividers.verticaldivider = self.vpaned.get_position()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   205
        cfg.root.window.dividers.horizontaldivider = self.hpaned.get_position()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   206
        cfg.root.window.dividers.editordivider = self.editor.get_position()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   207
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   208
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   209
    def restore_win_state(self):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   210
        pos = cfg.root.window.dividers.verticaldivider
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   211
        if pos >= 0:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   212
            self.vpaned.set_position(pos)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   213
        pos = cfg.root.window.dividers.horizontaldivider
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   214
        if pos >= 0:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   215
            self.hpaned.set_position(pos)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   216
        pos = cfg.root.window.dividers.editordivider
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   217
        if pos >= 0:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   218
            self.editor.set_position(pos)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   219
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   220
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   221
    def get_typename(self, oid, size):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   222
        self.curs.execute('SELECT typname FROM pg_type WHERE oid=%s', [oid])
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   223
        psycopg2.extras.wait_select(self.curs.connection)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   224
        row = self.curs.fetchone()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   225
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   226
        typname = None
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   227
        if row:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   228
            typname = row[0]
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   229
            if typname == 'int4':
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   230
                return 'integer'
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   231
            if typname in ('timestamp', 'interval', 'date'):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   232
                return typname
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   233
            if size and size > 0 and size < 65535:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   234
                typname += '(%s)' % size
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   235
        return typname
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   236
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   237
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   238
    def get_conninfo(self, nodb=False):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   239
        sel = self.cb_server.get_active()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   240
        srv = cfg.servers.server[sel]
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   241
        if nodb:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   242
            dbname = 'postgres'
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   243
        else:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   244
            dbname = self.cb_dbname.get_active_text()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   245
        conninfo = 'host=%s port=%s dbname=%s user=%s password=%s' \
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   246
            % (srv.host, srv.port, dbname, srv.user, srv.password)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   247
        return conninfo
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   248
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   249
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   250
    def connect(self, w):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   251
        conninfo = self.get_conninfo()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   252
        if self.conn:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   253
            # disconnect
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   254
            self.db.put_conn(conninfo, self.conn)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   255
            self.conn = None
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   256
            self.cb_server.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   257
            self.cb_dbname.set_sensitive(True)
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   258
            self.tb_connect.set_stock_id(Gtk.STOCK_CONNECT)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   259
            self.tb_connect.set_label('Connect')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   260
            self.tb_connect.set_tooltip_text('Connect')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   261
            self.tb_execute.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   262
            self.tb_begin.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   263
        else:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   264
            # connect
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   265
            self.logbuf.insert(self.logbuf.get_end_iter(), 'Connect %s\n' % conninfo)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   266
            try:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   267
                self.conn = self.db.get_conn(conninfo)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   268
            except DatabaseError as e:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   269
                self.logbuf.insert(self.logbuf.get_end_iter(), 'Error:\n%s\n' % e)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   270
                return
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   271
            self.cb_server.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   272
            self.cb_dbname.set_sensitive(False)
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   273
            self.tb_connect.set_stock_id(Gtk.STOCK_DISCONNECT)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   274
            self.tb_connect.set_label('Disconnect')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   275
            self.tb_connect.set_tooltip_text('Disconnect')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   276
            self.tb_execute.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   277
            self.tb_begin.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   278
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   279
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   280
    def begin(self, w):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   281
        self.logbuf.insert(self.logbuf.get_end_iter(), 'Begin transaction\n')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   282
        curs = self.conn.cursor()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   283
        curs.execute('BEGIN')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   284
        psycopg2.extras.wait_select(curs.connection)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   285
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   286
        self.tb_connect.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   287
        self.tb_begin.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   288
        self.tb_commit.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   289
        self.tb_rollback.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   290
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   291
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   292
    def commit(self, w):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   293
        self.logbuf.insert(self.logbuf.get_end_iter(), 'Commit\n')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   294
        curs = self.conn.cursor()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   295
        curs.execute('COMMIT')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   296
        psycopg2.extras.wait_select(curs.connection)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   297
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   298
        self.tb_connect.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   299
        self.tb_begin.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   300
        self.tb_commit.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   301
        self.tb_rollback.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   302
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   303
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   304
    def rollback(self, w):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   305
        self.logbuf.insert(self.logbuf.get_end_iter(), 'Rollback\n')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   306
        curs = self.conn.cursor()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   307
        curs.execute('ROLLBACK')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   308
        psycopg2.extras.wait_select(curs.connection)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   309
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   310
        self.tb_connect.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   311
        self.tb_begin.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   312
        self.tb_commit.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   313
        self.tb_rollback.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   314
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   315
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   316
    def execute(self, widget):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   317
        query = self.editor.get_selection() or self.editor.get_text()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   318
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   319
        self.tb_connect.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   320
        self.tb_execute.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   321
        self.tb_begin.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   322
        self.tb_commit.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   323
        self.tb_rollback.set_sensitive(False)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   324
        self.throbber.set_from_animation(self.throbber_anim)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   325
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   326
        self.curs = self.conn.cursor()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   327
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   328
        self.t1 = time.time()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   329
        try:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   330
            self.curs.execute(query)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   331
        except (psycopg2.OperationalError, psycopg2.DatabaseError) as e:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   332
            self.logbuf.insert(self.logbuf.get_end_iter(), 'Error:\n' + str(e))
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   333
            return
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   334
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   335
        self.execute_poll()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   336
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   337
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   338
    def execute_poll(self, source=None, cond=None):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   339
        try:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   340
            state = self.conn.poll()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   341
        except (psycopg2.OperationalError, psycopg2.DatabaseError) as e:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   342
            self.logbuf.insert(self.logbuf.get_end_iter(), 'Error:\n' + str(e))
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   343
            return
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   344
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   345
        if state == psycopg2.extensions.POLL_OK:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   346
            self.execute_finish()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   347
        elif state == psycopg2.extensions.POLL_WRITE:
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   348
            GObject.io_add_watch(self.conn.fileno(), GObject.IO_OUT, self.execute_poll)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   349
        elif state == psycopg2.extensions.POLL_READ:
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   350
            GObject.io_add_watch(self.conn.fileno(), GObject.IO_IN, self.execute_poll)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   351
        else:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   352
            self.logbuf.insert(self.logbuf.get_end_iter(), "poll() returned %s" % state)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   353
            return
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   354
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   355
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   356
    def execute_finish(self):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   357
        t2 = time.time()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   358
        t = (t2 - self.t1)*1000
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   359
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   360
        self.throbber.clear()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   361
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   362
        self.logbuf.insert(self.logbuf.get_end_iter(),
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   363
            'Query successful (%d ms, %d rows)\n' % (t, self.curs.rowcount))
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   364
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   365
        # notices
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   366
        for n in self.conn.notices:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   367
            self.logbuf.insert(self.logbuf.get_end_iter(), n)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   368
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   369
        if self.curs.rowcount >= 0:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   370
            rows = self.curs.fetchall()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   371
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   372
            names = []
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   373
            for c in self.curs.description:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   374
                name = c[0]
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   375
                typename = self.get_typename(c[1], c[3])
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   376
                names += [(name, typename)]
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   377
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   378
            self.dataview.load_data(names, rows)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   379
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   380
        self.tb_execute.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   381
        if self.conn.get_transaction_status() == psycopg2.extensions.TRANSACTION_STATUS_INTRANS:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   382
            self.tb_commit.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   383
            self.tb_rollback.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   384
        else:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   385
            self.tb_connect.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   386
            self.tb_begin.set_sensitive(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   387
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   388
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   389
    def simulate_click(self, tb):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   390
        if tb.get_property('sensitive'):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   391
            tb.get_child().activate()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   392
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   393
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   394
    def keypress(self, w, event):
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   395
        keyname = Gdk.keyval_name(event.keyval)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   396
        if keyname == 'F1':
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   397
            self.simulate_click(self.tb_settings)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   398
            return True
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   399
        if keyname == 'F2':
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   400
            print(self.cb_server.popup())
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   401
            return True
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   402
        if keyname == 'F3':
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   403
            print(self.cb_dbname.popup())
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   404
            return True
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   405
        if keyname == 'F4':
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   406
            self.simulate_click(self.tb_connect)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   407
            return True
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   408
        if keyname == 'F5':
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   409
            self.simulate_click(self.tb_execute)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   410
            return True
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   411
        if keyname == 'F6':
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   412
            self.simulate_click(self.tb_begin)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   413
            return True
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   414
        if keyname == 'F7':
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   415
            self.simulate_click(self.tb_commit)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   416
            return True
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   417
        if keyname == 'F8':
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   418
            self.simulate_click(self.tb_rollback)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   419
            return True
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   420
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   421
        return False
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   422
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   423
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   424
    def toolbar_server_keypress(self, w, event):
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   425
        keyname = Gdk.keyval_name(event.keyval)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   426
        if keyname == 'Tab':
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   427
            self.populate_db_list()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   428
            self.cb_dbname.grab_focus()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   429
            return True
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   430
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   431
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   432
    def on_server_changed(self, w):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   433
        if self.cb_server.get_active() != -1:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   434
            self.populate_db_list()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   435
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   436
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   437
    def populate_db_list(self):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   438
        conninfo = self.get_conninfo(True)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   439
        try:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   440
            conn = self.db.get_conn(conninfo)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   441
        except DatabaseError as e:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   442
            self.logbuf.insert(self.logbuf.get_end_iter(), 'Error:\n%s\n' % e)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   443
            return
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   444
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   445
        curs = conn.cursor()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   446
        curs.execute('SELECT * FROM pg_catalog.pg_database WHERE NOT datistemplate ORDER BY 1')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   447
        psycopg2.extras.wait_select(conn)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   448
        rows = curs.fetchall()
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   449
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   450
        self.db.put_conn(conninfo, conn)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   451
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   452
        for i in range(self.cb_dbname.get_model().iter_n_children(None)):
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   453
            self.cb_dbname.remove(0)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   454
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   455
        for row in rows:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   456
            self.cb_dbname.append_text(row[0])
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   457
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   458
        self.cb_dbname.set_active(0)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   459
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   460
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   461
    def reload_server_list(self, sel=None):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   462
        # clean
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   463
        for i in range(self.cb_server.get_model().iter_n_children(None)):
76
3a41b351b122 Port pgconsole to Python3 + GTK3.
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   464
            self.cb_server.remove(0)
11
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   465
        self.cb_server.get_child().set_text('')
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   466
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   467
        try:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   468
            # populate
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   469
            for server in cfg.servers.server:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   470
                if str(server.name):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   471
                    title = '%s (%s)' % (str(server.name), str(server.host))
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   472
                else:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   473
                    title = str(server.host)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   474
                self.cb_server.append_text(title)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   475
        except AttributeError:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   476
            pass
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   477
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   478
        if not sel is None:
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   479
            self.cb_server.set_active(sel)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   480
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   481
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   482
    def settings(self, w):
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   483
        Settings(self)
bc69eca59041 PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   484