browser.py
author Radek Brich <radek.brich@devl.cz>
Mon, 05 Mar 2012 18:36:46 +0100
changeset 31 c2e6e24b83d9
parent 2 gtkbrowser.py@efee419b7a2d
child 57 ba323bbed6a4
permissions -rwxr-xr-x
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.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import locale
locale.setlocale(locale.LC_ALL, '')

from tuikit import *
from pgtoolkit import pgbrowser
from pgtoolkit.toolbase import SimpleTool


class MyApplication(Application, SimpleTool):
    def __init__(self):
        Application.__init__(self)
        SimpleTool.__init__(self, name='browser', desc='PostgreSQL database browser.')
        self.init()
        
        self.top.connect('keypress', self.globalkeypress)

        browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
        model = TreeModel()
        view = TreeView(model)
        self.top.add(view)
        
        # populate schemas
        schemas = browser.list_schemas()
        model.add('/',  [schema['name'] for schema in schemas if not schema['system']])
        # populate tables
        for schema in schemas:
            if schema['system']:
                continue
            tables = browser.list_tables(schema=schema['name'])
            schemanode = '/'+schema['name']
            model.add(schemanode,  [table['name'] for table in tables])
            view.collapse(schemanode)        
        
        vert = VerticalLayout()
        self.top.layout(vert)
        
        view.setfocus()

    def globalkeypress(self, keyname, char):
        if keyname == 'escape':
            self.terminate()


if __name__ == '__main__':
    app = MyApplication()
    app.start()