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
+++ b/setup.py
@@ -1,17 +1,17 @@
 from setuptools import setup, find_packages
 import sys, os
 
 try:
     description = file('README.txt').read()
 except:
     description = ''
 
-version = '0.1.4'
+version = '0.1.5'
 
 setup(name='smartopen',
       version=version,
       description="open text in a browser contextually",
       long_description=description,
       classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
       keywords='',
       author='Jeff Hammel',
@@ -28,15 +28,16 @@ setup(name='smartopen',
       # -*- Entry points: -*-
       [console_scripts]
       smartopen = smartopen.smartopen:main
 
       [smartopen.locations]
       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
       FedEx = smartopen.handlers:FedEx
       MercurialRevision = smartopen.handlers:MercurialRevision
       """,
       )
--- a/smartopen/handlers.py
+++ b/smartopen/handlers.py
@@ -1,50 +1,51 @@
 import address
 import string
 import urllib
 import urllib2
 
-class Location:
+class Location(object):
     """
     generic class for locations
     """
 
     def __init__(self, baseurl=""):
         self.baseurl = baseurl
 
     def url(self, query):
         return self.baseurl + self.process(query)
 
     def process(self, query):
         return query
 
     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"""
 
     def process(self, query):
         if '://' in query:
             return query
         return 'http://' + query
 
     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"""
 
     def __init__(self):
         gmapsurl='http://maps.google.com/maps?f=q&hl=en&q='
         Location.__init__(self, gmapsurl)
 
@@ -95,16 +96,27 @@ class Wikipedia(Location):
             f = urllib2.urlopen(request).read()
         except urllib2.HTTPError, e:
             return False
 
         if 'Wikipedia does not have an article with this exact name' in f:
             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='
         Location.__init__(self, baseurl)
     
     def test(self, query):
         try:
             int(query)