Package logilab :: Package common :: Module pyro_ext
[frames] | no frames]

Source Code for Module logilab.common.pyro_ext

  1  # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
  2  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
  3  # 
  4  # This file is part of logilab-common. 
  5  # 
  6  # logilab-common is free software: you can redistribute it and/or modify it under 
  7  # the terms of the GNU Lesser General Public License as published by the Free 
  8  # Software Foundation, either version 2.1 of the License, or (at your option) any 
  9  # later version. 
 10  # 
 11  # logilab-common is distributed in the hope that it will be useful, but WITHOUT 
 12  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 13  # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more 
 14  # details. 
 15  # 
 16  # You should have received a copy of the GNU Lesser General Public License along 
 17  # with logilab-common.  If not, see <http://www.gnu.org/licenses/>. 
 18  """Python Remote Object utilities 
 19   
 20  Main functions available: 
 21   
 22  * `register_object` to expose arbitrary object through pyro using delegation 
 23    approach and register it in the nameserver. 
 24  * `ns_unregister` unregister an object identifier from the nameserver. 
 25  * `ns_get_proxy` get a pyro proxy from a nameserver object identifier. 
 26   
 27  :organization: Logilab 
 28   
 29   
 30  """ 
 31  __docformat__ = "restructuredtext en" 
 32   
 33  import logging 
 34  import tempfile 
 35   
 36  from Pyro import core, naming, errors, util, config 
 37   
 38  _LOGGER = logging.getLogger('pyro') 
 39  _MARKER = object() 
 40   
 41  config.PYRO_STORAGE = tempfile.gettempdir() 
 42   
43 -def ns_group_and_id(idstr, defaultnsgroup=_MARKER):
44 try: 45 nsgroup, nsid = idstr.rsplit('.', 1) 46 except ValueError: 47 if defaultnsgroup is _MARKER: 48 nsgroup = config.PYRO_NS_DEFAULTGROUP 49 else: 50 nsgroup = defaultnsgroup 51 nsid = idstr 52 if nsgroup is not None and not nsgroup.startswith(':'): 53 nsgroup = ':' + nsgroup 54 return nsgroup, nsid
55
56 -def host_and_port(hoststr):
57 if not hoststr: 58 return None, None 59 try: 60 hoststr, port = hoststr.split(':') 61 except ValueError: 62 port = None 63 else: 64 port = int(port) 65 return hoststr, port
66 67 _DAEMONS = {}
68 -def _get_daemon(daemonhost, start=True):
69 if not daemonhost in _DAEMONS: 70 if not start: 71 raise Exception('no daemon for %s' % daemonhost) 72 if not _DAEMONS: 73 core.initServer(banner=0) 74 host, port = host_and_port(daemonhost) 75 daemon = core.Daemon(host=host, port=port) 76 _DAEMONS[daemonhost] = daemon 77 return _DAEMONS[daemonhost]
78 79
80 -def locate_ns(nshost):
81 """locate and return the pyro name server to the daemon""" 82 core.initClient(banner=False) 83 return naming.NameServerLocator().getNS(*host_and_port(nshost))
84 85
86 -def register_object(object, nsid, defaultnsgroup=_MARKER, 87 daemonhost=None, nshost=None):
88 """expose the object as a pyro object and register it in the name-server 89 90 return the pyro daemon object 91 """ 92 nsgroup, nsid = ns_group_and_id(nsid, defaultnsgroup) 93 daemon = _get_daemon(daemonhost) 94 nsd = locate_ns(nshost) 95 # make sure our namespace group exists 96 try: 97 nsd.createGroup(nsgroup) 98 except errors.NamingError: 99 pass 100 daemon.useNameServer(nsd) 101 # use Delegation approach 102 impl = core.ObjBase() 103 impl.delegateTo(object) 104 daemon.connect(impl, '%s.%s' % (nsgroup, nsid)) 105 _LOGGER.info('registered %s a pyro object using group %s and id %s', 106 object, nsgroup, nsid) 107 return daemon
108 109
110 -def ns_unregister(nsid, defaultnsgroup=_MARKER, nshost=None):
111 """unregister the object with the given nsid from the pyro name server""" 112 nsgroup, nsid = ns_group_and_id(nsid, defaultnsgroup) 113 try: 114 nsd = locate_ns(nshost) 115 except errors.PyroError, ex: 116 # name server not responding 117 _LOGGER.error('can\'t locate pyro name server: %s', ex) 118 else: 119 try: 120 nsd.unregister('%s.%s' % (nsgroup, nsid)) 121 _LOGGER.info('%s unregistered from pyro name server', nsid) 122 except errors.NamingError: 123 _LOGGER.warning('%s not registered in pyro name server', nsid)
124 125
126 -def ns_get_proxy(nsid, defaultnsgroup=_MARKER, nshost=None):
127 nsgroup, nsid = ns_group_and_id(nsid, defaultnsgroup) 128 # resolve the Pyro object 129 try: 130 nsd = locate_ns(nshost) 131 pyrouri = nsd.resolve('%s.%s' % (nsgroup, nsid)) 132 except errors.ProtocolError, ex: 133 raise errors.PyroError( 134 'Could not connect to the Pyro name server (host: %s)' % nshost) 135 except errors.NamingError: 136 raise errors.PyroError( 137 'Could not get proxy for %s (not registered in Pyro), ' 138 'you may have to restart your server-side application' % nsid) 139 return core.getProxyForURI(pyrouri)
140 141
142 -def set_pyro_log_threshold(level):
143 pyrologger = logging.getLogger('Pyro.%s' % str(id(util.Log))) 144 # remove handlers so only the root handler is used 145 pyrologger.handlers = [] 146 pyrologger.setLevel(level)
147