Mercurial > hg > config
comparison python/clearsilver.py @ 0:f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
author | k0s <k0scist@gmail.com> |
---|---|
date | Thu, 15 Oct 2009 11:41:26 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f3ab51c79813 |
---|---|
1 #!/usr/bin/python | |
2 | |
3 import sys, os | |
4 | |
5 KEY_RETURN=""" | |
6 """ | |
7 | |
8 class ClearSilver(object): | |
9 """ | |
10 a blatant rip-off of quicksilver/katapult | |
11 """ | |
12 def __init__(self, startswith=''): | |
13 self.matches = {} | |
14 self.sortable = True | |
15 if startswith: | |
16 self.build_matches(startswith) | |
17 | |
18 def returnmatch(self): | |
19 keys = self.matches.keys() | |
20 if len(keys) == 1: | |
21 return keys[0], self.matches[keys[0]] | |
22 if self.sortable: | |
23 keys.sort() | |
24 return keys | |
25 | |
26 def find_matches(self, startswith): | |
27 nixlist = [] | |
28 for i in self.matches: | |
29 if not i.startswith(startswith): | |
30 nixlist.append(i) | |
31 | |
32 for i in nixlist: | |
33 self.matches.pop(i) | |
34 | |
35 return self.returnmatch() | |
36 | |
37 | |
38 class PATHsilver(ClearSilver): | |
39 | |
40 def build_matches(self, startswith=''): | |
41 path = os.environ['PATH'].split(':') | |
42 path.reverse() | |
43 for directory in path: | |
44 try: | |
45 for binary in os.listdir(directory): | |
46 if binary.startswith(startswith): | |
47 self.matches[binary] = '/'.join((directory,binary)) | |
48 except OSError: # directory not found | |
49 continue | |
50 return self.returnmatch() | |
51 | |
52 | |
53 def exec_match(self, key): | |
54 item = self.matches[key] | |
55 os.execl(item, item) | |
56 | |
57 | |
58 class Wordsilver(ClearSilver): | |
59 dictfile = '/usr/share/dict/cracklib-small' | |
60 | |
61 def build_matches(self, startswith=''): | |
62 f = file(self.dictfile, 'r') | |
63 for i in f.readlines(): | |
64 if i.startswith(startswith): | |
65 i = i.rstrip('\r\n') | |
66 self.matches[i] = i | |
67 return self.returnmatch() | |
68 | |
69 def exec_match(self, key): | |
70 item = self.matches[key] | |
71 print key | |
72 | |
73 class BookmarkSilver(ClearSilver): | |
74 def __init__(self, startswith=''): | |
75 ClearSilver.__init__(self) | |
76 self.sortable = False | |
77 if startswith: | |
78 self.build_matches(startswith) | |
79 | |
80 def add_url(self, i, startswith): | |
81 delimiter = '//' | |
82 j = i[i.index(delimiter) + len(delimiter):] | |
83 j = j.rstrip('/') | |
84 if j.startswith(startswith): | |
85 self.matches[j] = i | |
86 j = j.strip('w.') | |
87 if j.startswith(startswith): | |
88 self.matches[j] = i | |
89 | |
90 def build_matches(self, startswith=''): | |
91 # find the firefox files | |
92 firefoxdir = '/'.join((os.environ['HOME'], '.mozilla', 'firefox')) | |
93 profile = file('/'.join((firefoxdir, 'profiles.ini')), 'r').readlines() | |
94 profile = [i.rstrip('\n\r \t') for i in profile] | |
95 index = profile.index('Name=default') | |
96 delimiter = 'Path=' | |
97 while 1: | |
98 index += 1 | |
99 if profile[index].startswith(delimiter): | |
100 profile = '/'.join((firefoxdir, profile[index][len(delimiter):])) | |
101 break | |
102 bookmarks = '/'.join((profile, 'bookmarks.html')) | |
103 history = '/'.join((profile, 'history.dat')) | |
104 import re | |
105 history = file(history, 'r').read() | |
106 history = re.findall('http.*//.*\)', history) | |
107 history.reverse() | |
108 for i in history: | |
109 i = i[:i.index(')')] | |
110 self.add_url(i, startswith) | |
111 | |
112 bookmarks = file(bookmarks, 'r').read() | |
113 bookmarks = re.findall('"http.*//.*"', bookmarks) | |
114 | |
115 for i in bookmarks: | |
116 i = i.strip('"') | |
117 i = i[:i.index('"')] | |
118 self.add_url(i, startswith) | |
119 | |
120 return self.returnmatch() | |
121 | |
122 def exec_match(self, key): | |
123 item = self.matches[key] | |
124 | |
125 os.system("firefox " + item) | |
126 | |
127 if __name__ == '__main__': | |
128 | |
129 matcher_type = 'PATHsilver' | |
130 | |
131 # parse options | |
132 options = { 'D' : 'Wordsilver', 'H' : 'BookmarkSilver' } | |
133 for i in sys.argv[1:]: | |
134 i = i.strip('-') | |
135 if options.has_key(i): | |
136 matcher_type = options[i] | |
137 | |
138 # init the 'GUI' | |
139 import curses | |
140 stdscr = curses.initscr() | |
141 curses.noecho() | |
142 curses.cbreak() | |
143 stdscr.keypad(1) | |
144 | |
145 try: | |
146 # XXX should include colors at some point -- not right now, though | |
147 # curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE) | |
148 | |
149 c = '' | |
150 matcher = None | |
151 matches = None | |
152 entered='' | |
153 | |
154 while 1: | |
155 y, x = stdscr.getmaxyx() | |
156 stdscr.refresh() | |
157 c = stdscr.getch() | |
158 stdscr.erase() | |
159 | |
160 # handle backspaces | |
161 if c == curses.KEY_BACKSPACE or c == 127: | |
162 entered = entered[:-1] | |
163 matcher = None | |
164 matches = None | |
165 else: | |
166 | |
167 try: | |
168 c = chr(c) | |
169 except ValueError: | |
170 continue | |
171 | |
172 if c == KEY_RETURN: | |
173 break | |
174 | |
175 entered += c | |
176 | |
177 if not matcher: | |
178 matcher = eval(matcher_type + "()") | |
179 matches = matcher.build_matches(entered) | |
180 else: | |
181 | |
182 if c.isdigit() and int(c) < min(10, len(matches)): | |
183 for i in matches: | |
184 if i.startswith(entered): | |
185 break | |
186 else: | |
187 matches = matches[int(c):int(c)+1] | |
188 break | |
189 | |
190 matches = matcher.find_matches(entered) | |
191 | |
192 | |
193 if isinstance(matches, list): | |
194 i=1 | |
195 for match in matches: | |
196 if i >= y: continue | |
197 if i <= 10: | |
198 numstr = " :" + str(i-1) | |
199 else: | |
200 numstr = "" | |
201 stdscr.addstr(i,0, match + numstr) | |
202 i += 1 | |
203 stdscr.addstr(0,0, entered) | |
204 | |
205 if matches: stdscr.addstr(1,0, matches[0] + " :0") | |
206 else: | |
207 stdscr.addstr(0,0,entered + " -> " + matches[1]) | |
208 | |
209 finally: | |
210 # 'GUI' cleanup | |
211 curses.nocbreak() | |
212 stdscr.keypad(0) | |
213 curses.echo() | |
214 curses.endwin() | |
215 | |
216 # execute the program (if found) | |
217 if not matches: | |
218 sys.exit(1) | |
219 matcher.exec_match(matches[0]) | |
220 | |
221 |