browser.py
author Radek Brich <radek.brich@devl.cz>
Tue, 26 Feb 2013 14:40:39 +0100
changeset 68 b0d972be2631
parent 57 ba323bbed6a4
child 72 539c2dd47f3b
permissions -rwxr-xr-x
Add listtables tool.
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 *
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
     8
from pgtoolkit import pgbrowser
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
     9
from pgtoolkit.toolbase import SimpleTool
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):
57
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    13
    PLACEHOLDER = '__placeholder__'
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
    14
    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
    15
        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
    16
        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
    17
        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
    18
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    19
        self.top.connect('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
    20
57
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    21
        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
    22
        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
    23
        self.view = TreeView(self.model)
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.view.connect('expand', self.on_view_expand)
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.top.add(self.view)
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    26
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
    27
        vert = VerticalLayout()
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
    28
        self.top.layout(vert)
57
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    29
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    30
        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
    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
    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
    33
        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
    34
        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
    35
        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
    36
        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
    37
        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
    38
            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
    39
            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
    40
            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
    41
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    42
    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
    43
        """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
    44
57
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    45
        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
    46
        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
    47
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    48
        """
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    49
        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
    50
        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
    51
        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
    52
        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
    53
        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
    54
            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
    55
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    56
    def on_view_expand(self, node):
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    57
        if node.parent.name == 'schemas' and node.children[0].name == 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
    58
            schema_name = node.name
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    59
            self.populate_tables(node.children[0], 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
    60
ba323bbed6a4 Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    61
    def on_top_keypress(self, keyname, char):
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
    62
        if keyname == 'escape':
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
    63
            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
    64
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
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
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
    67
    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
    68
    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
    69