Go to the documentation of this file.00001 import functools
00002 import genpy
00003 import collections
00004 from . import numpy_msg
00005
00006 _to_numpy = {}
00007 _from_numpy = {}
00008
00009 def converts_to_numpy(msgtype, plural=False):
00010 assert issubclass(msgtype, genpy.Message)
00011 def decorator(f):
00012 _to_numpy[msgtype, plural] = f
00013 _to_numpy[numpy_msg(msgtype), plural] = f
00014 return f
00015 return decorator
00016
00017 def converts_from_numpy(msgtype, plural=False):
00018 assert issubclass(msgtype, genpy.Message)
00019 def decorator(f):
00020 _from_numpy[msgtype, plural] = f
00021 _from_numpy[numpy_msg(msgtype), plural] = f
00022 return f
00023 return decorator
00024
00025 def numpify(msg, *args, **kwargs):
00026 if msg is None:
00027 return
00028
00029 conv = _to_numpy.get((msg.__class__, False))
00030 if not conv and isinstance(msg, collections.Sequence):
00031 if not msg:
00032 raise ValueError("Cannot determine the type of an empty Collection")
00033 conv = _to_numpy.get((msg[0].__class__, True))
00034
00035
00036 if not conv:
00037 raise ValueError("Unable to convert message {} - only supports {}".format(
00038 msg.__class__.__name__,
00039 ', '.join(cls.__name__ + ("[]" if pl else '') for cls, pl in _to_numpy.keys())
00040 ))
00041
00042 return conv(msg, *args, **kwargs)
00043
00044 def msgify(msg_type, numpy_obj, *args, **kwargs):
00045 conv = _from_numpy.get((msg_type, kwargs.pop('plural', False)))
00046 if not conv:
00047 raise ValueError("Unable to build message {} - only supports {}".format(
00048 msg_type.__name__,
00049 ', '.join(cls.__name__ + ("[]" if pl else '') for cls, pl in _to_numpy.keys())
00050 ))
00051 return conv(numpy_obj, *args, **kwargs)