1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 = {}
78
79
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
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
96 try:
97 nsd.createGroup(nsgroup)
98 except errors.NamingError:
99 pass
100 daemon.useNameServer(nsd)
101
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
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
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
127 nsgroup, nsid = ns_group_and_id(nsid, defaultnsgroup)
128
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
143 pyrologger = logging.getLogger('Pyro.%s' % str(id(util.Log)))
144
145 pyrologger.handlers = []
146 pyrologger.setLevel(level)
147