Merge lp:~cmulk/cardapio/command-launcher-plugin into lp:cardapio

Proposed by cmulk
Status: Merged
Merge reported by: Thiago Teixeira
Merged at revision: not available
Proposed branch: lp:~cmulk/cardapio/command-launcher-plugin
Merge into: lp:cardapio
Diff against target: 129 lines (+125/-0)
1 file modified
src/plugins/command_launcher.py (+125/-0)
To merge this branch: bzr merge lp:~cmulk/cardapio/command-launcher-plugin
Reviewer Review Type Date Requested Status
Thiago Teixeira Pending
Review via email: mp+35771@code.launchpad.net
To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'src/plugins/command_launcher.py'
2--- src/plugins/command_launcher.py 1970-01-01 00:00:00 +0000
3+++ src/plugins/command_launcher.py 2010-09-17 03:19:39 +0000
4@@ -0,0 +1,125 @@
5+import_error = None
6+try:
7+ import os
8+ import glob
9+
10+except Exception, exception:
11+ import_error = exception
12+
13+
14+class CardapioPlugin (CardapioPluginInterface):
15+
16+ author = 'Clifton Mulkey'
17+ name = _('Command Launcher')
18+ description = _('Run commands from the search box')
19+
20+ url = ''
21+ help_text = ''
22+ version = '1.0'
23+
24+ plugin_api_version = 1.39
25+
26+ search_delay_type = None
27+ category_name = _('Run Command')
28+ category_icon = 'system-run'
29+ category_tooltip = _('Run system commands')
30+
31+ fallback_icon = 'system-run'
32+
33+ hide_from_sidebar = True
34+
35+
36+ def __init__(self, cardapio_proxy):
37+ '''
38+ This method is called when the plugin is enabled.
39+ Nothing much to be done here except initialize variables and set loaded to True
40+ '''
41+ self.c = cardapio_proxy
42+
43+ if import_error:
44+ self.c.write_to_log(self, 'Could not import certain modules', is_error = True)
45+ self.c.write_to_log(self, import_error, is_error = True)
46+ self.loaded = False
47+ return
48+
49+ self.pathlist = os.environ['PATH'].split(':')
50+
51+ self.loaded = True # set to true if everything goes well
52+
53+
54+ def search(self, text, result_limit):
55+
56+ self.current_query = text
57+ results = []
58+
59+ text_list = text.split(None,1)
60+ cmdname = text_list[0]
61+
62+ if len(text_list) == 2:
63+ args = ' ' + text_list[1]
64+ else:
65+ args = ''
66+
67+ num_results = 0
68+ for path in self.pathlist:
69+ if not num_results < result_limit: break
70+
71+ cmd_iter = (glob.iglob('%s/%s*' % (path, cmdname)))
72+
73+ while True:
74+ if not num_results < result_limit: break
75+
76+ try:
77+ cmd = os.path.basename(cmd_iter.next())
78+ item = {
79+ 'name' : '%s%s' % (cmd, args),
80+ 'tooltip' : 'Run \'%s%s\'' % (cmd,args),
81+ 'icon name' : cmd,
82+ 'type' : 'raw',
83+ 'command' : '%s%s' % (cmd, args),
84+ 'context menu' : [
85+ {
86+ 'name' : '%s%s %s' % (cmd, args, '(in Terminal)'),
87+ 'tooltip' : 'Run \'%s%s\' in a terminal' % (cmd, args),
88+ 'icon name' : 'utilities-terminal',
89+ 'type' : 'raw',
90+ 'command' : 'gnome-terminal -x bash -c \"%s%s ; bash\"' % (cmd, args)
91+ },
92+ {
93+ 'name' : '%s%s %s' % (cmd, args, '(as root)'),
94+ 'tooltip' : 'Run \'%s%s\' as root' % (cmd, args),
95+ 'icon name' : cmd,
96+ 'type' : 'raw',
97+ 'command' : 'gksudo \"%s%s\"' % (cmd, args)
98+ }]
99+ }
100+ results.append(item)
101+ num_results += 1
102+
103+ except StopIteration:
104+ break
105+
106+ results.append ({
107+ 'name' : text,
108+ 'tooltip' : 'Run \'%s\'' % text,
109+ 'icon name' : 'system-run',
110+ 'type' : 'raw',
111+ 'command' : text,
112+ 'context menu' : [
113+ {
114+ 'name' : text + ' (in Terminal)',
115+ 'tooltip' : 'Run \'%s\' in a terminal' % text,
116+ 'icon name' : 'utilities-terminal',
117+ 'type' : 'raw',
118+ 'command' : 'gnome-terminal -x bash -c \"%s ; bash\"' % text
119+ },
120+ {
121+ 'name' : text + ' (as root)',
122+ 'tooltip' : 'Run \'%s\' as root' % text,
123+ 'icon name' : 'system-run',
124+ 'type' : 'raw',
125+ 'command' : 'gksudo \"%s\"' % text
126+ }]
127+ })
128+
129+ self.c.handle_search_result(self, results, self.current_query)

Subscribers

People subscribed via source and target branches