#!/usr/local/bin/python -- ## -*-python-*- ##**************************************************************************** ## ## File: AsyncInterClient.py ## RCS: $Header: /u/testbed/CVSROOT/dldev/src/IBClient/ibclient.py,v 1.7 1996/09/30 22:34:42 hassan Exp $ ## Description: Asynchronous DL Interchange client ## Author: Scott Hassan, Stanford University ## Created: ## Modified: ## Language: python ## Package: N/A ## Status: Experimental (Do Not Distribute) ## ## (c) Copyright 1995, Stanford University, all rights reserved. ## ##***************************************************************************** ## ## ## import os, socket, sys import time, string, regex import ilu import any import urllib import IDLInterchange import IDLInterchange__skel import USMarc ##gContext = "dl/Users/hassan/" gContext = "dl/" gURL = "http://coho.stanford.edu/cgi-bin/hassan/cosnaming/cosnaming.py" ## exceptions NotFound = "Binding not found" SearchError = "Search Error" CannotProceed = "Cannot Proceed" def Keys(pNamingContext, pBindingPath): ## """ ##Return a list of the bound names in the naming context. ## - pDLNamingContext can be used to specify a different naming context ## other than the default. ## """ items = [] url = pBindingPath + "?method=List&pContext=" + pNamingContext try: fp = urllib.urlopen(url) lines = [] while 1: line = fp.readline() if not line: break line = string.strip(line) if line == "CannotProceed": raise CannotProceed, "no context found." lines.append(line) if len(lines) >= 4: items.append(lines[1]) lines = [] ## print lines fp.close() except IOError, (errcode, reason): raise CannotProceed, reason except os.error, (errcode, reason): raise CannotProceed, reason return items def Lookup(pName, pNamingContext, pBindingPath): ## """ ##Resolve pName into an SBH using the file based naming context. ## - pDLNamingContext can be used to specify a different naming context ## other than the default. ## """ url = pBindingPath + "?method=Resolve&pContext=" + pNamingContext + "&pName=" + pName try: fp = urllib.urlopen(url) lines = fp.readlines() fp.close() if lines == None: raise NotFound, "No binding found" except IOError, (errcode, reason): raise NotFound, reason except os.error, (errcode, reason): raise NotFound, reason try: if string.strip(lines[0]) == "CannotProceed": raise NotFound, "Unable to resolve binding." sbh = string.strip(lines[0]) typename = string.strip(lines[1]) typeid = string.strip(lines[2]) except IndexError, reason: raise NotFound, "error when parsing binding." return (sbh, typename, typeid) ## ---------------------------------------------------------------------- class Item(IDLInterchange__skel.CItem): def __init__(self): self.propdb = {} def keys(self): return self.propdb.keys() def has_key(self, key): return self.propdb.has_key(key) def __getitem__(self, key): return self.propdb[key] def __setitem__(self, key, value): self.propdb[key] = value def __delitem__(self, key): del self.propdb[key] def items(self): return self.propdb.items() class ResultCollection(IDLInterchange__skel.CCollection): def __init__(self): self.fServiceName = None self.fQuery = None self.fServPref = None self.fLoopHandle = None self.fTotalItems = None self.fItems = [] def __len__(self): return len(self.fItems) def __getitem__(self, pIndex): return self.fItems[pIndex] def __getslice__(self, i, j): return self.fItems[i:j] ## IDLInterchange method def SetTotalItems(self, pTotalItems): print "SetTotalItems(): ", pTotalItems self.fTotalItems = pTotalItems ## IDLInterchange method def AddItems(self, pMessageID, pItems, pMoreCookie): print "AddItems(): " names = pItems['aState']['aNames'] valueslist = pItems['aState']['anItemsState'] aACs = pItems['aACs'] i = 0 for values in valueslist: j = 0 item = Item() self.fItems.append(item) ## print "Item #", i for value in values: ## item.define_property(names[j], value) field = names[j] ## fieldname = USMarc.USMarc[field][0] value = any.InputAny(value) item[field] = value j = j + 1 ## print i = i + 1 ## IDLInterchange method def RaiseException(self, pMessageID, pException): print "RaiseException(): " print pException[1] ## IDLInterchange method def CompletedRequest(self, pMessageID): print "CompletedRequest()" ilu.ExitMainLoop(self.fLoopHandle) def Search(self, pLoopHandle, pServiceName, pQuery, pServPref): self.fLoopHandle = pLoopHandle self.fServiceName = pServiceName self.fQuery = pQuery self.fServPref = pServPref (sbh, typename, typeid) = Lookup(pServiceName, gContext + "Services/", gURL) service = ilu.ObjectOfSBH(IDLInterchange.CConstrainCollection, sbh) try: ret = service.IluPing() if ret == ilu.FALSE: raise SearchError, "Found service but the service is not responding." except ilu.IluProtocolError, aReason: raise SearchError, "Found service but the service is not responding: ilu.IluProtocolError" + `aReason` except ilu.IluGeneralError, aReason: raise SearchError, "Found service but the service is not responding: ilu.IluGeneralError" + `aReason` print service try: service.RequestConstrain(any.OutputAny(1000), self.fQuery, self.fServPref, self) except ilu.IluProtocolError, aReason: raise SearchError, "Unable to submit query: ilu.IluProtocolError" + `aReason` except ilu.IluGeneralError, aReason: raise SearchError, "Unable to submit query: ilu.IluGeneralError" + `aReason` def main(argc, argv): if argc <= 2: print "usage:", print argv[0], "servicename", "query" services = Keys(gContext + "Services", gURL) print for service in services: print service return theServiceName = argv[1] aQuery = string.join(argv[2:]) try: ## Now allow self.sourceObjName to be in the form: ## Dialog:275 or even Dialog:INSPEC,PAPER ## We will pass those subcollections on to DLCollection here --sbc l = string.split(theServiceName,':') theServiceName = l[0] if len(l) > 1: subCollections = string.split(l[1],',') else: subCollections = None except: baseSource = theServiceName subCollections = None print "service:", theServiceName print "query:", aQuery theFields = [USMarc.kTitle, USMarc.kURL, USMarc.kWeight, USMarc.kAbstract] theNumHits = 10 ## create query structure aQueryType0 = IDLInterchange.RPNQuery0(1, any.OutputAny(aQuery)) aQueryDescription = [IDLInterchange.QueryType.kQTYPE0, aQueryType0] aQuerySummary = IDLInterchange.TQuerySummary(None, aQueryDescription, theFields, []) aMoreSummary = [{'property_name': 'subcollections', 'property_value': any.OutputAny(subCollections)}] aServicePrefs = IDLInterchange.TServicePrefSummary(None, theNumHits, aMoreSummary) iluserver = ilu.CreateServer() lh = ilu.CreateLoopHandle() ## initiate search results = ResultCollection() try: results.Search(lh, theServiceName, aQuerySummary, aServicePrefs) except SearchError, reason: print SearchError, reason sys.exit(1) ## wait for events ilu.RunMainLoop(lh) ## Display the results for item in results[:]: for field in item.keys(): fieldname = USMarc.USMarc[field][0] print "%10s: %s" % (fieldname, item[field]) print ## ---------------------------------------------------------------------- if __name__ == '__main__': main(len(sys.argv), sys.argv)