Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions pyros_schemas/ros/schemagic.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def create(ros_msg_class,

members_types = _get_rosmsg_fields_as_dict(ros_msg_class)
members = {}
for s, stype in six.iteritems(members_types):
schema_instance = RosSchema()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you mean that instead of creating a child class (CustomMsgSchema) of RosSchema, and taking and instance of that class,
it is better to instantiate a RosSchema and usedirectly that instance ?
How about the second time we need that Schema, we have to create it again it seems ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to solve your source as much as possible without modifying it if possible. So I did not try various methods. Sorry and thank you of your efforts.

for s, stype in members_types.iteritems():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iteritems() is not py2/py3 compatible. please use methods that work in both.

# Note here we rely entirely on _opt_slots from the class to be set properly
# for both Nested or List representation of optional fields
ros_schema_inst = None
Expand Down Expand Up @@ -80,28 +81,24 @@ def create(ros_msg_class,
ros_schema_inst = RosNested(create(stype)) # we need to nest the next (Ros)Schema

members.setdefault(s, ros_schema_inst)
schema_instance.declared_fields[s] = ros_schema_inst
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems very hacky to me.
If marshmallow is updated and decides to change the internals of RosSchema, nothing will work and it will be hard to find this place again. Why not initializing the instance after we have gathered all members ?

schema_instance.fields[s] = ros_schema_inst

# supporting extra customization of the serialization
if pre_load_fun:
members['_helper_pre_load'] = pre_load(pre_load_fun)
schema_instance.declared_fields['_helper_pre_load'] = pre_load(pre_load_fun)
if post_load_fun:
members['_helper_post_load'] = post_load(post_load_fun)
schema_instance.declared_fields['_helper_post_load'] = post_load(post_load_fun)
if pre_dump_fun:
members['_helper_pre_dump'] = pre_dump(pre_dump_fun)
schema_instance.declared_fields['_helper_pre_dump'] = pre_dump(pre_dump_fun)
if post_dump_fun:
members['_helper_post_dump'] = post_dump(post_dump_fun)
schema_instance.declared_fields['_helper_post_dump'] = post_dump(post_dump_fun)

# adding extra members if needed
for k, v in kwargs:
members[k] = v
schema_instance.declared_fields[k] = v

members['_valid_ros_msgtype'] = ros_msg_class
members['_generated_ros_msgtype'] = ros_msg_class

MsgSchema = type(ros_msg_class.__name__ + 'Schema', (RosSchema,), members)

# Generating the schema instance
# TODO : nicer way ?
schema_instance = MsgSchema()
schema_instance._valid_ros_msgtype = ros_msg_class
schema_instance._generated_ros_msgtype = ros_msg_class

return schema_instance