#!/usr/bin/python # Copyright (c) 2005 by Mark T. Holder, Florida State University. (see end of file) '''Classes called during service acquisition to decide which implementation to use.''' from PIPRes.wrap.idl import CipresIDL_api1 class ByNameDisambiguator: '''Chooses and entry based on a specific application name (may not be unique).''' def __init__(self, raiseIfNotFound = False, **kwargs): '''kwargs should map interface name (e.g. TreeImprove) strings to the name of the application.''' self.interfaceToAppNameMap = kwargs self.raiseIfNotFound = raiseIfNotFound def restrictQuery(self, initialQuery, interfaceClass): '''Alter fields of initialQuery to restrict the list returned by CipresIDL_api1.Registry.find()''' interface = interfaceClass is not None and interfaceClass.__name__ or None appName = self.interfaceToAppNameMap.get(interface) if appName is not None: if len(initialQuery.applicationName): if initialQuery.applicationName != appName and self.raiseIfNotFound: raise CipresIDL_api1.NoRegistryMatchException, 'applicationName cannot be specified as both %s and %s' %(initialQuery.applicationName, appName) else: initialQuery.applicationName = appName return initialQuery def validateEntry(self, entry, interfaceClass): '''raise an exception if the entry is not acceptable.''' if self.raiseIfNotFound: interface = interfaceClass is not None and interfaceClass.__name__ or None appName = self.interfaceToAppNameMap.get(interface) capName = appName.upper() if str(entry.applicationName).upper() != capName: raise CipresIDL_api1.NoRegistryMatchException, 'Did not find an %s entry with applicationName = %s' % (interface, str(appName)) def chooseEntry(self, registryEntries, interfaceClass): '''return the list of all acceptable entries (or raise an exception).''' interface = interfaceClass is not None and interfaceClass.__name__ or None appName = self.interfaceToAppNameMap.get(interface) if appName is not None: capName = appName.upper() for e in registryEntries: if str(e.applicationName).upper() == capName: return [e] if self.raiseIfNotFound: raise CipresIDL_api1.NoRegistryMatchException, 'Did not find an %s entry with applicationName = %s' % (interface, str(appName)) return registryEntries # This file is part of the PIPRes library # The PIPRes library is free software; you can redistribute it # and/or modify it under the terms of the GNU Lesser General # Public License as published by the Free Software Foundation; # either version 2.1 of the License, or (at your option) any later # version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, # MA 02111-1307, USA