8 from pgtoolkit import pgbrowser |
8 from pgtoolkit import pgbrowser |
9 from pgtoolkit.toolbase import SimpleTool |
9 from pgtoolkit.toolbase import SimpleTool |
10 |
10 |
11 |
11 |
12 class MyApplication(Application, SimpleTool): |
12 class MyApplication(Application, SimpleTool): |
|
13 PLACEHOLDER = '__placeholder__' |
13 def __init__(self): |
14 def __init__(self): |
14 Application.__init__(self) |
15 Application.__init__(self) |
15 SimpleTool.__init__(self, name='browser', desc='PostgreSQL database browser.') |
16 SimpleTool.__init__(self, name='browser', desc='PostgreSQL database browser.') |
16 self.init() |
17 self.init() |
17 |
|
18 self.top.connect('keypress', self.globalkeypress) |
|
19 |
18 |
20 browser = pgbrowser.PgBrowser(self.pgm.get_conn('target')) |
19 self.top.connect('keypress', self.on_top_keypress) |
21 model = TreeModel() |
20 |
22 view = TreeView(model) |
21 self.browser = pgbrowser.PgBrowser(self.pgm.get_conn('target')) |
23 self.top.add(view) |
22 self.model = TreeModel() |
24 |
23 self.view = TreeView(self.model) |
25 # populate schemas |
24 self.view.connect('expand', self.on_view_expand) |
26 schemas = browser.list_schemas() |
25 self.top.add(self.view) |
27 model.add('/', [schema['name'] for schema in schemas if not schema['system']]) |
26 |
28 # populate tables |
|
29 for schema in schemas: |
|
30 if schema['system']: |
|
31 continue |
|
32 tables = browser.list_tables(schema=schema['name']) |
|
33 schemanode = '/'+schema['name'] |
|
34 model.add(schemanode, [table['name'] for table in tables]) |
|
35 view.collapse(schemanode) |
|
36 |
|
37 vert = VerticalLayout() |
27 vert = VerticalLayout() |
38 self.top.layout(vert) |
28 self.top.layout(vert) |
39 |
|
40 view.setfocus() |
|
41 |
29 |
42 def globalkeypress(self, keyname, char): |
30 self.populate_schemas() |
|
31 |
|
32 def populate_schemas(self): |
|
33 schemas = self.browser.list_schemas() |
|
34 schema_list = [schema['name'] for schema in schemas if not schema['system']] |
|
35 self.model.add('/', TreeNode('schemas', 'Schemas (%d)' % len(schema_list))) |
|
36 self.model.add('/schemas', schema_list) |
|
37 for schema_name in schema_list: |
|
38 path = '/schemas/' + schema_name |
|
39 self.model.add(path, self.PLACEHOLDER) |
|
40 self.view.collapse(path) |
|
41 |
|
42 def populate_tables(self, node, schema_name): |
|
43 """Read table list, put in node. |
|
44 |
|
45 Node is replaced by 'Tables (N)' node, |
|
46 tables are created as its children. |
|
47 |
|
48 """ |
|
49 tables = self.browser.list_tables(schema=schema_name) |
|
50 table_list = [table['name'] for table in tables] |
|
51 node.name = 'tables' |
|
52 node.title = 'Tables (%d)' % len(table_list) |
|
53 for table_name in table_list: |
|
54 node.add(TreeNode(table_name)) |
|
55 |
|
56 def on_view_expand(self, node): |
|
57 if node.parent.name == 'schemas' and node.children[0].name == self.PLACEHOLDER: |
|
58 schema_name = node.name |
|
59 self.populate_tables(node.children[0], schema_name) |
|
60 |
|
61 def on_top_keypress(self, keyname, char): |
43 if keyname == 'escape': |
62 if keyname == 'escape': |
44 self.terminate() |
63 self.terminate() |
45 |
64 |
46 |
65 |
47 if __name__ == '__main__': |
66 if __name__ == '__main__': |