Skip to content

Commit 6f45d46

Browse files
store types as constant and return ordered dict
Signed-off-by: Mikael Arguedas <[email protected]>
1 parent abbb86e commit 6f45d46

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

rosidl_generator_py/resource/_idl.py.em

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616
@#######################################################################
1717
@{
1818
from rosidl_parser.definition import Message
19+
msg_required_import_imported = False
1920
}@
2021
@[for message in content.get_elements_of_type(Message)]@
22+
@[ if not msg_required_import_imported]@
23+
@{msg_required_import_imported = True}@
24+
from collections import OrderedDict
25+
26+
import rosidl_parser.definition
27+
@[ end if]@
2128
@{
2229
TEMPLATE(
2330
'_msg.py.em',
@@ -33,6 +40,12 @@ TEMPLATE(
3340
from rosidl_parser.definition import Service
3441
}@
3542
@[for service in content.get_elements_of_type(Service)]@
43+
@[ if not msg_required_import_imported]@
44+
@{msg_required_import_imported = True}@
45+
from collections import OrderedDict
46+
47+
import rosidl_parser.definition
48+
@[ end if]@
3649
@{
3750
TEMPLATE(
3851
'_srv.py.em',
@@ -47,6 +60,12 @@ TEMPLATE(
4760
from rosidl_parser.definition import Action
4861
}@
4962
@[for action in content.get_elements_of_type(Action)]@
63+
@[ if not msg_required_import_imported]@
64+
@{msg_required_import_imported = True}@
65+
from collections import OrderedDict
66+
67+
import rosidl_parser.definition
68+
@[ end if]@
5069
@{
5170
TEMPLATE(
5271
'_action.py.em',

rosidl_generator_py/resource/_msg.py.em

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ from rosidl_parser.definition import NamespacedType
1616
from rosidl_parser.definition import NestedType
1717
from rosidl_parser.definition import Sequence
1818
from rosidl_parser.definition import String
19+
from rosidl_parser.definition import UnboundedSequence
1920
from rosidl_parser.definition import WString
2021
}@
2122

@@ -135,45 +136,44 @@ class @(message.structure.type.name)(metaclass=Metaclass_@(message.structure.typ
135136
@[end for]@
136137
]
137138

138-
_fields_and_field_types = {
139+
SLOT_TYPES = (
139140
@[for member in message.structure.members]@
140141
@{
141142
type_ = member.type
142143
if isinstance(type_, NestedType):
143144
type_ = type_.basetype
144145
}@
145-
'@(member.name)': '@
146-
@# the prefix for nested types
147-
@[ if isinstance(member.type, Sequence)]@
148-
sequence<@
146+
@
147+
@[ if isinstance(member.type, BoundedSequence)]@
148+
rosidl_parser.definition.BoundedSequence(@
149+
@[ elif isinstance(member.type, UnboundedSequence)]@
150+
rosidl_parser.definition.UnboundedSequence(@
151+
@[ elif isinstance(member.type, Array)]@
152+
rosidl_parser.definition.Array(@
149153
@[ end if]@
150154
@# the typename of the non-nested type or the nested basetype
151155
@[ if isinstance(type_, BasicType)]@
152-
@(type_.type)@
156+
rosidl_parser.definition.BasicType('@(type_.type)')@
153157
@[ elif isinstance(type_, BaseString)]@
154-
@
155158
@[ if isinstance(type_, WString)]@
156-
w@
157-
@[ end if]@
158-
string@
159-
@[ if type_.maximum_size is not None]@
160-
<@(type_.maximum_size)>@
159+
rosidl_parser.definition.WString('@
160+
@[ else]@
161+
rosidl_parser.definition.String('@
161162
@[ end if]@
163+
@(str(type_.maximum_size))')@
162164
@[ elif isinstance(type_, NamespacedType)]@
163-
@('/'.join([type_.namespaces[0], type_.name]))@
165+
rosidl_parser.definition.NamespacedType(['@("', '".join(type_.namespaces))'], '@(type_.name)')@
164166
@[ end if]@
165-
@# the suffix for nested types
166-
@[ if isinstance(member.type, Sequence)]@
167-
@[ if isinstance(member.type, BoundedSequence)]@
168-
, @(member.type.upper_bound)@
169-
@[ end if]@
170-
>@
167+
@[ if isinstance(member.type, BoundedSequence)]@
168+
, @(member.type.upper_bound))@
171169
@[ elif isinstance(member.type, Array)]@
172-
[@(member.type.size)]@
170+
, @(member.type.size))@
171+
@[ elif isinstance(member.type, UnboundedSequence)]@
172+
)@
173173
@[ end if]@
174-
',
174+
,
175175
@[end for]@
176-
}
176+
)
177177
178178
def __init__(self, **kwargs):
179179
assert all('_' + key in self.__slots__ for key in kwargs.keys()), \
@@ -246,9 +246,8 @@ if isinstance(type_, NestedType):
246246
return True
247247
248248
@@classmethod
249-
def get_fields_and_field_types(cls):
250-
from copy import copy
251-
return copy(cls._fields_and_field_types)
249+
def get_slot_types(cls):
250+
return OrderedDict(zip([s[1:] for s in cls.__slots__], cls.SLOT_TYPES))
252251
@[for member in message.structure.members]@
253252
254253
@{

0 commit comments

Comments
 (0)