54
68
#code for other initialization actions should be added here
70
# references to glade widgets
72
self.editor = self.builder.get_object("editor")
73
self.execute_button = self.builder.get_object("execute_buton")
74
self.copy_button = self.builder.get_object("copy_buton")
75
self.categories_combo = self.builder.get_object("categories_combo")
76
self.snippets_tv = self.builder.get_object("snippets_tv")
77
self.description_label = self.builder.get_object("description_label")
78
self.location_label = self.builder.get_object("location_label")
80
self.snippetsdir = "/home/jono/source/snippets"
81
self.snippetsfiles = []
82
self.snippetsdata = []
84
# set up the tree view
86
self.snippets_model = gtk.ListStore(str, str, str, str)
87
self.snippets_tv.set_model(self.snippets_model)
88
self.tvcolumn = gtk.TreeViewColumn("Available Snippets")
89
self.cellpb = gtk.CellRendererPixbuf()
90
self.cell_name = gtk.CellRendererText()
91
self.tvcolumn.pack_start(self.cellpb, False)
92
self.tvcolumn.pack_start(self.cell_name, True)
93
self.tvcolumn.set_attributes(self.cellpb, stock_id=0)
94
self.tvcolumn.set_attributes(self.cell_name, text=1)
95
self.snippets_tv.append_column(self.tvcolumn)
97
# set up categories combo box
98
self.categories_liststore = gtk.ListStore(str)
99
self.categories_combo.set_model(self.categories_liststore)
100
title_cell = gtk.CellRendererText()
101
self.categories_combo.pack_start(title_cell, True)
102
self.categories_combo.add_attribute(title_cell, 'text', 0)
104
# set up some variables for the current snippet
105
self.current_name = None
106
self.current_filename = None
107
self.current_description = None
109
self.get_snippets_file_list()
111
self.update_categories_combo()
113
def update_categories_combo(self):
114
"""Update the categories combo box with categories from across the snippets."""
118
for snippet in self.snippetsdata:
120
splitlist = item['cats'].split(',')
122
if len(finalcats) == 0:
133
templist.append("All")
134
self.categories_liststore.append(templist)
139
self.categories_liststore.append(templist)
141
self.categories_combo.set_active(0)
143
def select_combo_category(self, widget, data=None):
144
"""Select an item from the category combo box."""
146
cat = self.categories_combo.get_active_text()
147
self.update_snippets_treeview(cat)
150
def snippet_selected(self, widget, data=None):
151
"""Snippet is selected from the treeview"""
153
selection = widget.get_selection()
154
(model, it) = selection.get_selected()
156
# set the current snippet variables
157
self.current_name = self.snippets_model.get_value(it, 1)
158
self.current_filename = self.snippets_model.get_value(it, 2)
159
self.current_description = self.snippets_model.get_value(it, 3)
161
# read in the snippet
162
file_object = open(os.path.join(self.snippetsdir, self.current_filename))
165
all_the_text = file_object.read()
169
lines = all_the_text.splitlines()
172
buf = self.editor.get_buffer()
173
(start, end) = buf.get_bounds()
175
buf.delete(start, end)
180
self.editor.get_buffer().insert(self.editor.get_buffer().get_end_iter(), l)
181
self.editor.get_buffer().insert(self.editor.get_buffer().get_end_iter(), "\n")
183
self.location_label.set_text(self.snippetsdir + "/" + self.current_filename)
184
self.description_label.set_text(self.current_description)
186
def run_snippet(self, widget, data=None):
187
"""Run the currently selected snippet"""
189
fn = os.path.join(self.snippetsdir, self.current_filename)
190
subprocess.Popen(['python', fn])
192
def get_snippets_file_list(self):
193
"""Read in the snippets directory and put all Python files into a list."""
195
dirlist = os.walk(self.snippetsdir)
197
for path, subdirs, files in dirlist:
199
if fnmatch.fnmatch(f, "*.py"):
200
self.snippetsfiles.append(f)
202
self.scan_for_metadata()
204
def scan_for_metadata(self):
205
"""Scan snippets files for meta data."""
207
for f in self.snippetsfiles:
208
file_object = open(os.path.join(self.snippetsdir, f))
211
all_the_text = file_object.read()
215
lines = all_the_text.splitlines()
221
name = re.search(r'\[SNIPPET_NAME: (.*?)]', l)
222
cats = re.search(r'\[SNIPPET_CATEGORIES: (.*?)]', l)
223
description = re.search(r'\[SNIPPET_DESCRIPTION: (.*?)]', l)
226
tempdict['name'] = name.groups()[0]
229
tempdict['cats'] = cats.groups()[0]
231
if description is not None:
232
tempdict['description'] = description.groups()[0]
235
tempdict['filename'] = f
236
templist.append(tempdict)
237
self.snippetsdata.append(templist)
239
self.update_snippets_treeview("All")
242
def update_snippets_treeview(self, category):
243
"""Update snippets treeview with available snippets."""
247
# clear the tree view for when we change the current category
248
self.snippets_model.clear()
250
if category == "All":
251
for i in self.snippetsdata:
253
templist.append(gtk.STOCK_FILE)
254
templist.append(item['name'])
255
templist.append(item['filename'])
256
templist.append(item['description'])
258
self.snippets_model.append(templist)
262
for i in self.snippetsdata:
264
if category in item['cats']:
265
templist.append(gtk.STOCK_FILE)
266
templist.append(item['name'])
267
templist.append(item['filename'])
268
templist.append(item['description'])
270
self.snippets_model.append(templist)
56
275
def about(self, widget, data=None):
57
276
"""about - display the about box for acire """
58
277
about = AboutAcireDialog.NewAboutAcireDialog()