# HG changeset patch # User Jeff Hammel # Date 1293067324 28800 # Node ID ba9058605c5a358db3d7d24e71eb55bc6eac8923 # Parent a963acb1713d726dbb817e929fc02b4e20d4978b add a wiktionary handler diff -r a963acb1713d -r ba9058605c5a setup.py --- 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 diff -r a963acb1713d -r ba9058605c5a smartopen/handlers.py --- 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='