| 1 | #!/usr/bin/python
|
|---|
| 2 | #
|
|---|
| 3 | # Browse a Python dictionary in a two pane graphical interface written
|
|---|
| 4 | # in GTK.
|
|---|
| 5 | #
|
|---|
| 6 | # The GtkDictBrowser class is supposed to be generic enough to allow
|
|---|
| 7 | # applications to override enough methods and produce a
|
|---|
| 8 | # domain-specific browser provided the information is presented as a
|
|---|
| 9 | # Python dictionary.
|
|---|
| 10 | #
|
|---|
| 11 | # Possible applications:
|
|---|
| 12 | #
|
|---|
| 13 | # - Windows registry browser
|
|---|
| 14 | # - SPOOLSS printerdata browser
|
|---|
| 15 | # - tdb file browser
|
|---|
| 16 | #
|
|---|
| 17 |
|
|---|
| 18 | from gtk import *
|
|---|
| 19 | import string, re
|
|---|
| 20 |
|
|---|
| 21 | class GtkDictBrowser:
|
|---|
| 22 |
|
|---|
| 23 | def __init__(self, dict):
|
|---|
| 24 | self.dict = dict
|
|---|
| 25 |
|
|---|
| 26 | # This variable stores a list of (regexp, function) used to
|
|---|
| 27 | # convert the raw value data to a displayable string.
|
|---|
| 28 |
|
|---|
| 29 | self.get_value_text_fns = []
|
|---|
| 30 | self.get_key_text = lambda x: x
|
|---|
| 31 |
|
|---|
| 32 | # We can filter the list of keys displayed using a regex
|
|---|
| 33 |
|
|---|
| 34 | self.filter_regex = ""
|
|---|
| 35 |
|
|---|
| 36 | # Create and configure user interface widgets. A string argument is
|
|---|
| 37 | # used to set the window title.
|
|---|
| 38 |
|
|---|
| 39 | def build_ui(self, title):
|
|---|
| 40 | win = GtkWindow()
|
|---|
| 41 | win.set_title(title)
|
|---|
| 42 |
|
|---|
| 43 | win.connect("destroy", mainquit)
|
|---|
| 44 |
|
|---|
| 45 | hpaned = GtkHPaned()
|
|---|
| 46 | win.add(hpaned)
|
|---|
| 47 | hpaned.set_border_width(5)
|
|---|
| 48 | hpaned.show()
|
|---|
| 49 |
|
|---|
| 50 | vbox = GtkVBox()
|
|---|
| 51 | hpaned.add1(vbox)
|
|---|
| 52 | vbox.show()
|
|---|
| 53 |
|
|---|
| 54 | scrolled_win = GtkScrolledWindow()
|
|---|
| 55 | scrolled_win.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
|
|---|
| 56 | vbox.pack_start(scrolled_win)
|
|---|
| 57 | scrolled_win.show()
|
|---|
| 58 |
|
|---|
| 59 | hbox = GtkHBox()
|
|---|
| 60 | vbox.pack_end(hbox, expand = 0, padding = 5)
|
|---|
| 61 | hbox.show()
|
|---|
| 62 |
|
|---|
| 63 | label = GtkLabel("Filter:")
|
|---|
| 64 | hbox.pack_start(label, expand = 0, padding = 5)
|
|---|
| 65 | label.show()
|
|---|
| 66 |
|
|---|
| 67 | self.entry = GtkEntry()
|
|---|
| 68 | hbox.pack_end(self.entry, padding = 5)
|
|---|
| 69 | self.entry.show()
|
|---|
| 70 |
|
|---|
| 71 | self.entry.connect("activate", self.filter_activated)
|
|---|
| 72 |
|
|---|
| 73 | self.list = GtkList()
|
|---|
| 74 | self.list.set_selection_mode(SELECTION_MULTIPLE)
|
|---|
| 75 | self.list.set_selection_mode(SELECTION_BROWSE)
|
|---|
| 76 | scrolled_win.add_with_viewport(self.list)
|
|---|
| 77 | self.list.show()
|
|---|
| 78 |
|
|---|
| 79 | self.list.connect("select_child", self.key_selected)
|
|---|
| 80 |
|
|---|
| 81 | scrolled_win = GtkScrolledWindow()
|
|---|
| 82 | scrolled_win.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
|
|---|
| 83 | hpaned.add2(scrolled_win)
|
|---|
| 84 | scrolled_win.set_usize(500,400)
|
|---|
| 85 | scrolled_win.show()
|
|---|
| 86 |
|
|---|
| 87 | self.text = GtkText()
|
|---|
| 88 | self.text.set_editable(FALSE)
|
|---|
| 89 | scrolled_win.add_with_viewport(self.text)
|
|---|
| 90 | self.text.show()
|
|---|
| 91 |
|
|---|
| 92 | self.text.connect("event", self.event_handler)
|
|---|
| 93 |
|
|---|
| 94 | self.menu = GtkMenu()
|
|---|
| 95 | self.menu.show()
|
|---|
| 96 |
|
|---|
| 97 | self.font = load_font("fixed")
|
|---|
| 98 |
|
|---|
| 99 | self.update_keylist()
|
|---|
| 100 |
|
|---|
| 101 | win.show()
|
|---|
| 102 |
|
|---|
| 103 | # Add a key to the left hand side of the user interface
|
|---|
| 104 |
|
|---|
| 105 | def add_key(self, key):
|
|---|
| 106 | display_key = self.get_key_text(key)
|
|---|
| 107 | list_item = GtkListItem(display_key)
|
|---|
| 108 | list_item.set_data("raw_key", key) # Store raw key in item data
|
|---|
| 109 | self.list.add(list_item)
|
|---|
| 110 | list_item.show()
|
|---|
| 111 |
|
|---|
| 112 | # Event handler registered by build_ui()
|
|---|
| 113 |
|
|---|
| 114 | def event_handler(self, event, menu):
|
|---|
| 115 | return FALSE
|
|---|
| 116 |
|
|---|
| 117 | # Set the text to appear in the right hand side of the user interface
|
|---|
| 118 |
|
|---|
| 119 | def set_value_text(self, item):
|
|---|
| 120 |
|
|---|
| 121 | # Clear old old value in text window
|
|---|
| 122 |
|
|---|
| 123 | self.text.delete_text(0, self.text.get_length())
|
|---|
| 124 |
|
|---|
| 125 | if type(item) == str:
|
|---|
| 126 |
|
|---|
| 127 | # The text widget has trouble inserting text containing NULL
|
|---|
| 128 | # characters.
|
|---|
| 129 |
|
|---|
| 130 | item = string.replace(item, "\x00", ".")
|
|---|
| 131 |
|
|---|
| 132 | self.text.insert(self.font, None, None, item)
|
|---|
| 133 |
|
|---|
|
|---|