-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add convenience name translations for use by commandline utilities etc. #352
Conversation
Signed-off-by: Emerson Knapp <eknapp@amazon.com>
@mjcarroll see this and ros2/ros2cli#240, is this along the lines of what you were thinking? |
rclpy/rclpy/qos.py
Outdated
@@ -308,3 +308,65 @@ def __init__(self, qos_profile: QoSProfile, profile_name: str): | |||
'qos_profile_parameter_events') | |||
qos_profile_action_status_default = _rclpy_action.rclpy_action_get_rmw_qos_profile( | |||
'rcl_action_qos_profile_status_default') | |||
|
|||
|
|||
class QoSNameTranslations: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather see these names become part of the enum class. I think if the name is closer to the enum it will be more likely to be updated if/when new QoS features are added. Also, personally I want shorter names in code.
#!/usr/bin/env python3
from enum import IntEnum
class QoSLivelinessPolicy(IntEnum):
"""
Enum for QoS Liveliness settings.
This enum matches the one defined in rmw/types.h
"""
RMW_QOS_POLICY_LIVELINESS_SYSTEM_DEFAULT = 0
system_default = RMW_QOS_POLICY_LIVELINESS_SYSTEM_DEFAULT
RMW_QOS_POLICY_LIVELINESS_AUTOMATIC = 1
automatic = RMW_QOS_POLICY_LIVELINESS_AUTOMATIC
RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_NODE = 2
manual_by_node = RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_NODE
RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC = 3
manual_by_topic = RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC
import code
code.interact(local=locals())
Enum classes are generated with a __getitem__
that allows getting the member with a string.
>>> QoSLivelinessPolicy['system_default']
<QoSLivelinessPolicy.RMW_QOS_POLICY_LIVELINESS_SYSTEM_DEFAULT: 0>
>>> QoSLivelinessPolicy['automatic']
<QoSLivelinessPolicy.RMW_QOS_POLICY_LIVELINESS_AUTOMATIC: 1>
The keys can be gotten off the class via __members__
. One drawback for a CLI tool is the values would be a bit noisy as it includes the long all capitals name.
>>> QoSLivelinessPolicy.__members__.keys()
odict_keys(['RMW_QOS_POLICY_LIVELINESS_SYSTEM_DEFAULT', 'system_default', 'RMW_QOS_POLICY_LIVELINESS_AUTOMATIC', 'automatic', 'RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_NODE', 'manual_by_node', 'RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC', 'manual_by_topic'])
But that could be cleaned up with a little filtering
>>> [k for k in QoSLivelinessPolicy.__members__.keys() if not k.startswith('RMW')]
['system_default', 'automatic', 'manual_by_node', 'manual_by_topic']
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this suggestion, thanks! I have pushed a new commit to use this pattern, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would these still be useful for the CLI if the names were SYSTEM_DEFAULT
instead of system_default
? Since PEP 8 wants all-caps, it seems likely a linter will complain about these being lower case now or in the future.
The enum documentation also argues in favor of all-caps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be a reasonable use case to have users call PolicyEnum[arg.upper()]
, or perhaps provide another convenience function QoSPolicyEnum.get_short_name(cls, name)
that uppercases the name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or perhaps provide another convenience function QoSPolicyEnum.get_short_name(cls, name) that uppercases the name
Did you mean a convenience function that provides lower case names? I suspect the linter tests in this package will fail in the future if system_default
etc is lowercase instead of uppercase, meaning this might need to be
class QoSLivelinessPolicy(IntEnum):
"""
Enum for QoS Liveliness settings.
This enum matches the one defined in rmw/types.h
"""
RMW_QOS_POLICY_LIVELINESS_SYSTEM_DEFAULT = 0
SYSTEM_DEFAULT = RMW_QOS_POLICY_LIVELINESS_SYSTEM_DEFAULT
RMW_QOS_POLICY_LIVELINESS_AUTOMATIC = 1
AUTOMATIC = RMW_QOS_POLICY_LIVELINESS_AUTOMATIC
RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_NODE = 2
MANUAL_BY_NODE = RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_NODE
RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC = 3
MANUAL_BY_TOPIC = RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New commit should clarify what I was trying to say : yes use all uppercase names for enum, but provide case-insensitive getter function for users.
Signed-off-by: Emerson Knapp <eknapp@amazon.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with green CI
Dismissing for now with lower-case/upper-case question
Signed-off-by: Emerson Knapp <eknapp@amazon.com>
@thomas-moulard can you please run CI for this change |
I'm pretty confused about the osx and windows build failures there. Both show 100% tests pass and fail with
|
This smells like a problem in this recent change: colcon/colcon-core#187 |
Same issue on these builds ros2/demos#338 (comment) - so that seems likely to me since these two have no common changes |
Problem fixed in ros2/launch#245. Re-triggering builds: |
Unblocks ros2/ros2cli#240
Signed-off-by: Emerson Knapp eknapp@amazon.com