~jonobacon/acire/trunk

« back to all changes in this revision

Viewing changes to bin/acire

  • Committer: Jono Bacon
  • Date: 2009-12-31 00:35:03 UTC
  • Revision ID: jono@ubuntu.com-20091231003503-1y1lwp0whpjwhwq9
First code for the repo.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
# -*- coding: utf-8 -*-
3
3
### BEGIN LICENSE
4
 
# This file is in the public domain
 
4
# Copyright (C) 2009 Jono Bacon <jono@ubuntu.com>
 
5
#This program is free software: you can redistribute it and/or modify it 
 
6
#under the terms of the GNU General Public License version 3, as published 
 
7
#by the Free Software Foundation.
 
8
#
 
9
#This program is distributed in the hope that it will be useful, but 
 
10
#WITHOUT ANY WARRANTY; without even the implied warranties of 
 
11
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
12
#PURPOSE.  See the GNU General Public License for more details.
 
13
#
 
14
#You should have received a copy of the GNU General Public License along 
 
15
#with this program.  If not, see <http://www.gnu.org/licenses/>.
5
16
### END LICENSE
6
17
 
7
18
import sys
8
19
import os
9
20
import gtk
 
21
import fnmatch
 
22
import re
 
23
import subprocess
10
24
 
11
25
# Check if we are working in the source tree or from the installed 
12
26
# package and mangle the python path accordingly
53
67
 
54
68
        #code for other initialization actions should be added here
55
69
 
 
70
        # references to glade widgets
 
71
 
 
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")
 
79
 
 
80
        self.snippetsdir = "/home/jono/source/snippets"
 
81
        self.snippetsfiles = []
 
82
        self.snippetsdata = []
 
83
 
 
84
        # set up the tree view
 
85
 
 
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)
 
96
 
 
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)
 
103
 
 
104
        # set up some variables for the current snippet
 
105
        self.current_name = None
 
106
        self.current_filename = None
 
107
        self.current_description = None
 
108
 
 
109
        self.get_snippets_file_list()
 
110
 
 
111
        self.update_categories_combo()
 
112
 
 
113
    def update_categories_combo(self):
 
114
        """Update the categories combo box with categories from across the snippets."""                
 
115
 
 
116
        finalcats = []
 
117
        
 
118
        for snippet in self.snippetsdata:
 
119
            for item in snippet:
 
120
                splitlist = item['cats'].split(',')
 
121
                for s in splitlist:
 
122
                    if len(finalcats) == 0:
 
123
                        finalcats.append(s)
 
124
                    else:
 
125
                        for c in finalcats:
 
126
                            if c != s:
 
127
                                finalcats.append(s)
 
128
                                break
 
129
                            else:
 
130
                                break
 
131
 
 
132
        templist = []
 
133
        templist.append("All")
 
134
        self.categories_liststore.append(templist)
 
135
            
 
136
        for i in finalcats:
 
137
            templist = []
 
138
            templist.append(i)
 
139
            self.categories_liststore.append(templist)
 
140
 
 
141
        self.categories_combo.set_active(0)
 
142
 
 
143
    def select_combo_category(self, widget, data=None):
 
144
        """Select an item from the category combo box."""
 
145
 
 
146
        cat = self.categories_combo.get_active_text()
 
147
        self.update_snippets_treeview(cat)
 
148
 
 
149
                            
 
150
    def snippet_selected(self, widget, data=None):
 
151
        """Snippet is selected from the treeview"""
 
152
        
 
153
        selection = widget.get_selection()
 
154
        (model, it) = selection.get_selected()
 
155
 
 
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)
 
160
 
 
161
        # read in the snippet
 
162
        file_object = open(os.path.join(self.snippetsdir, self.current_filename))
 
163
 
 
164
        try:
 
165
            all_the_text = file_object.read()
 
166
        finally:
 
167
            file_object.close()
 
168
 
 
169
        lines  = all_the_text.splitlines()
 
170
 
 
171
        # clear the textview
 
172
        buf = self.editor.get_buffer()
 
173
        (start, end) = buf.get_bounds()
 
174
 
 
175
        buf.delete(start, end)
 
176
        self.editor.show()
 
177
 
 
178
        # show the code                
 
179
        for l in lines:
 
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")
 
182
            
 
183
        self.location_label.set_text(self.snippetsdir + "/" + self.current_filename)
 
184
        self.description_label.set_text(self.current_description)
 
185
 
 
186
    def run_snippet(self, widget, data=None):
 
187
        """Run the currently selected snippet"""
 
188
 
 
189
        fn = os.path.join(self.snippetsdir, self.current_filename)
 
190
        subprocess.Popen(['python', fn])
 
191
                                
 
192
    def get_snippets_file_list(self):
 
193
        """Read in the snippets directory and put all Python files into a list."""
 
194
 
 
195
        dirlist = os.walk(self.snippetsdir)
 
196
 
 
197
        for path, subdirs, files in dirlist:
 
198
            for f in files:
 
199
                if fnmatch.fnmatch(f, "*.py"):
 
200
                    self.snippetsfiles.append(f)
 
201
 
 
202
        self.scan_for_metadata()
 
203
        
 
204
    def scan_for_metadata(self):
 
205
        """Scan snippets files for meta data."""
 
206
        
 
207
        for f in self.snippetsfiles:
 
208
            file_object = open(os.path.join(self.snippetsdir, f))
 
209
 
 
210
            try:
 
211
                all_the_text = file_object.read()
 
212
            finally:
 
213
                file_object.close()
 
214
 
 
215
            lines  = all_the_text.splitlines()
 
216
 
 
217
            tempdict = {}
 
218
            templist = []
 
219
            
 
220
            for l in lines:
 
221
                name = re.search(r'\[SNIPPET_NAME: (.*?)]', l)
 
222
                cats = re.search(r'\[SNIPPET_CATEGORIES: (.*?)]', l)    
 
223
                description = re.search(r'\[SNIPPET_DESCRIPTION: (.*?)]', l)
 
224
 
 
225
                if name is not None:
 
226
                    tempdict['name'] = name.groups()[0]
 
227
 
 
228
                if cats is not None:
 
229
                    tempdict['cats'] = cats.groups()[0]
 
230
 
 
231
                if description is not None:
 
232
                    tempdict['description'] = description.groups()[0]
 
233
 
 
234
            if tempdict:
 
235
                tempdict['filename'] = f            
 
236
                templist.append(tempdict)
 
237
                self.snippetsdata.append(templist)
 
238
 
 
239
        self.update_snippets_treeview("All")
 
240
 
 
241
                
 
242
    def update_snippets_treeview(self, category):
 
243
        """Update snippets treeview with available snippets."""
 
244
 
 
245
        templist = []        
 
246
 
 
247
        # clear the tree view for when we change the current category
 
248
        self.snippets_model.clear()
 
249
 
 
250
        if category == "All":
 
251
            for i in self.snippetsdata:
 
252
                for item in i:
 
253
                    templist.append(gtk.STOCK_FILE)
 
254
                    templist.append(item['name'])
 
255
                    templist.append(item['filename'])
 
256
                    templist.append(item['description'])                
 
257
 
 
258
                    self.snippets_model.append(templist)
 
259
 
 
260
                    templist = []
 
261
        else:
 
262
            for i in self.snippetsdata:
 
263
                for item in i:
 
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'])                
 
269
 
 
270
                        self.snippets_model.append(templist)
 
271
 
 
272
                        templist = []            
 
273
 
 
274
                        
56
275
    def about(self, widget, data=None):
57
276
        """about - display the about box for acire """
58
277
        about = AboutAcireDialog.NewAboutAcireDialog()
78
297
 
79
298
        gtk.main_quit()
80
299
 
 
300
    def all_files(root, patterns='*', single_level=False, yield_folders=False):
 
301
        # Expand patterns from semicolon-separated string to list
 
302
        patterns = patterns.split(';')
 
303
        for path, subdirs, files in os.walk(root):
 
304
            if yield_folders:
 
305
                files.extend(subdirs)
 
306
            files.sort( )
 
307
            for name in files:
 
308
                for pattern in patterns:
 
309
                    if fnmatch.fnmatch(name, pattern):
 
310
                        yield os.path.join(path, name)
 
311
                        break
 
312
            if single_level:
 
313
                break
 
314
 
 
315
 
81
316
def NewAcireWindow():
82
317
    """NewAcireWindow - returns a fully instantiated
83
318
    AcireWindow object. Use this function rather than