changeset 15:1281d999618c

yep
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 01 May 2013 08:06:50 -0700
parents a62fbff067f8
children 12db2d46722d
files smartopen/handlers.py
diffstat 1 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/smartopen/handlers.py
+++ b/smartopen/handlers.py
@@ -1,30 +1,37 @@
+"""
+handlers for smartopen
+"""
+
 import address
 import string
+import subprocess
 import urllib
 import urllib2
-from subprocess import call
 
 class Location(object):
     """
     generic class for locations
     """
 
     def __init__(self, baseurl=""):
         self.baseurl = baseurl
         # should/could strip here?
 
     def url(self, query):
+        """the URL to construct"""
         return self.baseurl + self.process(query)
 
     def process(self, query):
-        return query
+        """how to process the query"""
+        return query.strip()
 
     def test(self, query):
+        """whether the handler matches"""
         return True
 
     def exists(self, URL):
         """does a URL exist?"""
         # need a phony user agent so wikipedia won't know we're a bot
         headers = {}
         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'
 
@@ -168,22 +175,23 @@ class MercurialRevision(Location):
 
     def test(self, query):
         query = set(query)
         if query.issubset(string.digits + 'abcdef'):
             return True
         return False
 
 class UbuntuPackage(Location):
+    # Note: only works where apt-cache is available
     def __init__(self):
         baseurl = 'http://packages.ubuntu.com/'
         Location.__init__(self, baseurl)
 
     def test(self, query):
-        if len(self.baseurl.strip().split()) > 1:
+        if len(query.strip().split()) > 1:
             return False # no spaces in packages
 
         # use `apt-cache show` for the package name
         # the output could be captured:, e.g.
         #         ...
         # Filename: pool/main/h/hello/hello_2.8-2_amd64.deb
         # Size: 28124
         # MD5sum: 73ad59a7920e2adcff9b84c00e38418a
@@ -201,16 +209,18 @@ class UbuntuPackage(Location):
         # Description-md5: b7df6fe7ffb325083a3a60819a7df548
         # Bugs: https://bugs.launchpad.net/ubuntu/+filebug
         # Origin: Ubuntu
         # Supported: 18m
 
         # in theory both the home page and the ubuntu page could be interesting
         # (different handlers?)
 
-        returncode = call(['apt-cache', 'show', self.baseurl])
+        returncode = subprocess.call(['apt-cache', 'show', query], stdout=subprocess.PIPE)
         if returncode:
             return False
 
+        return True
+
 # TODO:
 # - https://mozillians.org/en-US/u/jhammel/ handler
 # - https://mozillians.org/en-US/u/williamr/ handler
 # ... but no 404???