changeset 11:ba9058605c5a

add a wiktionary handler
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 22 Dec 2010 17:22:04 -0800
parents a963acb1713d
children bb995bdf82e2
files setup.py smartopen/handlers.py
diffstat 2 files changed, 22 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/setup.py	Tue Sep 21 12:01:34 2010 -0700
+++ b/setup.py	Wed Dec 22 17:22:04 2010 -0800
@@ -6,7 +6,7 @@
 except:
     description = ''
 
-version = '0.1.4'
+version = '0.1.5'
 
 setup(name='smartopen',
       version=version,
@@ -33,6 +33,7 @@
       URL = smartopen.handlers:URL
       GoogleMaps = smartopen.handlers:GoogleMaps
       Wikipedia = smartopen.handlers:Wikipedia
+      Wiktionary = smartopen.handlers:Wiktionary
       Google = smartopen.handlers:Google
       Trac = smartopen.handlers:Trac
       Bugzilla = smartopen.handlers:Bugzilla
--- a/smartopen/handlers.py	Tue Sep 21 12:01:34 2010 -0700
+++ b/smartopen/handlers.py	Wed Dec 22 17:22:04 2010 -0800
@@ -3,7 +3,7 @@
 import urllib
 import urllib2
 
-class Location:
+class Location(object):
     """
     generic class for locations
     """
@@ -20,6 +20,13 @@
     def test(self, query):
         return True
 
+    def exists(self, URL):
+        """does a URL exist?"""
+        try:
+            urllib.urlopen(URL)
+            return True
+        except IOError:
+            return False
 
 class URL(Location):
     """a straight URL"""
@@ -31,15 +38,9 @@
 
     def test(self, query):
         """try to open the url"""
-
         if ' ' in query or '\n' in query:
             return False
-
-        try:
-            site = urllib.urlopen(self.process(query))
-        except IOError:
-            return False
-        return True
+        return self.exists(self.process(query))
 
 class GoogleMaps(Location):
     """try to google-maps the address"""
@@ -100,6 +101,17 @@
             return False
         return True
 
+class Wiktionary(Location):
+    def __init__(self):
+        baseurl = 'http://en.wiktionary.org/wiki/'
+        Location.__init__(self, baseurl)
+    def test(self, query):
+        for c in (' ', '\n', '/'):
+            if c in query:
+                return False
+        if self.exists(self.url(query)):
+            return True
+        
 class Bugzilla(Location):
     def __init__(self):
         baseurl = 'https://bugzilla.mozilla.org/show_bug.cgi?id='