"""Spec-compliant Atom autodiscovery module with test suite"""
__version__ = "1.2"
__date__ = "2007-05-30"
__author__ = "Mark Pilgrim "
__license__ = """Copyright (c) 2003-2007, Mark Pilgrim, All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE."""
try:
import timeoutsocket # http://www.timo-tasi.org/python/timeoutsocket.py
timeoutsocket.setDefaultSocketTimeout(10)
except ImportError:
pass
from sgmllib import SGMLParser, charref
import urllib, urlparse, sys
class AutodiscoveryLinkParser(SGMLParser):
def __init__(self, baseuri):
SGMLParser.__init__(self)
self.links = []
self.baseuri = baseuri
def normalize_attrs(self, attrs):
attrs = [(k.lower(), charref.sub(lambda m: chr(int(m.groups()[0])), v).strip()) for k, v in attrs]
attrs = [(k, k in ('rel','type') and v.lower() or v) for k, v in attrs]
return attrs
def do_base(self, attrs):
attrs = dict(self.normalize_attrs(attrs))
if not attrs.has_key('href'): return
self.baseuri = attrs['href']
def do_link(self, attrs):
attrs = dict(self.normalize_attrs(attrs))
if not attrs.has_key('rel'): return
rels = attrs['rel'].split()
if 'alternate' not in rels: return
if attrs.get('type') != 'application/atom+xml': return
if not attrs.has_key('href'): return
self.links.append(urlparse.urljoin(self.baseuri, attrs['href']))
def getLinks(data, uri):
parser = AutodiscoveryLinkParser(uri)
parser.feed(data)
return parser.links
##### test harness ######
def test():
uri = 'http://diveintomark.org/tests/client/autodiscovery/html4-001.html'
failed = []
count = 0
while 1:
usock = urllib.urlopen(uri)
headers = usock.headers.dict
data = usock.read().strip()
charset = headers.get('content-type', '')
if charset.find('charset=') != -1:
charset = charset.split('charset=', 1).pop().strip()
if charset:
data = data.decode(charset)
if data.find('Atom autodiscovery test') == -1: break
sys.stdout.write('.')
count += 1
links = getLinks(data, uri)
if not links:
print '\n*** FAILED ***', uri, 'could not find link'
failed.append(uri)
elif len(links) > 1:
print '\n*** FAILED ***', uri, 'found too many links'
failed.append(uri)
else:
atomdata = urllib.urlopen(links[0]).read()
if atomdata.find('