~clicompanion-devs/clicompanion/clicomp-glade

« back to all changes in this revision

Viewing changes to clicompanion.0.0.7.py

  • Committer: duanedesign
  • Date: 2010-07-28 09:50:54 UTC
  • Revision ID: duanedesign@gmail.com-20100728095054-2ovs9xmorru1rxn7
updated with latest changes from v1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#
4
4
# clicompanion.py - commandline tool.
5
5
#
6
 
# Copyright 2010 Duane Hinnen
 
6
# Copyright 2010 Duane Hinnen, Kenny Meyer
7
7
#
8
8
# This program is free software: you can redistribute it and/or modify it
9
9
# under the terms of the GNU General Public License version 3, as published
20
20
# IMPORTANT: you need to move the .cheatsheet file to your $HOME
21
21
#
22
22
import os
23
 
import webbrowser
24
 
 
25
23
import gtk
26
24
import pygtk
27
25
import vte
28
26
 
29
27
pygtk.require('2.0')
30
28
 
31
 
states = []
32
 
row = ''
 
29
STATES = []
 
30
ROW = ''
33
31
#text=""
34
 
cheatsheet = os.path.expanduser("~/.clicompanion")
 
32
CHEATSHEET = os.path.expanduser("~/.clicompanion")
 
33
CONFIG_ORIG = "/etc/clicompanion.d/clicompanion.config"
35
34
 
36
35
 
37
36
class Companion:
38
37
 
39
 
    # create the terminal
 
38
    # create the terminal and set its size
40
39
    vte = vte.Terminal()
41
40
    vte.set_size_request(700, 350)
42
41
    # fork_command() will run a command, in this case it shows a prompt
43
42
    vte.fork_command('bash')
44
 
 
 
43
    
 
44
    #copy config file to user $HOME if does not exist
 
45
    def setup(self):
 
46
        if not os.path.isfile (CHEATSHEET):
 
47
            os.system ("cp %s %s" % (CONFIG_ORIG, CHEATSHEET))
 
48
            
45
49
    # close the window and quit
46
50
    def delete_event(self, widget,  data=None):
47
51
        gtk.main_quit()
50
54
    # Info Dialog Box    
51
55
    # if a command needs more info EX: a package name
52
56
    def get_info(self, widget, data=None):
53
 
        global row
54
 
        row_int = int(row[0][0])
 
57
        global ROW
 
58
        row_int = int(ROW[0][0])
55
59
 
56
60
        # Create Dialog object
57
61
        dialog = gtk.MessageDialog(
92
96
    def responseToDialog(self, text, dialog, response):
93
97
            dialog.response(response)
94
98
        
95
 
        # Add command dialog box
 
99
    # Add command dialog box
96
100
    def add_command(self, widget, data=None):
97
101
        # Create Dialog object
98
102
        dialog = gtk.MessageDialog(
102
106
            gtk.BUTTONS_OK,
103
107
            None)
104
108
 
105
 
        # ask for input. Use column 2 for what is required
 
109
        # primaary text
106
110
        dialog.set_markup("Add a command to your clicompanion dictionary")
107
111
 
108
112
        #create the text input field
130
134
        #add it and show it
131
135
        dialog.vbox.pack_end(hbox2, True, True, 0)
132
136
        dialog.vbox.pack_end(hbox1, True, True, 0)
133
 
 
134
137
        dialog.show_all()
135
 
 
136
138
        # Show the dialog
137
139
        dialog.run()
138
140
        
140
142
        text1 = entry1.get_text()
141
143
        text2 = entry2.get_text()
142
144
        text3 = entry3.get_text()
143
 
        # open flat file that contains the command dictionary
144
 
        with open(cheatsheet, "a") as cheatfile:
145
 
            cheatfile.write(text1+" :"+text2+" : "+text3+'\n')
146
 
            cheatfile.close()
147
 
            self.update()
148
 
            
 
145
        
 
146
        # open flat file that contains the commands and add the new command
 
147
        with open(CHEATSHEET, "a") as cheatfile:
 
148
            if text1 != "":
 
149
                cheatfile.write(text1+" :"+text2+" : "+text3+'\n')
 
150
                cheatfile.close()
 
151
                l = str(text1+" :"+text2+" : "+text3)
 
152
                ls = l.split(':',2)
 
153
                STATES.append(ls[0])
 
154
                self.liststore.append([ls[0],ls[1],ls[2]])
 
155
            #self.update()
 
156
          
149
157
        # The destroy method must be called otherwise the 'Close' button will
150
158
        # not work.
151
159
        dialog.destroy()
152
160
        #return text
153
161
        
154
 
        # Remove command method 
 
162
        # Remove command from command file and GUI 
155
163
    def remove_command(self, widget, data=None):
156
 
        row_int = int(row[0][0]) #convert pathlist into something usable    
 
164
        row_int = int(ROW[0][0]) #convert pathlist into something usable    
157
165
        del self.liststore[row_int] #remove line from list
158
166
        
159
167
        # open command file and delete line so the change is persistent
160
 
        with open(cheatsheet, "r") as cheatfile:
 
168
        with open(CHEATSHEET, "r") as cheatfile:
161
169
            cheatlines = cheatfile.readlines()
162
170
            del cheatlines[row_int]
163
171
            cheatfile.close()
164
 
        with open(cheatsheet, "w") as cheatfile2:           
 
172
        with open(CHEATSHEET, "w") as cheatfile2:           
165
173
            cheatfile2.writelines(cheatlines)
166
174
            cheatfile2.close()
167
 
    
 
175
 
168
176
    def _filter_commands(self, widget, data=None):
169
177
        """Show commands matching a given search term.
170
178
        
171
179
        The user should enter a term in the search box and the treeview should
172
180
        only display the rows which contain the search term. Pretty
173
 
        straight-forward.
 
181
        straight-forward. DH: Maybe for a smart guy like you ;)
174
182
        """
175
183
        
176
184
        # Get the text from the search box
189
197
 
190
198
        self.update()
191
199
 
 
200
 
 
201
        
192
202
    #send the command to the terminal
193
203
    def run_command(self, widget, data=None):
194
 
        #global row
 
204
        global ROW
195
205
        text = ""
196
 
        row_int = int(row[0][0]) # removes everything but number. Before: [5,]
 
206
        row_int = int(ROW[0][0]) # removes everything but number from EX: [5,]
197
207
        
198
 
        cmnd = states[row_int] #states is where commands are stored
 
208
        cmnd = STATES[row_int] #STATES is where commands are stored
199
209
        if not self.liststore[row_int][1] == " ": # command with user input
200
210
            text = Companion.get_info(self, text)
201
211
            #print text #debug
206
216
            Companion.vte.show()
207
217
            Companion.vte.grab_focus()
208
218
     
209
 
     #open the website that is the help file
210
 
    def open_site(self, widget, data=None):
211
 
        siteOpen = webbrowser.open("http://okiebuntu.homelinux.com")
212
 
        return siteOpen
213
 
      
 
219
     #open the man page for selected command
 
220
    def man_page(self, widget, data=None):
 
221
        row_int = int(ROW[0][0]) # removes everything but number from EX: [5,]
 
222
        cmnd = STATES[row_int] #STATES is where commands are store
 
223
        splitcommand=cmnd.split(" ")
 
224
        Companion.vte.feed_child("man "+splitcommand[0]+"\n") #send command
 
225
        Companion.vte.show()
 
226
 
 
227
        
214
228
    # open file containing command dictionary and put it in a variable
215
229
    def update(self):
216
 
        with open(cheatsheet, "r") as cheatfile:
 
230
        with open(CHEATSHEET, "r") as cheatfile:
217
231
            bugdata=cheatfile.read()
218
232
            cheatfile.close()
219
233
    
220
 
        #global states
 
234
        global STATES
221
235
        # add bug data from .clicompanion to the liststore
222
 
        self.states = []
223
 
        for line in bugdata.splitlines():
 
236
        STATES = []
 
237
        for line in sorted(bugdata.splitlines()):
224
238
            l = line.split(':',2)
225
 
            self.states.append(l[0])
 
239
            STATES.append(l[0])
226
240
            self.liststore.append([l[0],l[1],l[2]])
227
 
        
 
241
            
 
242
            
228
243
    def __init__(self):
 
244
        
 
245
        self.setup()
229
246
        # Create a new window
230
247
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
231
 
        self.window.set_title("CLI Companion")              
 
248
        self.window.set_title("CLI Companion")
232
249
        # Sets the border width of the window.
233
250
        self.window.set_border_width(10)
234
251
        #set the size of the window
279
296
        self.treeview.get_selection().set_mode(gtk.SELECTION_SINGLE)
280
297
        self.treeview.get_selection().connect('changed',lambda s: mark_selected(s)) 
281
298
        
282
 
             
283
299
        
284
300
        def mark_selected(treeselection):
285
301
            (model,pathlist)=treeselection.get_selected_rows()
286
 
            global row
287
 
            row = pathlist
 
302
            global ROW
 
303
            ROW = pathlist
288
304
            #print pathlist #debug
289
305
 
290
306
        # make ui layout
293
309
        self.scrolledwindow = gtk.ScrolledWindow()
294
310
        self.scrolledwindow.set_size_request(700, 220)
295
311
        
296
 
        def buttonBox(self, spacing, layout):
 
312
        def button_box(self, spacing, layout):
297
313
            #button box at bottom of main window
298
314
            frame = gtk.Frame()
299
315
            bbox = gtk.HButtonBox()
322
338
            #Help Button
323
339
            buttonHelp = gtk.Button(stock=gtk.STOCK_HELP)
324
340
            bbox.add(buttonHelp)
325
 
            buttonHelp.connect("clicked", self.open_site)
 
341
            buttonHelp.connect("clicked", self.man_page)
326
342
            
327
343
            return frame
328
344
 
330
346
        self.vbox.pack_start(self.scrolledwindow)
331
347
        self.vbox.pack_start(self.search_box, True, True, 10)
332
348
        self.vbox.pack_start(self.vte, True, True, 0)
333
 
        self.vbox.pack_start(buttonBox( self, 10, gtk.BUTTONBOX_END), True, True, 5)
 
349
        self.vbox.pack_start(button_box( self, 10, gtk.BUTTONBOX_END), True, True, 5)
334
350
 
335
351
        self.scrolledwindow.add(self.treeview)
336
352
        self.window.add(self.vbox)
341
357
def main():
342
358
    gtk.main()
343
359
 
344
 
if __name__ == "__main__":
 
360
if __name__ == "__main__":       
345
361
    companion = Companion()
346
362
    main()
347
363