Mercurial > hg > smartopen
comparison smartopen/handlers.py @ 15:1281d999618c
yep
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 01 May 2013 08:06:50 -0700 |
parents | a62fbff067f8 |
children |
comparison
equal
deleted
inserted
replaced
14:a62fbff067f8 | 15:1281d999618c |
---|---|
1 """ | |
2 handlers for smartopen | |
3 """ | |
4 | |
1 import address | 5 import address |
2 import string | 6 import string |
7 import subprocess | |
3 import urllib | 8 import urllib |
4 import urllib2 | 9 import urllib2 |
5 from subprocess import call | |
6 | 10 |
7 class Location(object): | 11 class Location(object): |
8 """ | 12 """ |
9 generic class for locations | 13 generic class for locations |
10 """ | 14 """ |
12 def __init__(self, baseurl=""): | 16 def __init__(self, baseurl=""): |
13 self.baseurl = baseurl | 17 self.baseurl = baseurl |
14 # should/could strip here? | 18 # should/could strip here? |
15 | 19 |
16 def url(self, query): | 20 def url(self, query): |
21 """the URL to construct""" | |
17 return self.baseurl + self.process(query) | 22 return self.baseurl + self.process(query) |
18 | 23 |
19 def process(self, query): | 24 def process(self, query): |
20 return query | 25 """how to process the query""" |
21 | 26 return query.strip() |
22 def test(self, query): | 27 |
28 def test(self, query): | |
29 """whether the handler matches""" | |
23 return True | 30 return True |
24 | 31 |
25 def exists(self, URL): | 32 def exists(self, URL): |
26 """does a URL exist?""" | 33 """does a URL exist?""" |
27 # need a phony user agent so wikipedia won't know we're a bot | 34 # need a phony user agent so wikipedia won't know we're a bot |
171 if query.issubset(string.digits + 'abcdef'): | 178 if query.issubset(string.digits + 'abcdef'): |
172 return True | 179 return True |
173 return False | 180 return False |
174 | 181 |
175 class UbuntuPackage(Location): | 182 class UbuntuPackage(Location): |
183 # Note: only works where apt-cache is available | |
176 def __init__(self): | 184 def __init__(self): |
177 baseurl = 'http://packages.ubuntu.com/' | 185 baseurl = 'http://packages.ubuntu.com/' |
178 Location.__init__(self, baseurl) | 186 Location.__init__(self, baseurl) |
179 | 187 |
180 def test(self, query): | 188 def test(self, query): |
181 if len(self.baseurl.strip().split()) > 1: | 189 if len(query.strip().split()) > 1: |
182 return False # no spaces in packages | 190 return False # no spaces in packages |
183 | 191 |
184 # use `apt-cache show` for the package name | 192 # use `apt-cache show` for the package name |
185 # the output could be captured:, e.g. | 193 # the output could be captured:, e.g. |
186 # ... | 194 # ... |
204 # Supported: 18m | 212 # Supported: 18m |
205 | 213 |
206 # in theory both the home page and the ubuntu page could be interesting | 214 # in theory both the home page and the ubuntu page could be interesting |
207 # (different handlers?) | 215 # (different handlers?) |
208 | 216 |
209 returncode = call(['apt-cache', 'show', self.baseurl]) | 217 returncode = subprocess.call(['apt-cache', 'show', query], stdout=subprocess.PIPE) |
210 if returncode: | 218 if returncode: |
211 return False | 219 return False |
220 | |
221 return True | |
212 | 222 |
213 # TODO: | 223 # TODO: |
214 # - https://mozillians.org/en-US/u/jhammel/ handler | 224 # - https://mozillians.org/en-US/u/jhammel/ handler |
215 # - https://mozillians.org/en-US/u/williamr/ handler | 225 # - https://mozillians.org/en-US/u/williamr/ handler |
216 # ... but no 404??? | 226 # ... but no 404??? |