-
Notifications
You must be signed in to change notification settings - Fork 1
/
packageGenerator.py
1264 lines (1139 loc) · 42.4 KB
/
packageGenerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This File reads the metamodel, reads the model and renders the code
# to be generated to for the ROS2 package. Actually implements the
# MDE transformation part, for model to code.
#
# Written in 14/4/2020
# Written by Rafael Brouzos
#
# After generating the code from the model go to the workspace root
# (folder named "workspace") in terminal and run the comands:
# $ colcon build
# $ . install/setup.bash
# $ ros2 run [package_name] [node_name]_exec
from weasyprint import HTML
from pyecore.resources import ResourceSet, URI, global_registry
from pyecore.resources.json import JsonResource
from pyecore.ecore import EClass, EAttribute
import pyecore.ecore as Ecore
from pyecoregen.ecore import EcoreGenerator
import pyecore.behavior as behavior
from pyecore.utils import DynamicEPackage
import subprocess
import os
import sys
from jinja2 import Environment, FileSystemLoader
import networkx as nx
import matplotlib.pyplot as plt
sys.path.append(os.path.join(os.path.dirname(__file__),'metamodelLib'))
# ~ import metamodel
import metageneros
# Obtain Generos Install directory
install_dir = str(os.path.dirname(__file__))
# Count arguments
if len(sys.argv) < 2 or len(sys.argv) > 4:
print ('Please give at least an XMI file name (model) and optionaly an Ecore file name (metamodel)')
sys.exit(0)
# Obtain model filename
model_filename = os.path.relpath(sys.argv[1], install_dir)
# Obtain output directory
if len(sys.argv) == 3:
destination = os.path.relpath(sys.argv[2], install_dir)
print(destination)
metamodel_filename = 'metamodelLib/metageneros.ecore'
elif len(sys.argv) == 4:
destination = os.path.relpath(sys.argv[2], install_dir)
metamodel_filename = os.path.relpath(sys.argv[3], install_dir)
else:
destination = 'workspace'
metamodel_filename = 'metamodelLib/metageneros.ecore'
# Go to working directory
if install_dir != '':
os.chdir(install_dir)
# Create rset and load Metamodel
global_registry[Ecore.nsURI] = Ecore
rset = ResourceSet()
# If you work with python package metamodel uncomment following line
# ~ rset.metamodel_registry[metamodel.nsURI] = metamodel
# if you with .ecore file metamodel uncomment following 3 lines
resource = rset.get_resource(URI(metamodel_filename))
root = resource.contents[0]
rset.metamodel_registry[root.nsURI] = root
# We obtain the model from an XMI
model_root = rset.get_resource(URI(model_filename)).contents[0]
# Create the workspace directory tree
os.system('mkdir '+destination)
os.chdir(destination)
os.system('mkdir src')
os.chdir('src')
# Create interfaces package directory tree
os.system('mkdir interfaces')
os.chdir('interfaces')
os.system('mkdir srv')
os.system('mkdir msg')
os.system('mkdir action')
os.system('mkdir documentation')
# Now the working directory is workspace/src/interfaces
os.chdir('..')
# Now the working directory is workspace/src
# Load the templates
file_loader = FileSystemLoader('../../templates')
env = Environment(loader=file_loader,trim_blocks=True, lstrip_blocks=True)
# Generate the custom interfaces
# Jinja2 Code
# Generate Topic Messages
# ___________________________________________
# Load the Template of the msg
template = env.get_template('temp_msg.msg')
allmsg = []
objects = []
# Build the msg data to pass to the Template
for t in model_root.hasSoftware.hasPackages[0].hasTopicMessages:
message_data = {}
message_data['name'] = t.name
message_data['description'] = t.description
objects = []
for o in t.hasObjectProperties:
obj = {}
obj['name'] = o.name
obj['type'] = o.datatype.type
obj['default'] = o.default
obj['constant'] = o.constant
obj['description'] = o.description
if o.datatype.__class__.__name__=="ROSData":
obj['package'] = o.datatype.package
else:
obj['package'] = "no"
objects.append(obj)
message_data['objects'] = objects
allmsg.append(message_data)
# Build msg only if it doesn't exist
if t._container.__class__.__name__=="CustomPackage":
# Fire up the rendering proccess
output = template.render(objects = objects, message_data = message_data)
# Write the generated file
dest='interfaces/msg/'+t.name+'.msg'
with open(dest, 'w') as f:
f.write(output)
# Generate Service Messages
# ___________________________________________
# Load the Template of the srv
template = env.get_template('temp_srv.srv')
# Build the srv data to pass to the Template
allsrv = []
for t in model_root.hasSoftware.hasPackages[0].hasServiceMessages:
service_data = {}
service_data['name'] = t.name
service_data['description'] = t.description
request = []
for o in t.hasRequest.hasObjectProperties:
req = {}
req['name'] = o.name
req['type'] = o.datatype.type
req['default'] = o.default
req['constant'] = o.constant
req['description'] = o.description
if o.datatype.__class__.__name__=="ROSData":
req['package'] = o.datatype.package
else:
req['package'] = "no"
request.append(req)
service_data['requests'] = request
response = []
for o in t.hasResponse.hasObjectProperties:
res = {}
res['name'] = o.name
res['type'] = o.datatype.type
res['default'] = o.default
res['constant'] = o.constant
res['description'] = o.description
if o.datatype.__class__.__name__=="ROSData":
res['package'] = o.datatype.package
else:
res['package'] = "no"
response.append(res)
service_data['responses'] = response
allsrv.append(service_data)
# Build srv only if it doesn't exist
if t._container.__class__.__name__=="CustomPackage":
# Fire up the rendering proccess
output = template.render(request = request, response = response, service_data = service_data)
# Write the generated file
dest='interfaces/srv/'+t.name+'.srv'
with open(dest, 'w') as f:
f.write(output)
# Generate Action Messages
# ___________________________________________
# Load the Template of the action
template = env.get_template('temp_action.action')
# Build the srv data to pass to the Template
allactions = []
for t in model_root.hasSoftware.hasPackages[0].hasActionInterfaces:
action_data = {}
action_data['name'] = t.name
action_data['description'] = t.description
goal = []
for o in t.hasGoal.hasObjectProperties:
g = {}
g['name'] = o.name
g['type'] = o.datatype.type
g['default'] = o.default
g['constant'] = o.constant
g['description'] = o.description
if o.datatype.__class__.__name__=="ROSData":
g['package'] = o.datatype.package
else:
g['package'] = "no"
goal.append(g)
action_data['goal'] = goal
result = []
for o in t.hasResult.hasObjectProperties:
r = {}
r['name'] = o.name
r['type'] = o.datatype.type
r['default'] = o.default
r['constant'] = o.constant
r['description'] = o.description
if o.datatype.__class__.__name__=="ROSData":
r['package'] = o.datatype.package
else:
r['package'] = "no"
result.append(r)
action_data['result'] = result
feedback = []
for o in t.hasFeedback.hasObjectProperties:
r = {}
r['name'] = o.name
r['type'] = o.datatype.type
r['default'] = o.default
r['constant'] = o.constant
r['description'] = o.description
if o.datatype.__class__.__name__=="ROSData":
r['package'] = o.datatype.package
else:
r['package'] = "no"
feedback.append(r)
action_data['feedback'] = feedback
allactions.append(action_data)
# Build action only if it doesn't exist
if t._container.__class__.__name__=="CustomPackage":
# Fire up the rendering proccess
output = template.render(goal = goal, result = result, feedback = feedback, action_data = action_data)
# Write the generated file
dest='interfaces/action/'+t.name+'.action'
with open(dest, 'w') as f:
f.write(output)
# Generate interface package CMkakeLists.txt
# ___________________________________________
# Load the Template of the CMakeLists.txt
template = env.get_template('temp_CMakeLists.txt')
# Build the msg data to pass to the Template
tmessages = []
smessages = []
amessages = []
depend = []
# Custom message interfaces
for t in model_root.hasSoftware.hasPackages[0].hasTopicMessages:
tmessages.append(t.name)
for tt in t.hasObjectProperties:
# Find packages and add dependencies from ros datatypes
if tt.datatype.__class__.__name__=="ROSData":
if tt.datatype.package not in depend:
depend.append(tt.datatype.package)
# Custom service interfaces
for t in model_root.hasSoftware.hasPackages[0].hasServiceMessages:
smessages.append(t.name)
for tt in t.hasResponse.hasObjectProperties:
# Find packages and add dependencies from ros datatypes
if tt.datatype.__class__.__name__=="ROSData":
if tt.datatype.package not in depend:
depend.append(tt.datatype.package)
for tt in t.hasRequest.hasObjectProperties:
# Find packages and add dependencies from ros datatypes
if tt.datatype.__class__.__name__=="ROSData":
if tt.datatype.package not in depend:
depend.append(tt.datatype.package)
# Custom action interfaces
for t in model_root.hasSoftware.hasPackages[0].hasActionInterfaces:
amessages.append(t.name)
for tt in t.hasGoal.hasObjectProperties:
# Find packages and add dependencies from ros datatypes
if tt.datatype.__class__.__name__=="ROSData":
if tt.datatype.package not in depend:
depend.append(tt.datatype.package)
for tt in t.hasResult.hasObjectProperties:
# Find packages and add dependencies from ros datatypes
if tt.datatype.__class__.__name__=="ROSData":
if tt.datatype.package not in depend:
depend.append(tt.datatype.package)
for tt in t.hasFeedback.hasObjectProperties:
# Find packages and add dependencies from ros datatypes
if tt.datatype.__class__.__name__=="ROSData":
if tt.datatype.package not in depend:
depend.append(tt.datatype.package)
# Fire up the rendering proccess
output = template.render(smessages=smessages, tmessages=tmessages, amessages = amessages, depend=depend)
# Write the generated file
dest='interfaces/'+'CMakeLists.txt'
with open(dest, 'w') as f:
f.write(output)
# Generate documentation.html
# ___________________________________________
# Load the Template of the documentation.html
template = env.get_template('temp_interfaces_documentation.html')
# Fire up the rendering proccess
output = template.render(allmsg = allmsg, allsrv = allsrv, allactions = allactions)
# Build the pdf of the Documentation
HTML(string=output).write_pdf("interfaces/documentation/report.pdf", stylesheets=["../../templates/temp_pdf.css"])
# Write the generated file
dest='interfaces/documentation/documentation.html'
with open(dest, 'w') as f:
f.write(output)
# Give execution permissions to the generated python file
os.chmod(dest, 509)
# Go to the workspace/src for the next package
# Generate main.css
# ___________________________________________
# Load the Template of the main.css
template = env.get_template('temp_main.css')
# Fire up the rendering proccess
output = template.render()
# Write the generated file
dest='interfaces/documentation/main.css'
with open(dest, 'w') as f:
f.write(output)
# Give execution permissions to the generated python file
os.chmod(dest, 509)
# Generate interface package package.xml
# ___________________________________________
# Load the Template of the package.xml
template = env.get_template('temp_cpppackage.xml')
# Fire up the rendering proccess
output = template.render()
# Write the generated file
dest='interfaces/'+'package.xml'
with open(dest, 'w') as f:
f.write(output)
# Build the packages
for package in model_root.hasSoftware.hasPackages:
if package.name == "interfaces" or package.name.startswith('launch') or package.builtin == True:
continue
# Now the working directory is workspace/src
# Create the package directory tree
os.system('mkdir '+package.name)
os.chdir(package.name)
# RosPackages should not be implemented
if package.__class__.__name__!="RosPackage":
# Now the working directory is workspace/src/package_name
os.system('mkdir '+package.name)
os.system('mkdir resource')
os.chdir(package.name)
os.system('touch __init__.py')
os.chdir('../resource')
os.system('touch '+package.name)
os.chdir('..')
# Now the working directory is workspace/src/package_name
# Load the templates
file_loader = FileSystemLoader('../../../templates')
env = Environment(loader=file_loader,trim_blocks=True, lstrip_blocks=True)
# Jinja2 Code
# Generate Setup.py
# ___________________________________________
# Load the Template of the setup.py
template = env.get_template('temp_setup.py')
# Build the package data to pass to the Template
pack_data = {}
pack_data['packageName'] = package.name
pack_data['maintainer'] = package.maintainer
pack_data['email'] = package.email
pack_data['description'] = package.description
pack_data['license'] = package.license
# Build the package dependencies data to pass to the Template
# Registered Dependencies
pack_depend = []
pack_depend.append("rclpy")
pack_depend.append("interfaces")
for d in package.hasDependencies:
if d.package.name not in pack_depend:
pack_depend.append(d.package.name)
# Dependencies for every node in the package
for n in package.hasNodes:
# In Subscribers using Ros Messages
for s in n.hasSubscribers:
if s.smsg.__class__.__name__=="CustomMessage":
for o in s.smsg.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
else:
if s.smsg.package not in pack_depend:
pack_depend.append(s.smsg.package)
# In Publishers using Ros Messages
for p in n.hasPublishers:
if p.pmsg.__class__.__name__=="CustomMessage":
for o in p.pmsg.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
else:
if p.pmsg.package not in pack_depend:
pack_depend.append(p.pmsg.package)
# In Servers using Ros Messages
for s in n.hasServers:
if s.servicemessage.__class__.__name__=="CustomService":
for o in s.servicemessage.hasRequest.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
for o in s.servicemessage.hasResponse.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
else:
if s.servicemessage.package not in pack_depend:
pack_depend.append(s.servicemessage.package)
# In Clients using Ros Messages
for c in n.hasClients:
if c.servicemessage.__class__.__name__=="CustomService":
for o in c.servicemessage.hasRequest.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
for o in c.servicemessage.hasResponse.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
else:
if c.servicemessage.package not in pack_depend:
pack_depend.append(c.servicemessage.package)
# In Action Clients using Action Interfaces
for c in n.hasActionClients:
if c.actioninterface.__class__.__name__=="CustomActionInterface":
for o in c.actioninterface.hasGoal.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
for o in c.actioninterface.hasResult.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
for o in c.actioninterface.hasFeedback.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
# In Action Servers using Action Interfaces
for c in n.hasActionServers:
if c.actioninterface.__class__.__name__=="CustomActionInterface":
for o in c.actioninterface.hasGoal.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
for o in c.actioninterface.hasResult.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
for o in c.actioninterface.hasFeedback.hasObjectProperties:
if o.datatype.__class__.__name__=="ROSData":
if o.datatype.package not in pack_depend:
pack_depend.append(o.datatype.package)
# Build the entry points data to pass to the Template
entry_data = []
for n in package.hasNodes:
entry_data.append(n.name+'_exec = '+package.name+'.'+n.name+'_node:main'),
for c in n.hasClients:
entry_data.append(n.name+'_'+c.name+' = '+package.name+'.'+n.name+'_node:'+'run_'+c.name),
# RosPackages should not be implemented
if package.__class__.__name__!="RosPackage":
# Fire up the rendering proccess
output = template.render(pack=pack_data, entry_points=entry_data)
# Write the generated file
dest='setup.py'
with open(dest, 'w') as f:
f.write(output)
# Give execution permissions to the generated python file
os.chmod(dest, 509)
# Generate package.xml
# ___________________________________________
# Load the Template of the package.xml
template = env.get_template('temp_package.xml')
# RosPackages should not be implemented
if package.__class__.__name__!="RosPackage":
# Fire up the rendering proccess
output = template.render(pack=pack_data, pack_depend=pack_depend)
# Write the generated file
dest='package.xml'
with open(dest, 'w') as f:
f.write(output)
# Give execution permissions to the generated python file
os.chmod(dest, 509)
# Generate setup.cfg
# ___________________________________________
# Load the Template of the setup.cfg
template = env.get_template('temp_setup.cfg')
# RosPackages should not be implemented
if package.__class__.__name__!="RosPackage":
# Fire up the rendering proccess
output = template.render(pack=pack_data)
# Write the generated file
dest='setup.cfg'
with open(dest, 'w') as f:
f.write(output)
# Give execution permissions to the generated python file
os.chmod(dest, 509)
# Generate nodes
# ___________________________________________
allnodes = []
for node in package.hasNodes:
# Load the Template of a node
template = env.get_template('temp_node.py')
# Build the node data to pass to the Template
node_data = {}
node_data['name'] = node.name
node_data['namespace'] = node.namespace
# Build the parameter data to pass to the Template
params = []
for p in node.hasParameters:
param = {}
param['name'] = p.name
param['value'] = p.value
param['type'] = p.type
param['type2'] = p.type.value
param['description'] = p.description
params.append(param)
node_data['param'] = params
# Build the publisher/subscriber data to pass to the Template
subscribers = []
publishers = []
types = []
extra_imports = []
for s in node.hasSubscribers:
smsgObj = []
sub = {}
sub['name'] = s.name
sub['topicPath'] = s.topicPath
sub['qos'] = 10
# Add QoS profile
if s.qosprofile.__class__.__name__=="CustomQosProfile":
profile = {}
profile['history'] = s.qosprofile.history
profile['reliability'] = s.qosprofile.reliability
profile['durability'] = s.qosprofile.durability
profile['depth'] = s.qosprofile.depth
profile['liveliness'] = s.qosprofile.liveliness
profile['deadlineSec'] = s.qosprofile.deadlineSec
profile['deadlineNSec'] = s.qosprofile.deadlineNSec
profile['lifespanSec'] = s.qosprofile.lifespanSec
profile['lifespanNSec'] = s.qosprofile.lifespanNSec
profile['liveliness_lease_durationSec'] = s.qosprofile.liveliness_lease_durationSec
profile['liveliness_lease_durationNSec'] = s.qosprofile.liveliness_lease_durationNSec
profile['avoid_ros_namespace_conventions'] = s.qosprofile.avoid_ros_namespace_conventions
elif s.qosprofile.__class__.__name__=="RosQosProfile":
profile = {}
profile['history'] = "standart"
profile['name'] = s.qosprofile.name
else:
profile = {}
profile['history'] = "default"
sub['profile'] = profile
# Add msg objects
if s.smsg.name in types:
sub['unique'] = 0
else:
sub['unique'] = 1
sub['type'] = s.smsg.name
types.append(s.smsg.name)
if s.smsg.__class__.__name__=="CustomMessage":
sub['package'] = 'interfaces'
for r in s.smsg.hasObjectProperties:
smsgObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
else:
smsgObj.append(s.smsg.name)
sub['package'] = s.smsg.package
#TODO append the Ros subscriber parameters
sub['msg'] = smsgObj
subscribers.append(sub)
node_data['subscribers'] = subscribers
for p in node.hasPublishers:
pmsgObj = []
pub = {}
pub['name'] = p.name
pub['topicPath'] = p.topicPath
pub['publishRate'] = p.publishRate
pub['qos'] = 10
# Add QoS profile
if p.qosprofile.__class__.__name__=="CustomQosProfile":
profile = {}
profile['history'] = p.qosprofile.history
profile['reliability'] = p.qosprofile.reliability
profile['durability'] = p.qosprofile.durability
profile['depth'] = p.qosprofile.depth
profile['liveliness'] = p.qosprofile.liveliness
profile['deadlineSec'] = p.qosprofile.deadlineSec
profile['deadlineNSec'] = p.qosprofile.deadlineNSec
profile['lifespanSec'] = p.qosprofile.lifespanSec
profile['lifespanNSec'] = p.qosprofile.lifespanNSec
profile['liveliness_lease_durationSec'] = p.qosprofile.liveliness_lease_durationSec
profile['liveliness_lease_durationNSec'] = p.qosprofile.liveliness_lease_durationNSec
profile['avoid_ros_namespace_conventions'] = p.qosprofile.avoid_ros_namespace_conventions
elif p.qosprofile.__class__.__name__=="RosQosProfile":
profile = {}
profile['history'] = "standart"
profile['name'] = p.qosprofile.name
else:
profile = {}
profile['history'] = "default"
pub['profile'] = profile
# Add msg objects
if p.pmsg.name in types:
pub['unique'] = 0
else:
pub['unique'] = 1
pub['type'] = p.pmsg.name
types.append(p.pmsg.name)
if p.pmsg.__class__.__name__=="CustomMessage":
pub['package'] = 'interfaces'
for r in p.pmsg.hasObjectProperties:
pmsgObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
else:
pmsgObj.append(p.pmsg.name)
pub['package'] = p.pmsg.package
#TODO append the Ros publisher parameters
pub['msg'] = pmsgObj
publishers.append(pub)
node_data['publishers'] = publishers
# Build the servers/clients data to pass to the Template
servers = []
clients = []
for s in node.hasServers:
srequestObj = []
sresponseObj = []
ser = {}
ser['name'] = s.name
ser['servicePath'] = s.servicePath
ser['serviceName'] = s.serviceName
# Add QoS profile
if s.qosprofile.__class__.__name__=="CustomQosProfile":
profile = {}
profile['history'] = s.qosprofile.history
profile['reliability'] = s.qosprofile.reliability
profile['durability'] = s.qosprofile.durability
profile['depth'] = s.qosprofile.depth
profile['liveliness'] = s.qosprofile.liveliness
profile['deadlineSec'] = s.qosprofile.deadlineSec
profile['deadlineNSec'] = s.qosprofile.deadlineNSec
profile['lifespanSec'] = s.qosprofile.lifespanSec
profile['lifespanNSec'] = s.qosprofile.lifespanNSec
profile['liveliness_lease_durationSec'] = s.qosprofile.liveliness_lease_durationSec
profile['liveliness_lease_durationNSec'] = s.qosprofile.liveliness_lease_durationNSec
profile['avoid_ros_namespace_conventions'] = s.qosprofile.avoid_ros_namespace_conventions
elif s.qosprofile.__class__.__name__=="RosQosProfile":
profile = {}
profile['history'] = "standart"
profile['name'] = s.qosprofile.name
else:
profile = {}
profile['history'] = "default"
ser['profile'] = profile
# Add srv objects
if s.servicemessage.name in types:
ser['unique'] = 0
else:
ser['unique'] = 1
ser['type'] = s.servicemessage.name
types.append(s.servicemessage.name)
if s.servicemessage.__class__.__name__=="CustomService":
ser['package'] = 'interfaces'
for r in s.servicemessage.hasRequest.hasObjectProperties:
srequestObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
for r in s.servicemessage.hasResponse.hasObjectProperties:
sresponseObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
else:
srequestObj.append(s.servicemessage.name)
ser['package'] = s.servicemessage.package
#TODO append the Ros service parameters
ser['requests'] = srequestObj
ser['responses'] = sresponseObj
servers.append(ser)
node_data['servers'] = servers
for c in node.hasClients:
crequestObj = []
cresponseObj = []
cli = {}
cli['name'] = c.name
cli['servicePath'] = c.servicePath
cli['serviceName'] = c.serviceName
# Add QoS profile
if c.qosprofile.__class__.__name__=="CustomQosProfile":
profile = {}
profile['history'] = c.qosprofile.history
profile['reliability'] = c.qosprofile.reliability
profile['durability'] = c.qosprofile.durability
profile['depth'] = c.qosprofile.depth
profile['liveliness'] = c.qosprofile.liveliness
profile['deadlineSec'] = c.qosprofile.deadlineSec
profile['deadlineNSec'] = c.qosprofile.deadlineNSec
profile['lifespanSec'] = c.qosprofile.lifespanSec
profile['lifespanNSec'] = c.qosprofile.lifespanNSec
profile['liveliness_lease_durationSec'] = c.qosprofile.liveliness_lease_durationSec
profile['liveliness_lease_durationNSec'] = c.qosprofile.liveliness_lease_durationNSec
profile['avoid_ros_namespace_conventions'] = c.qosprofile.avoid_ros_namespace_conventions
elif c.qosprofile.__class__.__name__=="RosQosProfile":
profile = {}
profile['history'] = "standart"
profile['name'] = c.qosprofile.name
else:
profile = {}
profile['history'] = "default"
cli['profile'] = profile
# Add srv objects
if c.servicemessage.name in types:
cli['unique'] = 0
else:
cli['unique'] = 1
cli['type'] = c.servicemessage.name
types.append(c.servicemessage.name)
if c.servicemessage.__class__.__name__=="CustomService":
cli['package'] = 'interfaces'
for r in c.servicemessage.hasRequest.hasObjectProperties:
crequestObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
for r in c.servicemessage.hasResponse.hasObjectProperties:
cresponseObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
else:
crequestObj.append(c.servicemessage.name)
cli['package'] = c.servicemessage.package
#TODO append the Ros client parameters
cli['requests'] = crequestObj
cli['responses'] = cresponseObj
clients.append(cli)
node_data['clients'] = clients
# Build the action servers/clients data to pass to the Template
action_servers = []
action_clients = []
for s in node.hasActionServers:
sgoalObj = []
sresultObj = []
sfeedbackObj = []
ser = {}
ser['name'] = s.name
if s.actioninterface.name in types:
ser['unique'] = 0
else:
ser['unique'] = 1
ser['type'] = s.actioninterface.name
types.append(s.actioninterface.name)
if s.actioninterface.__class__.__name__=="CustomActionInterface":
ser['package'] = 'interfaces'
for r in s.actioninterface.hasGoal.hasObjectProperties:
sgoalObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
for r in s.actioninterface.hasResult.hasObjectProperties:
sresultObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
for r in s.actioninterface.hasFeedback.hasObjectProperties:
sfeedbackObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
else:
ser['package'] = s.actioninterface.package
#TODO append the Ros action parameters
ser['goal'] = sgoalObj
ser['result'] = sresultObj
ser['feedback'] = sfeedbackObj
action_servers.append(ser)
node_data['action_servers'] = action_servers
for c in node.hasActionClients:
cgoalObj = []
cresultObj = []
cfeedbackObj = []
cli = {}
cli['name'] = c.name
if c.actioninterface.name in types:
cli['unique'] = 0
else:
cli['unique'] = 1
cli['type'] = c.actioninterface.name
types.append(c.actioninterface.name)
if c.actioninterface.__class__.__name__=="CustomActionInterface":
cli['package'] = 'interfaces'
for r in c.actioninterface.hasGoal.hasObjectProperties:
cgoalObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
for r in c.actioninterface.hasResult.hasObjectProperties:
cresultObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
for r in c.actioninterface.hasFeedback.hasObjectProperties:
cfeedbackObj.append(r.name)
if r.datatype.__class__.__name__=="ROSData" and r.datatype.type not in types:
imp = {}
imp['type'] = r.datatype.type
imp['package'] = r.datatype.package
extra_imports.append(imp)
types.append(r.datatype.type)
else:
cli['package'] = c.actioninterface.package
#TODO append the Ros action parameters
cli['goal'] = cgoalObj
cli['result'] = cresultObj
cli['feedback'] = cfeedbackObj
action_clients.append(cli)
node_data['action_clients'] = action_clients
allnodes.append(node_data)
# RosPackages should not be implemented
if package.__class__.__name__!="RosPackage":
# Fire up the rendering proccess
output = template.render(pack = pack_data, node = node_data, publishers = publishers,
subscribers = subscribers, objects = objects, smessages=smessages, tmessages=tmessages,
servers = servers, clients=clients, params = params,extra_imports=extra_imports,
action_clients = action_clients, action_servers = action_servers)
# Write the generated file
dest=package.name+'/'+node.name+'_node.py'
with open(dest, 'w') as f:
f.write(output)
# Give execution permissions to the generated python file
os.chmod(dest, 509)
# Generate documentation.html
# ___________________________________________
# Load the Template of the documentation.html
template = env.get_template('temp_documentation.html')
# Fire up the rendering proccess
output = template.render(pack=pack_data, allmsg = allmsg, allnodes = allnodes)
os.system('mkdir documentation')
# Build the pdf of the Documentation
HTML(string=output).write_pdf("documentation/report.pdf", stylesheets=["../../../templates/temp_pdf.css"])
# Write the generated file
dest='documentation/documentation.html'
with open(dest, 'w') as f:
f.write(output)
# Give execution permissions to the generated python file
os.chmod(dest, 509)
# Generate main.css
# ___________________________________________
# Load the Template of the main.css
template = env.get_template('temp_main.css')
# Fire up the rendering proccess
output = template.render()
# Write the generated file
dest='documentation/main.css'
with open(dest, 'w') as f:
f.write(output)
# Give execution permissions to the generated python file
os.chmod(dest, 509)
# Go to the workspace/src for the next package
os.chdir('..')
# Generate the Deployment package
# Jinja2 Code
# Generate Host packages
# ___________________________________________
# Generate package.xml
# ___________________________________________
# Go to launchers package directory
for package in model_root.hasSoftware.hasPackages:
if not package.name.startswith('launch'):
continue
# Now the working directory is workspace/src
# Create the package directory tree
os.system('mkdir '+package.name)
os.chdir(package.name)
# RosPackages should not be implemented
if package.__class__.__name__!="RosPackage":
# Now the working directory is workspace/src/package_name
os.system('mkdir '+package.name)
os.system('mkdir resource')
os.chdir(package.name)
os.system('touch __init__.py')
os.chdir('../resource')
os.system('touch '+package.name)
os.chdir('..')
# Now the working directory is workspace/src/package_name
# Load the templates
file_loader = FileSystemLoader('../../../templates')
env = Environment(loader=file_loader,trim_blocks=True, lstrip_blocks=True)
# Load the Template of the package.xml
template = env.get_template('temp_package.xml')
# Build the package data to pass to the Template