Skip to content

Instantly share code, notes, and snippets.

@durdn
Created June 7, 2010 14:36
Show Gist options
  • Save durdn/428723 to your computer and use it in GitHub Desktop.
Save durdn/428723 to your computer and use it in GitHub Desktop.
query.py
from urllib import urlopen,quote
from urlparse import urljoin
from tms.parser import Record, TMSParser
from settings import TMS_SERVER_ENTRY_POINT
from settings import AUTHOR_SERVER_ENTRY_POINT
from settings import LOG_TMS_REQUESTS
def indent_xml(xml, indent=' '*4):
"""Indent and return XML."""
from xml.dom.minidom import parseString
return parseString(xml).toprettyxml(indent)
class Connection(object):
def __init__(self, server=TMS_SERVER_ENTRY_POINT,author_server=AUTHOR_SERVER_ENTRY_POINT):
if server:
self.server = server
if author_server:
self.author_server = author_server
class QueryProcessor(object):
" Fires actual requests to the TMS backend and receives xml responses "
def __init__(self, connection=Connection()):
self.connection = connection
def find(self,*args, **kwargs):
if len(kwargs.keys()) == 1:
k = kwargs.keys()[0]
v = kwargs[k]
return self.cit(query='dc:%s=%s' % (k,v))
else:
raise NotImplementedError('only one key=value pair is supported')
def get_author(self,authorname,action='get', command='search',fields='*',start=None,end=None):
""" Fires an C-IT query to get Author Biografie """
if not start:
range = '*'
else:
range = '%s-%s' % (str(start),str(end))
url = self.connection.author_server + 'action=%s&command=%s&' \
'query=displayname=%s&' \
'fields=%s&range=%s' % (action,command,unicode(authorname).encode('ascii','ignore'),fields,range)
response = urlopen(quote(url, '/:=')).read()
return response
def cit(self,action='get',
command='search',query='*=*',fields='*',start=None,end=None, sort=None):
""" Fires an C-IT query """
if not start:
range = '*'
else:
range = '%s-%s' % (str(start),str(end))
url = self.connection.server + 'action=%s&command=%s&' \
'query=%s&' \
'fields=%s&range=%s' % (action,command,unicode(query).encode('ascii','replace'),fields,range)
if sort:
url = '%s&sort=%s' % (url,sort)
# added url quoting martijng 04/01/2010
#if flag is true we print tms request to stderr
if LOG_TMS_REQUESTS:
import sys
print >>sys.stderr, 'TMS REQUEST:',quote(url, "/:=")
response = urlopen(quote(url, '/:=')).read()
return response
def sru(self,version='1.1',query=None,maximum_records=20,startrecord=1,*args,**kwargs):
""" Fires an SRU query """
kwargs['maximumRecords']= maximum_records
kwargs['startrecord'] = startrecord
kwargs['version'] = quote(version)
kwargs['query'] = quote(query)
#fixed elements in request
kwargs['operation'] = 'searchRetrieve'
kwargs['recordSchema'] = 'dc'
kwargs['sortKeys']= 'dc:identifier'
#full url of request
url = self.connection.server + '?operation=%(operation)s&version=%(version)s&' \
'query=%(query)s&' \
'maximumRecords=%(maximumRecords)d&recordSchema=%(recordSchema)s&' \
'startrecord=%(startrecord)d&sortKeys=%(sortKeys)s' % kwargs
response = urlopen(url).read()
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment