Esempio n. 1
0
def resolve_name(name, caller_id=None):
    """
    Resolve a ROS name to its global, canonical form. Private ~names
    are resolved relative to the node name. 

    @param name: name to resolve.
    @type  name: str
    @param caller_id: node name to resolve relative to. To
    resolve to local namespace, omit this parameter (or use None)
    @type  caller_id: str
    @return: Resolved name. If name is empty/None, resolve_name
    returns parent namespace. If namespace is empty/None,
    @rtype: str
    """
    if not caller_id:
        caller_id = get_name()
    if not name: #empty string resolves to namespace
        return namespace(caller_id)

    name = canonicalize_name(name)
    if name[0] == SEP: #global name
        resolved_name = name
    elif is_private(name): #~name
        resolved_name = ns_join(caller_id, name[1:])
    else: #relative
        resolved_name = namespace(caller_id) + name

    #Mappings override general namespace-based resolution
    # - do this before canonicalization as remappings are meant to
    #   match the name as specified in the code
    if resolved_name in _resolved_mappings:
        return _resolved_mappings[resolved_name]
    else:
        return resolved_name
Esempio n. 2
0
def resolve_name(name, caller_id=None):
    """
    Resolve a ROS name to its global, canonical form. Private ~names
    are resolved relative to the node name. 

    @param name: name to resolve.
    @type  name: str
    @param caller_id: node name to resolve relative to. To
    resolve to local namespace, omit this parameter (or use None)
    @type  caller_id: str
    @return: Resolved name. If name is empty/None, resolve_name
    returns parent namespace. If namespace is empty/None,
    @rtype: str
    """
    if not caller_id:
        caller_id = get_name()
    if not name: #empty string resolves to namespace
        return namespace(caller_id)

    name = str(name)  # enforce string conversion else struct.pack might raise UnicodeDecodeError (see #3998)
    name = canonicalize_name(name)
    if name[0] == SEP: #global name
        resolved_name = name
    elif is_private(name): #~name
        resolved_name = ns_join(caller_id, name[1:])
    else: #relative
        resolved_name = namespace(caller_id) + name

    #Mappings override general namespace-based resolution
    # - do this before canonicalization as remappings are meant to
    #   match the name as specified in the code
    if resolved_name in _resolved_mappings:
        return _resolved_mappings[resolved_name]
    else:
        return resolved_name
Esempio n. 3
0
def _set_caller_id(caller_id):
    """
    Internal API.
    Set the global name (i.e. caller_id) and namespace. Methods can
    check what the name of the current node is by calling get_caller_id.

    The caller_id is important as it is the first parameter to any API
    call on a remote node.  Invoked by ROSNode constructor
    @param caller_id: new caller ID
    @type  caller_id: str
    """    
    global _caller_id, _caller_namespace
    _caller_id = caller_id
    _caller_namespace = namespace(caller_id)
Esempio n. 4
0
def _set_caller_id(caller_id):
    """
    Internal API.
    Set the global name (i.e. caller_id) and namespace. Methods can
    check what the name of the current node is by calling get_caller_id.

    The caller_id is important as it is the first parameter to any API
    call on a remote node.  Invoked by ROSNode constructor
    @param caller_id: new caller ID
    @type  caller_id: str
    """    
    global _caller_id, _caller_namespace
    _caller_id = caller_id
    _caller_namespace = namespace(caller_id)
Esempio n. 5
0
def scoped_name(caller_id, name):
    """
    Convert the global caller_id to a relative name within the namespace. For example, for
    namespace '/foo' and name '/foo/bar/name', the return value will
    be 'bar/name'

    WARNING: scoped_name does not validate that name is actually within
    the supplied namespace.
    @param caller_id: caller ID, in canonical form
    @type  caller_id: str
    @param name: name to scope
    @type  name: str
    @return: name scoped to the caller_id's namespace. 
    @rtype: str
    """
    if not is_global(caller_id):
        raise ROSException("caller_id must be global")
    return canonicalize_name(name)[len(namespace(caller_id)):]
Esempio n. 6
0
def scoped_name(caller_id, name):
    """
    Convert the global caller_id to a relative name within the namespace. For example, for
    namespace '/foo' and name '/foo/bar/name', the return value will
    be 'bar/name'

    WARNING: scoped_name does not validate that name is actually within
    the supplied namespace.
    @param caller_id: caller ID, in canonical form
    @type  caller_id: str
    @param name: name to scope
    @type  name: str
    @return: name scoped to the caller_id's namespace. 
    @rtype: str
    """
    if not is_global(caller_id):
        raise ROSException("caller_id must be global")
    return canonicalize_name(name)[len(namespace(caller_id)):]
Esempio n. 7
0
def test_namespace():
    from rosgraph.names import namespace
    try:
        namespace(1)
        assert False, "1"
    except TypeError:
        pass
    try:
        namespace(None)
        assert False, "None"
    except ValueError:
        pass
    assert '/' == namespace('')
    assert '/' == namespace('/')
    assert '/' == namespace('/foo')
    assert '/' == namespace('/foo/')
    assert '/foo/' == namespace('/foo/bar')
    assert '/foo/' == namespace('/foo/bar/')
    assert '/foo/bar/' == namespace('/foo/bar/baz')
    assert '/foo/bar/' == namespace('/foo/bar/baz/')

    # unicode tests
    assert u'/' == namespace(u'')
    assert u'/' == namespace(u'/')
    assert u'/foo/bar/' == namespace(u'/foo/bar/baz/')
Esempio n. 8
0
def test_namespace():
    from rosgraph.names import namespace
    try:
        namespace(1)
        assert False, "1"
    except TypeError: pass
    try:
        namespace(None)
        assert False, "None"
    except ValueError: pass
    assert '/'== namespace('')
    assert '/'== namespace('/')
    assert '/'== namespace('/foo')
    assert '/'== namespace('/foo/')
    assert '/foo/'== namespace('/foo/bar')
    assert '/foo/'== namespace('/foo/bar/')
    assert '/foo/bar/'== namespace('/foo/bar/baz')
    assert '/foo/bar/'== namespace('/foo/bar/baz/')

    # unicode tests
    assert u'/'== namespace(u'')
    assert u'/'== namespace(u'/')
    assert u'/foo/bar/'== namespace(u'/foo/bar/baz/')