def __init__(self, ns, filename, parent=None, params=None, env_args=None, \ resolve_dict=None, include_resolve_dict=None, arg_names=None): """ @param ns: namespace @type ns: str @param filename: name of file this is being loaded from @type filename: str @param resolve_dict: (optional) resolution dictionary for substitution args @type resolve_dict: dict @param include_resolve_dict: special resolution dictionary for <include> tags. Must be None if this is not an <include> context. @type include_resolve_dict: dict @param arg_names: name of args that have been declared in this context @type arg_names: [str] """ self.parent = parent self.ns = make_global_ns(ns or '/') self._remap_args = [] self.params = params or [] self.env_args = env_args or [] self.filename = filename # for substitution args self.resolve_dict = resolve_dict or {} # arg names. Args that have been declared in this context self.arg_names = arg_names or [] # special scoped resolve dict for processing in <include> tag self.include_resolve_dict = include_resolve_dict or None
def _ns_clear_params_attr(self, tag_name, tag, context, ros_config, node_name=None, include_filename=None): """ Common processing routine for xml tags with NS and CLEAR_PARAMS attributes @param tag: DOM Node @type tag: Node @param context: current namespace context @type context: LoaderContext @param clear_params: list of params to clear @type clear_params: [str] @param node_name: name of node (for use when tag_name == 'node') @type node_name: str @param include_filename: <include> filename if this is an <include> tag. If specified, context will use include rules. @type include_filename: str @return: loader context @rtype: L{LoaderContext} """ if tag.hasAttribute(NS): ns = self.resolve_args(tag.getAttribute(NS), context) if not ns: raise XmlParseException( "<%s> tag has an empty '%s' attribute" % (tag_name, NS)) else: ns = None if include_filename is not None: child_ns = context.include_child(ns, include_filename) else: child_ns = context.child(ns) clear_p = self.resolve_args(tag.getAttribute(CLEAR_PARAMS), context) if clear_p: clear_p = _bool_attr(clear_p, False, 'clear_params') if clear_p: if tag_name == 'node': if not node_name: raise XmlParseException( "<%s> tag must have a 'name' attribute to use '%s' attribute" % (tag_name, CLEAR_PARAMS)) # use make_global_ns to give trailing slash in order to be consistent with XmlContext.ns ros_config.add_clear_param( make_global_ns(ns_join(child_ns.ns, node_name))) else: if not ns: raise XmlParseException( "'ns' attribute must be set in order to use 'clear_params'" ) ros_config.add_clear_param(child_ns.ns) return child_ns
def list_params(ns): """ Get list of parameters in ns @param ns: namespace to match @type ns: str """ try: ns = make_global_ns(ns) names = _succeed(get_param_server().getParamNames(_get_caller_id())) names.sort() return [n for n in names if n.startswith(ns)] except socket.error: raise ROSParamIOException("Unable to communicate with master!")
def test_make_global_ns(self): from roslib.names import make_global_ns for n in ['~foo']: try: make_global_ns(n) self.fail("make_global_ns should fail on %s"%n) except ValueError: pass self.assertEquals('/foo/', make_global_ns('foo')) self.assertEquals('/', make_global_ns('')) self.assertEquals('/foo/', make_global_ns('/foo')) self.assertEquals('/foo/', make_global_ns('/foo/')) self.assertEquals('/foo/bar/', make_global_ns('/foo/bar')) self.assertEquals('/foo/bar/', make_global_ns('/foo/bar/'))
def test_make_global_ns(self): from roslib.names import make_global_ns for n in ['~foo']: try: make_global_ns(n) self.fail("make_global_ns should fail on %s" % n) except ValueError: pass self.assertEquals('/foo/', make_global_ns('foo')) self.assertEquals('/', make_global_ns('')) self.assertEquals('/foo/', make_global_ns('/foo')) self.assertEquals('/foo/', make_global_ns('/foo/')) self.assertEquals('/foo/bar/', make_global_ns('/foo/bar')) self.assertEquals('/foo/bar/', make_global_ns('/foo/bar/'))
def _set_param(self, ctx, my_state, test_vals, param_server): ctx = make_global_ns(ctx) for type, vals in test_vals: try: caller_id = ns_join(ctx, "node") count = 0 for val in vals: key = ns_join(caller_id, "%s-%s" % (type, count)) param_server.set_param(key, val) self.assert_(param_server.has_param(key)) true_key = ns_join(ctx, key) my_state[true_key] = val count += 1 except Exception, e: assert "getParam failed on type[%s], val[%s]" % (type, val)
def _set_param(self, ctx, my_state, test_vals, param_server): ctx = make_global_ns(ctx) for type, vals in test_vals: try: caller_id = ns_join(ctx, "node") count = 0 for val in vals: key = ns_join(caller_id, "%s-%s"%(type,count)) param_server.set_param(key, val) self.assert_(param_server.has_param(key)) true_key = ns_join(ctx, key) my_state[true_key] = val count += 1 except Exception, e: assert "getParam failed on type[%s], val[%s]"%(type,val)
def _setParam(self, ctx, myState, testVals, master): ctx = make_global_ns(ctx) for type, vals in testVals: try: callerId = ns_join(ctx, "node") count = 0 for val in vals: key = "%s-%s"%(type,count) #print("master.setParam(%s,%s)"%(callerId, key)) master.setParam(callerId, key, val) self.assert_(self.apiSuccess(master.hasParam(callerId, key))) trueKey = ns_join(ctx, key) myState[trueKey] = val count += 1 except Exception: assert "getParam failed on type[%s], val[%s]"%(type,val)
def _setParam(self, ctx, myState, testVals, master): ctx = make_global_ns(ctx) for type, vals in testVals: try: callerId = ns_join(ctx, "node") count = 0 for val in vals: key = "%s-%s"%(type,count) #print "master.setParam(%s,%s)"%(callerId, key) master.setParam(callerId, key, val) self.assert_(self.apiSuccess(master.hasParam(callerId, key))) trueKey = ns_join(ctx, key) myState[trueKey] = val count += 1 except Exception, e: assert "getParam failed on type[%s], val[%s]"%(type,val)
def _ns_clear_params_attr(self, tag_name, tag, context, ros_config, node_name=None, include_filename=None): """ Common processing routine for xml tags with NS and CLEAR_PARAMS attributes @param tag: DOM Node @type tag: Node @param context: current namespace context @type context: LoaderContext @param clear_params: list of params to clear @type clear_params: [str] @param node_name: name of node (for use when tag_name == 'node') @type node_name: str @param include_filename: <include> filename if this is an <include> tag. If specified, context will use include rules. @type include_filename: str @return: loader context @rtype: L{LoaderContext} """ if tag.hasAttribute(NS): ns = self.resolve_args(tag.getAttribute(NS), context) if not ns: raise XmlParseException("<%s> tag has an empty '%s' attribute"%(tag_name, NS)) else: ns = None if include_filename is not None: child_ns = context.include_child(ns, include_filename) else: child_ns = context.child(ns) clear_p = self.resolve_args(tag.getAttribute(CLEAR_PARAMS), context) if clear_p: clear_p = _bool_attr(clear_p, False, 'clear_params') if clear_p: if tag_name == 'node': if not node_name: raise XmlParseException("<%s> tag must have a 'name' attribute to use '%s' attribute"%(tag_name, CLEAR_PARAMS)) # use make_global_ns to give trailing slash in order to be consistent with XmlContext.ns ros_config.add_clear_param(make_global_ns(ns_join(child_ns.ns, node_name))) else: if not ns: raise XmlParseException("'ns' attribute must be set in order to use 'clear_params'") ros_config.add_clear_param(child_ns.ns) return child_ns
def _testPrivateNames(self): master = self.master myState = {} self._checkParamState(myState) testContexts = ['', 'sub1', 'sub1/sub2', 'sub1/sub2/sub3'] for c in testContexts: c = make_global_ns(c) callerId = ns_join(c, "node") keyStub = "param1" testKey = "~%s" % keyStub testVal = random.randint(-1000000, 100000) master.setParam(callerId, testKey, testVal) trueKey = ns_join(callerId, keyStub) myState[trueKey] = testVal print("checking", trueKey) v1 = self.apiSuccess(master.getParam('/', trueKey)) v2 = self.apiSuccess(master.getParam(callerId, testKey)) assert v1 == v2, "[%s]: %s vs. [%s,%s]: %s" % ( trueKey, v1, callerId, testKey, v2) assert self.apiSuccess(master.hasParam(callerId, testKey)), testKey assert self.apiSuccess(master.hasParam('node', trueKey)), trueKey #test setting a local param on a different node testKey = ns_join("altnode", "param2") testVal = random.randint(-1000000, 100000) master.setParam(callerId, testKey, testVal) trueKey = ns_join(c, testKey) altCallerId = ns_join(c, "altnode") myState[trueKey] = testVal v1 = self.apiSuccess(master.getParam(altCallerId, "~param2")) v2 = self.apiSuccess(master.getParam(callerId, testKey)) assert v1 == v2 assert self.apiSuccess(master.hasParam(callerId, testKey)), testKey assert self.apiSuccess(master.hasParam(altCallerId, "~param2")) self._checkParamState(myState)
def _testPrivateNames(self): master = self.master myState = {} self._checkParamState(myState) testContexts = ['', 'sub1', 'sub1/sub2', 'sub1/sub2/sub3'] for c in testContexts: c = make_global_ns(c) callerId = ns_join(c, "node") keyStub = "param1" testKey = "~%s"%keyStub testVal = random.randint(-1000000, 100000) master.setParam(callerId, testKey, testVal) trueKey = ns_join(callerId, keyStub) myState[trueKey] = testVal print("checking", trueKey) v1 = self.apiSuccess(master.getParam('/', trueKey)) v2 = self.apiSuccess(master.getParam(callerId, testKey)) assert v1 == v2, "[%s]: %s vs. [%s,%s]: %s"%(trueKey, v1, callerId, testKey, v2) assert self.apiSuccess(master.hasParam(callerId, testKey)), testKey assert self.apiSuccess(master.hasParam('node', trueKey)), trueKey #test setting a local param on a different node testKey = ns_join("altnode","param2") testVal = random.randint(-1000000, 100000) master.setParam(callerId, testKey, testVal) trueKey = ns_join(c, testKey) altCallerId = ns_join(c, "altnode") myState[trueKey] = testVal v1 = self.apiSuccess(master.getParam(altCallerId, "~param2")) v2 = self.apiSuccess(master.getParam(callerId, testKey)) assert v1 == v2 assert self.apiSuccess(master.hasParam(callerId, testKey)), testKey assert self.apiSuccess(master.hasParam(altCallerId, "~param2")) self._checkParamState(myState)
def _testEncapsulation(self): """testEncapsulation: test encapsulation: setting same parameter at different levels""" master = self.master myState = {} self._checkParamState(myState) testContexts = ['', 'en', 'en/sub1', 'en/sub2', 'en/sub1/sub2'] for c in testContexts: c = make_global_ns(c) testKey = 'param1' testVal = random.randint(-1000000, 100000) callerId = ns_join(c, "node") trueKey = ns_join(c, testKey) master.setParam(callerId, testKey, testVal) myState[trueKey] = testVal # make sure the master has the parameter under both keys and that they are equal v1 = self.apiSuccess(master.getParam('/', trueKey)) v2 = self.apiSuccess(master.getParam(callerId, testKey)) assert v1 == v2, "[%s]: %s vs. [%s,%s]: %s"%(trueKey, v1, callerId, testKey, v2) assert self.apiSuccess(master.hasParam(callerId, testKey)), testKey assert self.apiSuccess(master.hasParam('node', trueKey)), trueKey self._checkParamState(myState)