""" zurlparser.py - experimental Z39.50 url parser, see rfc 2056 for more info requires web.py. incomplete implementation. example: http://localhost:8080/validate/z39.50s://z3950.loc.gov:7090/VOYAGER?au="van rossum guido" """ import web class ParseError(Exception): """Base class for exceptions from this module""" pass class URIParseError(ParseError): """Class for parsing URI errors""" def __init__(self, _reason): self.reason = _reason ParseError.__init__(self) def __str__(self): return self.reason class URLParser: """URL Parser class""" def parse_uri(self): """Parse a Z39.50 URL in RFC 2056 format - not completely implemented""" uri = self def pull(v, q, index): _i = index + 1, v.find(q, index) return v[_i[0]:_i[1]], _i[1] if uri[0:10] == 'z39.50s://': host, _index = pull(uri, ':', 9) port, _index = pull(uri, '/', _index) if uri.find('?') == -1: db = uri[_index + 1:] query = None else: db, _index = pull(uri, '?', _index) query = uri[_index + 1:] if uri.find('&') <> -1: #to be implemented... pass return {'host': host, 'port': int(port), 'db': db, 'query': query} elif uri[0:10] == 'z39.50r://': raise URIParseError('URI scheme "z39.50r:" not yet implemented') else: raise URIParseError('%s is not a valid Z39.50 URI' % uri) class validate: """web.py class to validate Z39.50 URIs""" def GET(self, uri): print URLParser().parse_uri(uri) web.webapi.internalerror = web.debugerror if __name__ == '__main__': web.run(urls, globals())