Skip to content

Commit

Permalink
Print 'Infinite' for infinite durations in topic endpoint info (#722)
Browse files Browse the repository at this point in the history
Signed-off-by: Emerson Knapp <eknapp@amazon.com>
  • Loading branch information
emersonknapp authored May 6, 2021
1 parent b3cd14d commit 901fb47
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
9 changes: 9 additions & 0 deletions rclpy/rclpy/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def nanoseconds(self):
def __repr__(self):
return 'Duration(nanoseconds={0})'.format(self.nanoseconds)

def __str__(self):
if self == Infinite:
return 'Infinite'
return f'{self.nanoseconds} nanoseconds'

def __eq__(self, other):
if isinstance(other, Duration):
return self.nanoseconds == other.nanoseconds
Expand Down Expand Up @@ -79,3 +84,7 @@ def from_msg(cls, msg):

def get_c_duration(self):
return self._duration_handle


# Constant representing an infinite amount of time.
Infinite = Duration(nanoseconds=_rclpy.RMW_DURATION_INFINITE)
29 changes: 15 additions & 14 deletions rclpy/rclpy/topic_endpoint_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,18 @@ def __eq__(self, other):
for slot in self.__slots__)

def __str__(self):
result = 'Node name: %s\n' % self.node_name
result += 'Node namespace: %s\n' % self.node_namespace
result += 'Topic type: %s\n' % self.topic_type
result += 'Endpoint type: %s\n' % self.endpoint_type.name
result += 'GID: %s\n' % '.'.join(format(x, '02x') for x in self.endpoint_gid)
result += 'QoS profile:\n'
result += ' Reliability: %s\n' % self.qos_profile.reliability.name
result += ' Durability: %s\n' % self.qos_profile.durability.name
result += ' Lifespan: %d nanoseconds\n' % self.qos_profile.lifespan.nanoseconds
result += ' Deadline: %d nanoseconds\n' % self.qos_profile.deadline.nanoseconds
result += ' Liveliness: %s\n' % self.qos_profile.liveliness.name
result += ' Liveliness lease duration: %d nanoseconds' % \
self.qos_profile.liveliness_lease_duration.nanoseconds
return result
gid = '.'.join(format(x, '02x') for x in self.endpoint_gid)
return '\n'.join([
f'Node name: {self.node_name}',
f'Node namespace: {self.node_namespace}',
f'Topic type: {self.topic_type}',
f'Endpoint type: {self.endpoint_type.name}',
f'GID: {gid}',
'QoS profile:',
f' Reliability: {self.qos_profile.reliability.name}',
f' Durability: {self.qos_profile.durability.name}',
f' Lifespan: {self.qos_profile.lifespan}',
f' Deadline: {self.qos_profile.deadline}',
f' Liveliness: {self.qos_profile.liveliness.name}',
f' Liveliness lease duration: {self.qos_profile.liveliness_lease_duration}',
])
1 change: 1 addition & 0 deletions rclpy/src/rclpy/_rclpy_pybind11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ PYBIND11_MODULE(_rclpy_pybind11, m) {
.value("CANCELED", GOAL_EVENT_CANCELED);

m.attr("RCL_DEFAULT_DOMAIN_ID") = py::int_(RCL_DEFAULT_DOMAIN_ID);
m.attr("RMW_DURATION_INFINITE") = py::int_(rmw_time_total_nsec(RMW_DURATION_INFINITE));

py::enum_<rcl_clock_change_t>(m, "ClockChange")
.value(
Expand Down
5 changes: 5 additions & 0 deletions rclpy/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from rclpy.clock import ClockType
from rclpy.duration import Duration
from rclpy.duration import Infinite
from rclpy.time import Time

from test_msgs.msg import Builtins
Expand Down Expand Up @@ -228,3 +229,7 @@ def test_seconds_nanoseconds(self):
assert (1, int(5e8)) == Time(seconds=1, nanoseconds=5e8).seconds_nanoseconds()
assert (1, int(5e8)) == Time(seconds=0, nanoseconds=15e8).seconds_nanoseconds()
assert (0, 0) == Time().seconds_nanoseconds()

def test_infinite_duration(self):
duration = Infinite
assert str(duration) == 'Infinite'

0 comments on commit 901fb47

Please sign in to comment.