browser.py
author Radek Brich <radek.brich@devl.cz>
Wed, 09 Jul 2014 18:04:11 +0200
changeset 105 10551741f61f
parent 104 d8ff52a0390f
permissions -rwxr-xr-x
Move batchquery, listtables tools under pgtool.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
     1
#!/usr/bin/env python3
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
     2
# -*- coding: utf-8 -*-
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
     3
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
     4
import locale
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
     5
locale.setlocale(locale.LC_ALL, '')
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
     6
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
     7
from tuikit import *
104
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
     8
from pydbkit import pgbrowser
d8ff52a0390f Rename to pydbkit.
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
     9
from pydbkit.toolbase import SimpleTool
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    10
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    11
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    12
class MyApplication(Application, SimpleTool):
72
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    13
57
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    14
    PLACEHOLDER = '__placeholder__'
72
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    15
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    16
    def __init__(self):
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    17
        Application.__init__(self)
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    18
        SimpleTool.__init__(self, name='browser', desc='PostgreSQL database browser.')
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    19
        self.init()
57
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    20
72
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    21
        self.top.add_handler('keypress', self.on_top_keypress)
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    22
57
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    23
        self.browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    24
        self.model = TreeModel()
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    25
        self.view = TreeView(self.model)
72
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    26
        self.view.add_handler('expand', self.on_view_expand)
57
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    27
72
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    28
        scroll = ScrollView()
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    29
        scroll.add(self.view)
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    30
        self.top.add(scroll, halign='fill', valign='fill')
57
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    31
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    32
        self.populate_schemas()
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    33
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    34
    def populate_schemas(self):
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    35
        schemas = self.browser.list_schemas()
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    36
        schema_list = [schema['name'] for schema in schemas if not schema['system']]
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    37
        self.model.add('/', TreeNode('schemas', 'Schemas (%d)' % len(schema_list)))
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    38
        self.model.add('/schemas', schema_list)
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    39
        for schema_name in schema_list:
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    40
            path = '/schemas/' + schema_name
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    41
            self.model.add(path, self.PLACEHOLDER)
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    42
            self.view.collapse(path)
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    43
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    44
    def populate_tables(self, node, schema_name):
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    45
        """Read table list, put in node.
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    46
57
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    47
        Node is replaced by 'Tables (N)' node,
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    48
        tables are created as its children.
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    49
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    50
        """
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    51
        tables = self.browser.list_tables(schema=schema_name)
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    52
        table_list = [table['name'] for table in tables]
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    53
        node.name = 'tables'
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    54
        node.title = 'Tables (%d)' % len(table_list)
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    55
        for table_name in table_list:
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    56
            node.add(TreeNode(table_name))
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    57
72
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    58
    def on_view_expand(self, ev):
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    59
        if ev.node.parent.name == 'schemas' and ev.node.children[0].name == self.PLACEHOLDER:
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    60
            schema_name = ev.node.name
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    61
            self.populate_tables(ev.node.children[0], schema_name)
57
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    62
72
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    63
    def on_top_keypress(self, ev):
539c2dd47f3b Update browser tool to work with current Tuikit. Add RPM's BuildRequires to setup.cfg.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    64
        if ev.keyname == 'escape':
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    65
            self.terminate()
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    66
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    67
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    68
if __name__ == '__main__':
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    69
    app = MyApplication()
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    70
    app.start()
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    71