-
Notifications
You must be signed in to change notification settings - Fork 0
/
poris2ruby.py
867 lines (690 loc) · 39.9 KB
/
poris2ruby.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
# Importing auxiliar libraries for the test
import argparse # This library allows us to easily parse the command line arguments
from pyexcel_ods import get_data # This function allows us to easily read an ODS file (for api)
from xml.dom import minidom
from pathlib import Path
import os
# Importing test configuration file
import config
savemem = config.savemem
def nametoidl(n):
ret = n.replace('(','_')
ret = ret.replace(')','_')
ret = ret.replace('.','_')
ret = ret.replace('+','p')
ret = ret.replace('/','_')
ret = ret.replace('¿','_')
ret = ret.replace('?','_')
ret = ret.replace('-','_')
ret = ret.replace('á','a')
ret = ret.replace('é','e')
ret = ret.replace('í','i')
ret = ret.replace('ó','o')
ret = ret.replace('ú','u')
ret = ret.replace('Á','A')
ret = ret.replace('É','E')
ret = ret.replace('Í','I')
ret = ret.replace('Ó','O')
ret = ret.replace('Ú','U')
ret = ret.replace('ñ','ny')
ret = ret.replace('Ñ','NY')
if (ret.lower() == 'sequence'):
ret = ret+"b"
return ret
def desctomonit(n):
return nametoidl(n.split('\n')[0].split('\r')[0])
######### WE WILL PARSE THE COMMAND LINE ARGUMENTS FOR THE WRAPPER GEN #############
def dir_path(string):
if os.path.isdir(string):
return string
else:
raise NotADirectoryError(string)
parser = argparse.ArgumentParser(description='Launches a PORIS device generation from an ODS file describing the PORIS instrument')
## The second argument is the api ODS file
parser.add_argument('model_root_path',type=dir_path, help="the root path where the models shall be picket from")
parser.add_argument('model_path',type=argparse.FileType('r'), help="the path for the model to be processed")
parser.add_argument('output_path',type=dir_path, help="the path to create the Python classes")
# Obtaining the arguments from the command line
args=parser.parse_args()
# Printing the obtained arguments:
print("/* The PORIS instrument description ODS filename is:",args.model_path.name+" */")
deviceName = Path(args.model_path.name).stem
print("Device name:",deviceName)
relativepath = args.model_path.name.replace(args.model_root_path, '', 1)
relativepath = ''.join(relativepath.rsplit(".ods", 1))
# As an example of a constant defined in the configuration file, we'll print the welcome message
print(config.welcome_message)
def loadODS():
dictdata = get_data(args.model_path.name,start_row=config.dict_max_rows_row, row_limit=config.dict_max_rows_row+1,
start_column=config.dict_max_rows_column,column_limit=config.dict_max_rows_column)[config.dict_file_sheet]
desc_file_row_limit = dictdata[0][1]
# Now we read the PORIS instrument description from the file file
filedata = get_data(args.model_path.name,start_row=config.desc_file_start_row, row_limit=desc_file_row_limit,
start_column=config.desc_file_start_column,column_limit=config.desc_file_column_limit)
nodesdata = filedata[config.desc_file_sheet]
virtual_nodes_counter = 1
nodes_dict = {}
virtual_nodes = {}
rowcount = 0
for row in nodesdata:
if (len(row)>1):
rowcount += 1
print("row",row)
thiskey = row[config.desc_ident_column]
if (len(thiskey)>0):
# print(thiskey)
thisnode = {}
thisnode['ident'] = thiskey
# In case we came from graphml, this column could be empty
thisid = str(row[config.desc_id_column])
if len(thisid) <= 0:
# TODO: Find a more robust strategy to generate identifiers
# out of the Redmine space
thisid = 2000000000+rowcount
thisnode['id'] = thisid
thisnode['subject'] = row[config.desc_subject_column]
thisnode['description'] = row[config.desc_description_column]
thisnode['parent'] = row[config.desc_parent_column]
blockingstr = row[config.desc_blocking_column]
thisnode['tracker'] = row[config.desc_tracker_column]
thisnode['blocking'] = []
thisnode['virtual'] = False
if blockingstr is not None and len(blockingstr)>0:
thisnode['blocking'] = [x.strip() for x in blockingstr.split(',')]
else:
if thisnode['tracker'] == "prMode":
# The mode can not hold no destinations because the player will show an empty slot,
# we will create a virtual value for it
virtnode = {}
virtid = str(-virtual_nodes_counter)
virtual_nodes_counter += 1
virtident = "VIRT"+virtid
virtnode['ident'] = virtident
virtnode['id'] = virtid
virtnode['tracker'] = "prValue"
virtnode['subject'] = thisnode['subject']
virtnode['description'] = thisnode['description']
virtnode['parent'] = thisnode['parent']
virtnode['blocking'] = []
virtnode['precedents'] = []
virtnode['children'] = []
virtnode['virtual'] = True
nodes_dict[virtident] = virtnode
thisnode['blocking'] = [virtident]
else:
thisnode['blocking'] = []
# print("Parsed:",thisnode['blocking'])
thisnode['min'] = row[config.desc_min_column]
thisnode['default_data'] = row[config.desc_def_column]
thisnode['max'] = row[config.desc_max_column]
thisnode['deftext'] = row[config.desc_deftext_column]
precedentstr = row[config.desc_precedents_column]
thisnode['precedents'] = []
if precedentstr is not None:
thisnode['precedents'] = [x.strip() for x in precedentstr.split(',')]
else:
thisnode['precedents'] = []
thisnode['children'] = []
nodes_dict[thiskey] = thisnode
# Relationships
for thiskey in nodes_dict.keys():
thisnode = nodes_dict[thiskey]
# Parent relationship
if thisnode['parent'] is not None:
parentkey = thisnode['parent']
if len(parentkey) > 0:
if parentkey in nodes_dict.keys():
thisparent = nodes_dict[parentkey]
thisnode['parentnode'] = thisparent
thisparent['children'].append(thiskey)
nodes_to_add = {}
'''
##### Removing Engineering
if not savemem:
# Create engineering modes
for thiskey in nodes_dict.keys():
thisnode = nodes_dict[thiskey]
if thisnode['virtual'] == False:
if thisnode['tracker'] == "prSys":
# Now, for each node of type prSys we must
# create a new mode, engineering
virtnode = {}
virtid = str(-virtual_nodes_counter)
virtual_nodes_counter += 1
virtident = "ENG"+virtid
virtnode['ident'] = virtident
virtnode['id'] = virtid
virtnode['tracker'] = "prMode"
virtnode['subject'] = "Engineering"
virtnode['description'] = thisnode['subject']+" engineering mode"
virtnode['parent'] = thiskey
virtnode['parentnode'] = thisnode
virtnode['blocking'] = []
virtnode['precedents'] = []
virtnode['children'] = []
virtnode['virtual'] = False
# The engineering mode will be blocked by all the modes of its children
for ch0 in thisnode['children']:
if nodes_dict[ch0]['tracker'] == "prSys" or nodes_dict[ch0]['tracker'] == "prParam":
for ch in nodes_dict[ch0]['children']:
# If it is a virtual node, it can be not in nodes_dict, but in nodes_to_add
if ch in nodes_dict.keys():
if nodes_dict[ch]['tracker'] == "prMode":
if nodes_dict[ch]['virtual'] == False:
virtnode['blocking'] += [ch]
print(ch,nodes_dict[ch]['subject'],"bloquea a",virtident,virtnode['description'])
#print(virtnode['blocking'])
else:
if nodes_to_add[ch]['virtual'] == False:
print("CHECK THIS: A NON VIRTUAL NODE NOT IN NODES_DICT")
if len(virtnode['blocking']) > 0:
nodes_to_add[virtident] = virtnode
thisnode['children'].append(virtident)
'''
tree_dict = {}
while len(tree_dict) < len(nodes_dict):
for k in nodes_dict.keys():
if k not in tree_dict.keys():
if 'parentnode' not in nodes_dict[k].keys():
tree_dict[k] = nodes_dict[k]
print("Adding ROOT",tree_dict[k]['subject'])
else:
if nodes_dict[k]['parent'] in tree_dict.keys():
tree_dict[k] = nodes_dict[k]
print("Adding NODE",tree_dict[k]['parentnode']['subject'],":",tree_dict[k]['subject'])
for n in nodes_to_add.keys():
# Tengo que añadir el bloqueo de los modos de ingeniería
# hijos a los modos de ingeniería padres
virtnode = nodes_to_add[n]
thisparent = virtnode['parentnode']
for c in thisparent['children']:
if c != n and thisparent["tracker"] == "prSys":
for c2 in tree_dict[c]['children']:
#print(c2,"va a",n,"?")
if c2 in nodes_to_add.keys():
print(c2,nodes_to_add[c2]['description'],"bloquea a",n,virtnode['description'])
virtnode['blocking'] += [c2]
#print(virtnode['blocking'])
for n in nodes_to_add.keys():
#print("Añado el nodo ",n)
#print(nodes_to_add[n])
tree_dict[n] = nodes_to_add[n]
for k in tree_dict.keys():
n = tree_dict[k]
print(k,n['subject'])
return tree_dict
# This function will create, in parallel:
# - The $S1.rb
# - The $S1PORIS.rb
methods_dict = {}
def createPythonCode(nodes_dict,deviceName,output_path: str,relative_path: str):
global methods_dict
methodsstr = " #----------------------------------------------------------------------\n"
methodsstr += " # Specific methods\n"
methodsstr += " #----------------------------------------------------------------------\n"
porishstr = "require_relative 'PORIS'\n\n"
porishstr += "class "+deviceName+"PORIS < PORISDoc\n"
porishstr += " def initialize(project_id)\n"
porishstr += " super(project_id)\n"
poriscstr = ""
poriscinitstr = ""
poriscinitrelstr = ""
for thiskey in nodes_dict.keys():
thisnode = nodes_dict[thiskey]
nodename = nametoidl(thisnode['subject'])
thisclass = thisnode['tracker']
if thisclass == "prParam" or thisclass == "prSys":
enumname = "enum_"+nodename
enummodename = enumname+"Mode"
choicesname = nodename+"ChoiceList"
nodevar = "enum"+nodename
modevar = "enum"+nodename+"Mode"
modevar_ = modevar+"_"
nodevar_ = nodevar+"_"
'''
/** Process enum */
enum_EnumMon processEnum(enum_EnumMon value);
'''
if thisclass == "prParam":
# PORISParam prBinning;
porishstr += " @pr"+nodename+" = PORISParam.new(\""+nodename+"\")\n"
'''
##### Removing UNKNOWN
if not savemem:
porishstr += " @md"+nodename+"Mode_UNKNOWN = PORISMode.new(\""+nodename+"Mode_UNKNOWN\")\n"
porishstr += " @vl"+nodename+"_UNKNOWN = PORISValue.new(\""+nodename+"_UNKNOWN\")\n"
else:
porishstr += " @md"+nodename+"UNKNOWN = PORISMode.new(\"UNKNOWN\")\n"
porishstr += " @vl"+nodename+"_UNKNOWN = PORISValue.new(\"UNKNOWN\")\n"
'''
methodsstr += "\n ## "+thisclass+" "+nodename+" \n"
methodsstr += "\n # "+nodename+"\n"
methodsstr += " def get_"+nodename+"Node\n"
methodsstr += " @pr"+nodename+"\n\tend\n\n"
methodsstr += " def get_"+nodename+"\n"
methodsstr += " @pr"+nodename+".getSelectedValue\n\tend\n\n"
methodsstr += " def set_"+nodename+"(value)\n"
methodsstr += " @pr"+nodename+".setValue(value)\n\tend\n"
'''
prBinning.id = idcounter++;
prBinning.idx = 1;
prBinning.ident = "Binning";
prBinning.name = "Binning";
prBinning.description = "Parametro de binning";
prBinning.parent = &prDetector;
prDetector.params.push_back(&prBinning);
prBinning_UNKNOWN.id = idcounter++;
prBinning_UNKNOWN.idx = MyDASDevice::Binning_UNKNOWN;
prBinning_UNKNOWN.ident = "Binning_UNKNOWN";
prBinning_UNKNOWN.name = "Binning_UNKNOWN";
prBinning_UNKNOWN.description = "Binning_UNKNOWN";
prBinning_UNKNOWN.parent = &prBinning;
prBinning.values.push_back(&prBinning_UNKNOWN);
prBinningMode_UNKNOWN.id = idcounter++;
prBinningMode_UNKNOWN.idx = MyDASDevice::BinningMode_UNKNOWN;
prBinningMode_UNKNOWN.ident = "BinningMode_UNKNOWN";
prBinningMode_UNKNOWN.name = "BinningMode_UNKNOWN";
prBinningMode_UNKNOWN.description = "BinningMode_UNKNOWN";
prBinningMode_UNKNOWN.parent = &prBinning;
prBinning.modes.push_back(&prBinningMode_UNKNOWN);
prBinningMode_UNKNOWN.values.push_back(&prBinning_UNKNOWN);
prDetectorMode_UNKNOWN.submodes.push_back(&prBinningMode_UNKNOWN);
'''
parentNode = None
parentNodeName = ""
if 'parentnode' in thisnode.keys():
parentNode = thisnode['parentnode']
parentNodeName = nametoidl(parentNode['subject'])
if 'paramcounter' in parentNode.keys():
counter = parentNode['paramcounter']
else:
counter = 0
parentNode['paramcounter'] = counter + 1
else:
counter = 0
poriscinitstr += " self.addItem(@pr"+nodename+")\n"
if not savemem:
poriscinitstr += " @pr"+nodename+".setIdent(\""+thisnode['ident']+"\")\n"
poriscinitstr += " @pr"+nodename+".setDescription(\""+desctomonit(thisnode['description'])+"\")\n"
if parentNode is not None:
poriscinitstr += " @sys"+parentNodeName+".addParam(@pr"+nodename+")\n"
'''
##### Removing UNKNOWN
if not savemem:
poriscinitstr += " self.addItem(@vl"+nodename+"_UNKNOWN)\n"
if not savemem:
poriscinitstr += " @vl"+nodename+ "_UNKNOWN.setIdent(\"UNK_"+thisnode['ident']+"\")\n"
poriscinitstr += " @vl"+nodename+ "_UNKNOWN.setDescription(\"Unknown value for "+nodename+"\")\n"
poriscinitstr += " @pr"+nodename+".addValue(@vl"+nodename+ "_UNKNOWN)\n"
if not savemem:
poriscinitstr += " self.addItem(@md"+nodename+"Mode_UNKNOWN)\n"
else:
poriscinitstr += " self.addItem(@md"+nodename+"UNKNOWN)\n"
if not savemem:
poriscinitstr += " @md"+nodename+ "Mode_UNKNOWN.setIdent(\"UNKM_"+thisnode['ident']+"\")\n"
poriscinitstr += " @md"+nodename+ "Mode_UNKNOWN.setDescription(\"Unknown mode for "+nodename+"\")\n"
if not savemem:
poriscinitstr += " @pr"+nodename+".addMode(@md"+nodename+ "Mode_UNKNOWN)\n"
poriscinitstr += " @md"+nodename+ "Mode_UNKNOWN.addValue(@vl"+nodename+ "_UNKNOWN)\n"
poriscinitstr += " @md"+parentNodeName+ "Mode_UNKNOWN.addSubMode(@md"+nodename+ "Mode_UNKNOWN)\n"
else:
poriscinitstr += " @pr"+nodename+".addMode(@md"+nodename+ "UNKNOWN)\n"
poriscinitstr += " @md"+nodename+ "UNKNOWN.addValue(@vl"+nodename+ "_UNKNOWN)\n"
poriscinitstr += " @md"+parentNodeName+ "UNKNOWN.addSubMode(@md"+nodename+ "UNKNOWN)\n"
'''
methodsstr += "\n ## "+nodename+"Mode \n"
methodsstr += " def get_"+nodename+"Mode\n"
#@sysARCGenIII.getSelectedMode
methodsstr += " @pr"+nodename+".getSelectedMode\n\tend\n\n"
methodsstr += " def set_"+nodename+"Mode(mode)\n"
methodsstr += " @pr"+nodename+".selectMode(mode)\n\tend\n\n"
else:
# PORISSys prDetector;
porishstr += " @sys"+nodename+" = PORISSys.new(\""+nodename+"\")\n"
'''
##### Removing UNKNOWN
if not savemem:
porishstr += " @md"+nodename+"Mode_UNKNOWN = PORISMode.new(\""+nodename+"Mode_UNKNOWN\")\n"
else:
porishstr += " @md"+nodename+"UNKNOWN = PORISMode.new(\"UNKNOWN\")\n"
'''
parentNode = None
parentNodeName = ""
if 'parentnode' in thisnode.keys():
parentNode = thisnode['parentnode']
parentNodeName = nametoidl(parentNode['subject'])
if 'syscounter' in parentNode.keys():
counter = parentNode['syscounter']
else:
counter = 0
parentNode['syscounter'] = counter + 1
else:
counter = 0
porishstr += " self.setRoot(@sys"+nodename+")\n"
'''
prDetector.id = idcounter++;
prDetector.idx = 0;
prDetector.ident = "Detector";
prDetector.name = "Detector";
prDetector.description = "Detector";
prDetector.parent = NULL;
'''
poriscinitstr += " self.addItem(@sys"+nodename+")\n"
if not savemem:
poriscinitstr += " @sys"+nodename+".setIdent(\""+thisnode['ident']+"\")\n"
poriscinitstr += " @sys"+nodename+".setDescription(\""+desctomonit(thisnode['description'])+"\")\n"
if parentNode is not None:
poriscinitstr += " @sys"+parentNodeName+".addSubsystem(@sys"+nodename+")\n"
'''
##### Removing UNKNOWN
if not savemem:
poriscinitstr += " self.addItem(@md"+nodename+"Mode_UNKNOWN)\n"
else:
poriscinitstr += " self.addItem(@md"+nodename+"UNKNOWN)\n"
if not savemem:
poriscinitstr += " @md"+nodename+ "Mode_UNKNOWN.setIdent(\"UNKM_"+thisnode['ident']+ "\")\n"
poriscinitstr += " @md"+nodename+ "Mode_UNKNOWN.setDescription(\""+desctomonit(thisnode['description'])+"\")\n"
poriscinitstr += " @sys"+nodename+".addMode(@md"+nodename+ "Mode_UNKNOWN)\n"
else:
poriscinitstr += " @sys"+nodename+".addMode(@md"+nodename+ "UNKNOWN)\n"
'''
methodsstr += "\n ## "+nodename+"Mode \n"
methodsstr += " def get_"+nodename+"Mode\n"
#@sysARCGenIII.getSelectedMode
methodsstr += " @sys"+nodename+".getSelectedMode\n\tend\n\n"
methodsstr += " def set_"+nodename+"Mode(mode)\n"
methodsstr += " @sys"+nodename+".selectMode(mode)\n\tend\n"
modeliststr = ""
modeshortliststr = ""
if thisclass == "prParam":
valuesstr = ""
valuesshortstr = ""
valuemaxstr = valuesstr
'''
##### Removing UNKNOWN
valuesstr += nodename+"_UNKNOWN"
valuesshortstr += "UNKNOWN"
valuemaxstr += valuesstr
'''
'''
##### Removing UNKNOWN
if not savemem:
modeliststr += nodename+"Mode_UNKNOWN"
else:
modeliststr += nodename+"UNKNOWN"
modeshortliststr += "UNKNOWN"
'''
switchfm2 = False
if 'parentnode' in thisnode.keys():
parentnode = thisnode['parentnode']
if parentnode is not None:
parentnodename = nametoidl(parentnode['subject'])
parentnodeclass = parentnode['tracker']
parentnodevar = "enum"+parentnodename
parentmodevar = "enum"+parentnodename+"Mode"
parentmodevar_ = parentmodevar+"_"
parentnodevar_ = parentnodevar+"_"
parentnodemodevar = parentnode
for k in parentnode['children']:
childnode = nodes_dict[k]
childname = nametoidl(childnode['subject'])
childclass = childnode['tracker']
if childclass == "prMode":
anyvaluepresentparentinner = False
firstfound = False
for kv in childnode['blocking']:
kvnode = nodes_dict[kv]
kvname = nametoidl(kvnode['subject'])
kvclass = kvnode['tracker']
if kvclass == "prMode":
if kvnode['parentnode'] == thisnode:
if not savemem:
poriscinitrelstr += " # Marcamos "+nodename+"Mode_"+kvname+" como elegible para "+parentnodename+"Mode_"+childname+"\n"
poriscinitrelstr += " @md"+parentnodename+"Mode_"+childname+".addSubMode(@md"+nodename+"Mode_"+kvname+")\n"
else:
poriscinitrelstr += " @md"+parentnodename+childname+".addSubMode(@md"+nodename+kvname+")\n"
anyvaluepresentparentinner = True
if not firstfound:
firstfound = True
if (anyvaluepresentparentinner):
anyvaluepresentparentouter = True
else:
parentnode = None
monitsetparam = None
methodsstrfl = ""
minimum_float = None
maximum_float = None
if thisclass == "prParam":
for k in thisnode['children']:
childnode = nodes_dict[k]
childname = nametoidl(childnode['subject'])
childclass = childnode['tracker']
if childclass == "prMode":
if not savemem:
modeliststr += ","+nodename+"Mode_"+childname
modeshortliststr += ","+childname
else:
modeliststr += ","+nodename+childname
modeshortliststr += ","+childname
anyvaluepresentinner = False
firstdone = False
for kv in childnode['blocking']:
kvnode = nodes_dict[kv]
kvname = nametoidl(kvnode['subject'])
kvclass = kvnode['tracker']
if kvclass == "prValFloat" or kvclass == "prValText" or kvclass == "prValue":
if kvnode['virtual'] == False:
if not savemem:
poriscinitrelstr += " # Marcamos "+nodename+"_"+kvname+" como elegible para "+nodename+"Mode_"+childname+"\n"
poriscinitrelstr += " @md"+nodename+"Mode_"+childname+".addValue(@vl"+nodename+"_"+kvname+")\n"
else:
poriscinitrelstr += " @md"+nodename+childname+".addValue(@vl"+nodename+"_"+kvname+")\n"
anyvaluepresentinner = True
if not firstdone:
firstdone = True
if kvclass == "prValFloat":
if maximum_float is None:
maximum_float = kvnode['max']
else:
maximum_float = max(maximum_float,kvnode['max'])
if minimum_float is None:
minimum_float = kvnode['min']
else:
minimum_float = min(minimum_float,kvnode['min'])
methodsstrfl += " ## "+thisclass+" "+parentNodeName+" \n"
methodsstrfl += "\n # "+nodename+"Double \n"
methodsstrfl += " def get_"+nodename+"Double\n"
methodsstrfl += " v = @pr"+nodename+".getSelectedValue\n"
methodsstrfl += " v.class = PORISValueFloat\n"
methodsstrfl += " v.getData\n\tend\n\n"
methodsstrfl += " def set_"+nodename+"Double(data)\n"
methodsstrfl += " @pr"+nodename+".getSelectedValue.setData(data)\n\tend\n"
if kvclass == "prValText":
methodsstrfl += " ## "+thisclass+" "+parentNodeName+" \n"
methodsstrfl += "\n # "+nodename+"String #\n"
methodsstrfl += " def get_"+nodename+"String-> str :\n"
methodsstrfl += " v = @pr"+nodename+".getSelectedValue\n"
methodsstrfl += " v.class = PORISValueString\n"
methodsstrfl += " v.getData\n\tend\n\n"
methodsstrfl += " def set_"+nodename+"String(data)\n"
methodsstrfl += " @pr"+nodename+".getSelectedValue.setData(data)\n\tend\n"
else:
if childclass == "prValFloat" or childclass == "prValText" or childclass == "prValue":
valuesstr += ","+nodename+"_"+childname
valuesshortstr += ","+childname
valuemaxstr = nodename+"_"+childname
methodsstr += methodsstrfl
else:
if thisclass == "prSys":
for k in thisnode['children']:
childnode = nodes_dict[k]
childname = nametoidl(childnode['subject'])
childclass = childnode['tracker']
if childclass == "prMode":
if not savemem:
modeliststr += ","+nodename+"Mode_"+childname
modeshortliststr += ","+childname
else:
modeliststr += ","+nodename+childname
modeshortliststr += ","+childname
else:
parentNode = thisnode['parentnode']
parentNodeName = nametoidl(parentNode['subject'])
if thisclass == "prMode":
#PORISMode prDetectorMode_Image;
if not savemem:
porishstr += " @md"+parentNodeName+ "Mode_" + nodename+" = PORISMode.new(\""+parentNodeName+ "Mode_" + nodename + "\")\n"
else:
porishstr += " @md"+parentNodeName + nodename+" = PORISMode.new(\"" + nodename + "\")\n"
'''
prDetectorMode_UNKNOWN.id = idcounter++;
prDetectorMode_UNKNOWN.idx = MyDASDevice::DetectorMode_UNKNOWN;
prDetectorMode_UNKNOWN.ident = "DetectorMode_UNKNOWN";
prDetectorMode_UNKNOWN.name = "DetectorMode_UNKNOWN";
prDetectorMode_UNKNOWN.description = "DetectorMode_UNKNOWN";
prDetectorMode_UNKNOWN.parent = &prDetector;
'''
if not savemem:
poriscinitstr += " self.addItem(@md"+parentNodeName+ "Mode_"+nodename+")\n"
else:
poriscinitstr += " self.addItem(@md" + parentNodeName + "_" + nodename+")\n"
if not savemem:
poriscinitstr += " @md"+parentNodeName+ "Mode_" + nodename+".setIdent(\""+thisnode['ident']+"\")\n"
poriscinitstr += " @md"+parentNodeName+ "Mode_" + nodename+".setDescription(\""+desctomonit(thisnode['description'])+"\")\n"
if parentNode['tracker'] == "prParam":
if not savemem:
poriscinitstr += " @pr"+parentNodeName+".addMode(@md"+parentNodeName+ "Mode_" + nodename+")\n"
else:
poriscinitstr += " @pr"+parentNodeName+".addMode(@md"+parentNodeName+ nodename+")\n"
else:
if not savemem:
poriscinitstr += " @sys"+parentNodeName+".addMode(@md"+parentNodeName+ "Mode_" + nodename+")\n"
else:
poriscinitstr += " @sys"+parentNodeName+".addMode(@md"+parentNodeName + nodename+")\n"
else:
if thisclass == "prValue":
if thisnode['virtual'] == False:
#PORISValue prBinning_1x1;
if not savemem:
porishstr += " @vl"+parentNodeName+"_" + nodename+" = PORISValue.new(\""+parentNodeName+"_" + nodename+"\")\n"
else:
porishstr += " @vl"+parentNodeName+"_" + nodename+" = PORISValue.new(\"" + nodename+"\")\n"
'''
prBinning_1x1.id = idcounter++;
prBinning_1x1.idx = MyDASDevice::Binning_1x1;
prBinning_1x1.ident = "Binning_1x1";
prBinning_1x1.name = "Binning_1x1";
prBinning_1x1.description = "Sin binning";
prBinning_1x1.parent = &prBinning;
'''
poriscinitstr += " self.addItem(@vl" + parentNodeName+ "_" + nodename+")\n"
if not savemem:
poriscinitstr += " @vl"+parentNodeName+ "_" + nodename+".setIdent(\""+thisnode['ident']+"\")\n"
poriscinitstr += " @vl"+parentNodeName+ "_" + nodename+".setDescription(\""+desctomonit(thisnode['description'])+"\")\n"
poriscinitstr += " @pr"+parentNodeName+".addValue(@vl"+parentNodeName+ "_" + nodename+")\n"
else:
if thisclass == "prValFloat":
#PORISValueFloat prExpTime_Normal;
if not savemem:
porishstr += " @vl"+parentNodeName+"_" + nodename+" = PORISValueFloat.new(\""+parentNodeName+"_" + nodename + "\","+str(thisnode['min'])+","+str(thisnode['default_data'])+","+str(thisnode['max'])+")\n"
else:
porishstr += " @vl"+parentNodeName+"_" + nodename+" = PORISValueFloat.new(\"" + nodename + "\","+str(thisnode['min'])+","+str(thisnode['default_data'])+","+str(thisnode['max'])+")\n"
'''
prExpTime_Normal.id = idcounter++;
prExpTime_Normal.idx = MyDASDevice::ExpTime_Normal;
prExpTime_Normal.ident = "ExpTime_Normal";
prExpTime_Normal.name = "ExpTime_Normal";
prExpTime_Normal.description = "Rango normal de valores para ExpTime";
prExpTime_Normal.parent = &prExpTime;
'''
poriscinitstr += " self.addItem(@vl" + parentNodeName + "_" + nodename+")\n"
if not savemem:
poriscinitstr += " @vl"+parentNodeName+ "_" + nodename+".setIdent(\""+thisnode['ident']+"\")\n"
poriscinitstr += " @vl"+parentNodeName+ "_" + nodename+".setDescription(\""+desctomonit(thisnode['description'])+"\")\n"
#poriscinitstr += " @vl"+parentNodeName+ "_" + nodename+".min = "+str(thisnode['min'])+"\n"
#poriscinitstr += " @vl"+parentNodeName+ "_" + nodename+".default_data = "+str(thisnode['default_data'])+"\n"
#poriscinitstr += " @vl"+parentNodeName+ "_" + nodename+".max = "+str(thisnode['max'])+"\n"
poriscinitstr += " @pr"+parentNodeName+".addValue(@vl"+parentNodeName+ "_" + nodename+")\n"
else:
if thisclass == "prValText":
if not savemem:
porishstr += " @vl"+parentNodeName+"_" + nodename+" = PORISValueString.new(\""+parentNodeName+"_" + nodename+"\",'"+str(thisnode['deftext'])+"')\n"
else:
porishstr += " @vl"+parentNodeName+"_" + nodename+" = PORISValueString.new(\""+nodename+"\",'"+str(thisnode['deftext'])+"')\n"
'''
prShiftList_Normal.id = idcounter++;
prShiftList_Normal.idx = MyDASDevice::ShiftList_Normal;
prShiftList_Normal.ident = "ShiftList_Normal";
prShiftList_Normal.name = "ShiftList_Normal";
prShiftList_Normal.description = "Lista _separada por comas_ con los desplazamientos";
prShiftList_Normal.parent = &prShiftList;
'''
poriscinitstr += " self.addItem(@vl" + parentNodeName + "_" + nodename+")\n"
if not savemem:
poriscinitstr += " @vl"+parentNodeName+ "_" + nodename+".setIdent(\""+thisnode['ident']+"\")\n"
poriscinitstr += " @vl"+parentNodeName+ "_" + nodename+".setDescription(\""+desctomonit(thisnode['description'])+"\")\n"
poriscinitstr += " @pr"+parentNodeName+".addValue(@vl"+parentNodeName+ "_" + nodename+")\n"
else:
if thisclass == "prCmd":
thisnode = {}
thiskey = parentNodeName+ "_" + nodename
thisnode['method'] = "exec"+thiskey
thisnode['node'] = parentNodeName
methods_dict[thiskey] = thisnode
methodsstr += '\n ## Action trigger '+thiskey+' ##\n'
methodsstr += ' def '+thisnode['method']+'(value)\n'
methodsstr += ' # Override this\n'
methodsstr += ' true\n\tend\n'
else:
porishstr += " //TODO: Other xx"+parentNodeName+ "_" + nodename+"\n"
poriscinitstr += poriscinitrelstr + "end\n\n"
poriscstr += poriscinitstr
with open("./"+output_path+relative_path+"/"+deviceName+"PORIS.rb", "w+") as text_file:
text_file.write(porishstr)
text_file.write(poriscstr)
text_file.write(methodsstr+"end\n")
def replaceStringFile(pattern_file,dest_file,pattern_text, replacement_text):
# Read in the file
with open(pattern_file, 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace(pattern_text, replacement_text)
# Write the file out again
with open(dest_file, 'w') as file:
file.write(filedata)
def fileGenFromPattern(pattern_file_str,src_file_str, dest_file_str,marker_str,particles_dir,replacement_dir):
# Loading the capsule declaration pattern
pattern_lines = []
with open(pattern_file_str, 'r') as pattern_file:
pattern_lines = pattern_file.readlines()
# Computing the capsule declaration lines
lines_to_include = []
particle_lines = {}
for k in particles_dir.keys():
for v in particles_dir[k]:
particle_lines[v] = []
for l in pattern_lines:
# We replace the constant part
line_base = l
for k2 in replacement_dir.keys():
line_base = line_base.replace(k2,replacement_dir[k2])
# We iterate creating lines for each particle
for k in particles_dir.keys():
for v in particles_dir[k]:
line_to_include = line_base.replace(k,v)
particle_lines[v] += [line_to_include]
for k in particle_lines.keys():
lines_to_include += particle_lines[k]
lines_to_include += ["\n"]
filesrc = open(src_file_str, 'r')
filedest = open(dest_file_str, 'w')
for line in filesrc:
filedest.write(line)
if marker_str in line:
filedest.writelines(lines_to_include)
filesrc.close()
filedest.close()
# Loads the ODS file into a nodes dictionary
nodes_dict = loadODS()
# Creates the code in the src_c++ folder
createPythonCode(nodes_dict,deviceName,args.output_path, relativepath)