def resolve(uri, hmac_key=None): """ Resolve a 'magic' uri (PYRONAME, PYROMETA) into the direct PYRO uri. It finds a name server, and use that to resolve a PYRONAME uri into the direct PYRO uri pointing to the named object. If uri is already a PYRO uri, it is returned unmodified. You can consider this a shortcut function so that you don't have to locate and use a name server proxy yourself. Note: if you need to resolve more than a few names, consider using the name server directly instead of repeatedly calling this function, to avoid the name server lookup overhead from each call. """ if isinstance(uri, basestring): uri = core.URI(uri) elif not isinstance(uri, core.URI): raise TypeError("can only resolve Pyro URIs") if uri.protocol == "PYRO": return uri log.debug("resolving %s", uri) if uri.protocol == "PYRONAME": with locateNS(uri.host, uri.port, hmac_key=hmac_key) as nameserver: return nameserver.lookup(uri.object) elif uri.protocol == "PYROMETA": with locateNS(uri.host, uri.port, hmac_key=hmac_key) as nameserver: candidates = nameserver.list(metadata_all=uri.object) if candidates: candidate = random.choice(list(candidates.values())) log.debug("resolved to candidate %s", candidate) return core.URI(candidate) raise NamingError( "no registrations available with desired metadata properties %s" % uri.object) else: raise PyroError("invalid uri protocol")
def __init__(self, host=None, port=None, unixsocket=None, nathost=None, natport=None): if Pyro4.config.DOTTEDNAMES: raise PyroError( "Name server won't start with DOTTEDNAMES enabled because of security reasons" ) if host is None: host = Pyro4.config.HOST if port is None: port = Pyro4.config.NS_PORT if nathost is None: nathost = Pyro4.config.NATHOST if natport is None: natport = Pyro4.config.NATPORT or None super(NameServerDaemon, self).__init__(host, port, unixsocket, nathost=nathost, natport=natport) self.nameserver = NameServer() self.register(self.nameserver, constants.NAMESERVER_NAME) self.nameserver.register(constants.NAMESERVER_NAME, self.uriFor(self.nameserver)) log.info("nameserver daemon created")
def resolve(uri): """Resolve a 'magic' uri (PYRONAME) into the direct PYRO uri.""" if isinstance(uri, basestring): uri = core.URI(uri) elif not isinstance(uri, core.URI): raise TypeError("can only resolve Pyro URIs") if uri.protocol == "PYRO": return uri log.debug("resolving %s", uri) if uri.protocol == "PYRONAME": nameserver = locateNS(uri.host, uri.port) uri = nameserver.lookup(uri.object) nameserver._pyroRelease() return uri else: raise PyroError("invalid uri protocol")
def resolve(uri, hmac_key=None): """ Resolve a 'magic' uri (PYRONAME) into the direct PYRO uri. It finds a name server, and use that to resolve a PYRONAME uri into the direct PYRO uri pointing to the named object. If uri is already a PYRO uri, it is returned unmodified. You can consider this a shortcut function so that you don't have to locate and use a name server proxy yourself. Note: if you need to resolve more than a few names, consider using the name server directly instead of repeatedly calling this function, to avoid the name server lookup overhead from each call. """ if isinstance(uri, basestring): uri = core.URI(uri) elif not isinstance(uri, core.URI): raise TypeError("can only resolve Pyro URIs") if uri.protocol == "PYRO": return uri log.debug("resolving %s", uri) if uri.protocol == "PYRONAME": nameserver = locateNS(uri.host, uri.port, hmac_key=hmac_key) uri = nameserver.lookup(uri.object) nameserver._pyroRelease() return uri else: raise PyroError("invalid uri protocol")
def __enter__(self): if not self.nameserver: raise PyroError("cannot reuse this object") return self