Skip to content

Commit

Permalink
add namespace feature (ros#93).
Browse files Browse the repository at this point in the history
  • Loading branch information
kohei306 committed Oct 14, 2020
1 parent f9b1abe commit 4e75742
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/genmsg/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
CONSTCHAR = '='
COMMENTCHAR = '#'
IODELIM = '---'
NAMESPACE = '#NAMESPACE'


verbose = False
Expand Down
22 changes: 20 additions & 2 deletions src/genmsg/msg_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
except ImportError:
from io import StringIO # Python 3.x

from . base import InvalidMsgSpec, log, SEP, COMMENTCHAR, CONSTCHAR, IODELIM, EXT_MSG, EXT_SRV
from . base import InvalidMsgSpec, log, SEP, COMMENTCHAR, CONSTCHAR, IODELIM, EXT_MSG, EXT_SRV, NAMESPACE
from . msgs import MsgSpec, TIME, TIME_MSG, DURATION, DURATION_MSG, HEADER, HEADER_FULL_NAME, \
is_builtin, is_valid_msg_field_name, is_valid_msg_type, bare_msg_type, is_valid_constant_type, \
Field, Constant, resolve_type
Expand Down Expand Up @@ -237,6 +237,19 @@ def _load_field_line(orig_line, package_context):
field_type = HEADER_FULL_NAME
return field_type, name

def _load_namespace_line(orig_line, name_space):
if not orig_line.startswith(NAMESPACE):
return name_space
split_line = line_splits = [s for s in [x.strip() for x in orig_line.split(" ")] if s]
len_split_line = len(split_line)
if len_split_line == 2:
if name_space == '':
return split_line[1]
else:
raise InvalidMsgSpec("Invalid constant declaration, there should be only one #NAMESPACE per msg file: %s"%orig_line)
else:
raise InvalidMsgSpec("Invalid constant declaration: %s"%orig_line)

def _strip_comments(line):
return line.split(COMMENTCHAR)[0].strip() #strip comments

Expand All @@ -256,7 +269,9 @@ def load_msg_from_string(msg_context, text, full_name):
types = []
names = []
constants = []
name_space = ''
for orig_line in text.split('\n'):
name_space = _load_namespace_line(orig_line, name_space)
clean_line = _strip_comments(orig_line)
if not clean_line:
continue #ignore empty lines
Expand All @@ -266,7 +281,9 @@ def load_msg_from_string(msg_context, text, full_name):
field_type, name = _load_field_line(orig_line, package_name)
types.append(field_type)
names.append(name)
spec = MsgSpec(types, names, constants, text, full_name, package_name)
if name_space == '':
name_space = package_name # No Name space has been specified. Use package name.
spec = MsgSpec(types, names, constants, text, full_name, package_name, name_space)
msg_context.register(full_name, spec)
return spec

Expand Down Expand Up @@ -464,6 +481,7 @@ def load_srv_from_string(msg_context, text, full_name):
accum = text_out
else:
accum.write(l+'\n')


# create separate MsgSpec objects for each half of file
msg_in = load_msg_from_string(msg_context, text_in.getvalue(), '%sRequest'%(full_name))
Expand Down
5 changes: 3 additions & 2 deletions src/genmsg/msgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class MsgSpec(object):
correspondence. MsgSpec can also return an md5 of the source text.
"""

def __init__(self, types, names, constants, text, full_name, package = '', short_name = ''):
def __init__(self, types, names, constants, text, full_name, package = '', name_space = '', short_name = ''):
"""
:param types: list of field types, in order of declaration, ``[str]``
:param names: list of field names, in order of declaration, ``[str]``
Expand Down Expand Up @@ -261,6 +261,7 @@ def __init__(self, types, names, constants, text, full_name, package = '', short
self.full_name = full_name
self.short_name = short_name
self.package = package
self.name_space = name_space
try:
self._parsed_fields = [Field(name, type) for (name, type) in zip(self.names, self.types)]
except ValueError as e:
Expand Down Expand Up @@ -291,7 +292,7 @@ def __eq__(self, other):
return self.types == other.types and self.names == other.names and \
self.constants == other.constants and self.text == other.text and \
self.full_name == other.full_name and self.short_name == other.short_name and \
self.package == other.package
self.package == other.package and self.name_space == other.name_space

def __ne__(self, other):
if not other or not isinstance(other, MsgSpec):
Expand Down

0 comments on commit 4e75742

Please sign in to comment.