annotate smartopen/handlers.py @ 15:1281d999618c

yep
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 01 May 2013 08:06:50 -0700
parents a62fbff067f8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
1 """
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
2 handlers for smartopen
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
3 """
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
4
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
5 import address
10
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
6 import string
15
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
7 import subprocess
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
8 import urllib
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
9 import urllib2
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
10
11
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
11 class Location(object):
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
12 """
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
13 generic class for locations
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
14 """
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
15
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
16 def __init__(self, baseurl=""):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
17 self.baseurl = baseurl
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
18 # should/could strip here?
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
19
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
20 def url(self, query):
15
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
21 """the URL to construct"""
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
22 return self.baseurl + self.process(query)
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
23
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
24 def process(self, query):
15
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
25 """how to process the query"""
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
26 return query.strip()
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
27
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
28 def test(self, query):
15
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
29 """whether the handler matches"""
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
30 return True
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
31
11
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
32 def exists(self, URL):
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
33 """does a URL exist?"""
12
bb995bdf82e2 annoyingly work around user agents again; i hate robot protection sometimes
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
34 # need a phony user agent so wikipedia won't know we're a bot
bb995bdf82e2 annoyingly work around user agents again; i hate robot protection sometimes
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
35 headers = {}
bb995bdf82e2 annoyingly work around user agents again; i hate robot protection sometimes
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
36 headers['User-Agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4'
bb995bdf82e2 annoyingly work around user agents again; i hate robot protection sometimes
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
37
bb995bdf82e2 annoyingly work around user agents again; i hate robot protection sometimes
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
38 request = urllib2.Request(URL, None, headers)
11
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
39 try:
12
bb995bdf82e2 annoyingly work around user agents again; i hate robot protection sometimes
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
40 f = urllib2.urlopen(request).read()
11
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
41 return True
12
bb995bdf82e2 annoyingly work around user agents again; i hate robot protection sometimes
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
42 except urllib2.HTTPError, e:
bb995bdf82e2 annoyingly work around user agents again; i hate robot protection sometimes
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
43 return False
bb995bdf82e2 annoyingly work around user agents again; i hate robot protection sometimes
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
44 except urllib2.URLError, e:
11
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
45 return False
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
46
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
47 class URL(Location):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
48 """a straight URL"""
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
49
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
50 def process(self, query):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
51 if '://' in query:
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
52 return query
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
53 return 'http://' + query
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
54
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
55 def test(self, query):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
56 """try to open the url"""
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
57 if ' ' in query or '\n' in query:
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
58 return False
11
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
59 return self.exists(self.process(query))
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
60
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
61 class GoogleMaps(Location):
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
62 """try to google-maps the address"""
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
63
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
64 def __init__(self):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
65 gmapsurl='http://maps.google.com/maps?f=q&hl=en&q='
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
66 Location.__init__(self, gmapsurl)
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
67
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
68 def process(self, query):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
69 theaddress = address.normalizeaddress(query)
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
70 if not theaddress:
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
71 return theaddress
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
72 return urllib.quote_plus(theaddress)
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
73
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
74 def test(self, query):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
75 return bool(self.process(query))
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
76
1
10fc4904c10f now can pass data
k0s <k0scist@gmail.com>
parents: 0
diff changeset
77 class Trac(Location):
2
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
78 def __init__(self, baseurl):
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
79 baseurl = baseurl.strip('/') + '/'
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
80 Location.__init__(self, baseurl)
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
81
2
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
82 def process(self, query):
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
83 if query[0] == 'r':
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
84 if query[1:].isdigit():
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
85 return 'changeset/' + str(query[1:])
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
86 if query[0] == '#':
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
87 if query[1:].isdigit():
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
88 return 'ticket/' + str(query[1:])
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
89
2
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
90 def test(self, query):
f084e152dd47 add Trac handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
91 return bool(self.process(query))
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
92
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
93
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
94 class Wikipedia(Location):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
95 """try to open the query in wikipedia"""
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
96 def __init__(self):
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
97 wikiurl = 'http://en.wikipedia.org/wiki/'
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
98 Location.__init__(self, wikiurl)
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
99
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
100 def process(self, query):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
101 return urllib.quote_plus('_'.join(query.split()))
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
102
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
103 def test(self, query):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
104 'test to see if the article exists'
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
105
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
106 # need a phony user agent so wikipedia won't know we're a bot
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
107 headers = {}
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
108 headers['User-Agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4'
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
109
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
110 request = urllib2.Request(self.url(query), None, headers)
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
111 try:
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
112 f = urllib2.urlopen(request).read()
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
113 except urllib2.HTTPError, e:
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
114 return False
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
115
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
116 if 'Wikipedia does not have an article with this exact name' in f:
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
117 return False
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
118 return True
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
119
11
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
120 class Wiktionary(Location):
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
121 def __init__(self):
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
122 baseurl = 'http://en.wiktionary.org/wiki/'
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
123 Location.__init__(self, baseurl)
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
124 def test(self, query):
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
125 for c in (' ', '\n', '/'):
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
126 if c in query:
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
127 return False
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
128 if self.exists(self.url(query)):
ba9058605c5a add a wiktionary handler
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
129 return True
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
130
7
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
131 class Bugzilla(Location):
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
132 def __init__(self):
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
133 baseurl = 'https://bugzilla.mozilla.org/show_bug.cgi?id='
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
134 Location.__init__(self, baseurl)
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
135
7
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
136 def test(self, query):
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
137 try:
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
138 int(query)
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
139 return True
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
140 except:
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
141 return False
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
142
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
143 class Google(Location):
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
144 def __init__(self):
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
145 googleurl = 'http://www.google.com/search?hl=en&q='
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
146 Location.__init__(self, googleurl)
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
147
0
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
148 def process(self, query):
d6fa501af82f initial commit of smartopen
k0s <k0scist@gmail.com>
parents:
diff changeset
149 return urllib.quote_plus(query)
7
d5343f681ef9 add bugzilla handler
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
150
8
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
151 class FedEx(Location):
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
152 def __init__(self):
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
153 baseurl = 'http://www.fedex.com/Tracking?cntry_code=us&language=english&tracknumber_list='
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
154 Location.__init__(self, baseurl)
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
155
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
156 def process(self, query):
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
157 if query.count(' ') == 2:
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
158 query = ''.join(query.split(' '))
8
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
159 return query
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
160
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
161 def test(self, query):
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
162 query = self.process(query)
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
163 if len(query) != 12:
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
164 return False
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
165 try:
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
166 int(query)
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
167 except ValueError:
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
168 return False
44950f376e98 add FedEx tracking number handler
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
169 return True
10
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
170
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
171 class MercurialRevision(Location):
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
172 def __init__(self):
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
173 baseurl = 'http://hg.mozilla.org/mozilla-central/rev/'
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
174 Location.__init__(self, baseurl)
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
175
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
176 def test(self, query):
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
177 query = set(query)
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
178 if query.issubset(string.digits + 'abcdef'):
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
179 return True
a963acb1713d add a new handler: MercurialRevision
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
180 return False
13
f11ce7b1a349 note to self
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
181
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
182 class UbuntuPackage(Location):
15
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
183 # Note: only works where apt-cache is available
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
184 def __init__(self):
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
185 baseurl = 'http://packages.ubuntu.com/'
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
186 Location.__init__(self, baseurl)
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
187
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
188 def test(self, query):
15
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
189 if len(query.strip().split()) > 1:
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
190 return False # no spaces in packages
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
191
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
192 # use `apt-cache show` for the package name
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
193 # the output could be captured:, e.g.
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
194 # ...
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
195 # Filename: pool/main/h/hello/hello_2.8-2_amd64.deb
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
196 # Size: 28124
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
197 # MD5sum: 73ad59a7920e2adcff9b84c00e38418a
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
198 # SHA1: 70ef927cfa40b8e367f9ca7821e0064a946726c5
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
199 # SHA256: f8341711f6b803e032be9aff2206dfdb4045a0c6c296b0ea0d310d415f10ff4d
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
200 # Description-en: The classic greeting, and a good example
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
201 # The GNU hello program produces a familiar, friendly greeting. It
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
202 # allows non-programmers to use a classic computer science tool which
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
203 # would otherwise be unavailable to them.
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
204 # .
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
205 # Seriously, though: this is an example of how to do a Debian package.
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
206 # It is the Debian version of the GNU Project's `hello world' program
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
207 # (which is itself an example for the GNU Project).
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
208 # Homepage: http://www.gnu.org/software/hello/
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
209 # Description-md5: b7df6fe7ffb325083a3a60819a7df548
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
210 # Bugs: https://bugs.launchpad.net/ubuntu/+filebug
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
211 # Origin: Ubuntu
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
212 # Supported: 18m
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
213
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
214 # in theory both the home page and the ubuntu page could be interesting
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
215 # (different handlers?)
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
216
15
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
217 returncode = subprocess.call(['apt-cache', 'show', query], stdout=subprocess.PIPE)
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
218 if returncode:
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
219 return False
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
220
15
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
221 return True
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
222
13
f11ce7b1a349 note to self
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
223 # TODO:
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
224 # - https://mozillians.org/en-US/u/jhammel/ handler
13
f11ce7b1a349 note to self
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
225 # - https://mozillians.org/en-US/u/williamr/ handler
14
a62fbff067f8 start bringing this whole thing up to speed
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
226 # ... but no 404???