# HG changeset patch # User Radek Brich # Date 1355775124 -3600 # Node ID ba323bbed6a436810b1f7f038bc59d3e950e7a82 # Parent 94e091c23ebbc45ae8a26e1ca392a47472e5057d Update browser tool: Add Schemas and Tables nodes, populate nodes when expanded. diff -r 94e091c23ebb -r ba323bbed6a4 browser.py --- a/browser.py Mon Dec 17 16:48:12 2012 +0100 +++ b/browser.py Mon Dec 17 21:12:04 2012 +0100 @@ -10,36 +10,55 @@ class MyApplication(Application, SimpleTool): + PLACEHOLDER = '__placeholder__' def __init__(self): Application.__init__(self) SimpleTool.__init__(self, name='browser', desc='PostgreSQL database browser.') self.init() - - self.top.connect('keypress', self.globalkeypress) + + self.top.connect('keypress', self.on_top_keypress) - 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) - + self.browser = pgbrowser.PgBrowser(self.pgm.get_conn('target')) + self.model = TreeModel() + self.view = TreeView(self.model) + self.view.connect('expand', self.on_view_expand) + self.top.add(self.view) + vert = VerticalLayout() self.top.layout(vert) - - view.setfocus() + + self.populate_schemas() + + def populate_schemas(self): + schemas = self.browser.list_schemas() + schema_list = [schema['name'] for schema in schemas if not schema['system']] + self.model.add('/', TreeNode('schemas', 'Schemas (%d)' % len(schema_list))) + self.model.add('/schemas', schema_list) + for schema_name in schema_list: + path = '/schemas/' + schema_name + self.model.add(path, self.PLACEHOLDER) + self.view.collapse(path) + + def populate_tables(self, node, schema_name): + """Read table list, put in node. - def globalkeypress(self, keyname, char): + Node is replaced by 'Tables (N)' node, + tables are created as its children. + + """ + tables = self.browser.list_tables(schema=schema_name) + table_list = [table['name'] for table in tables] + node.name = 'tables' + node.title = 'Tables (%d)' % len(table_list) + for table_name in table_list: + node.add(TreeNode(table_name)) + + def on_view_expand(self, node): + if node.parent.name == 'schemas' and node.children[0].name == self.PLACEHOLDER: + schema_name = node.name + self.populate_tables(node.children[0], schema_name) + + def on_top_keypress(self, keyname, char): if keyname == 'escape': self.terminate()