diff --git a/src/genmsg/base.py b/src/genmsg/base.py index b09d89e..a966301 100644 --- a/src/genmsg/base.py +++ b/src/genmsg/base.py @@ -45,6 +45,7 @@ CONSTCHAR = '=' COMMENTCHAR = '#' IODELIM = '---' +NAMESPACE = '#NAMESPACE' verbose = False diff --git a/src/genmsg/msg_loader.py b/src/genmsg/msg_loader.py index 4548018..de79406 100644 --- a/src/genmsg/msg_loader.py +++ b/src/genmsg/msg_loader.py @@ -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 @@ -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 @@ -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 @@ -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 @@ -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)) diff --git a/src/genmsg/msgs.py b/src/genmsg/msgs.py index 1468aa0..542c586 100644 --- a/src/genmsg/msgs.py +++ b/src/genmsg/msgs.py @@ -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]`` @@ -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: @@ -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):