summaryrefslogtreecommitdiffstats
path: root/control/gen-fan-zone-defs.py
blob: 7db3d959cfa8130a177226ac311c5a28d772d811 (plain)
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
#!/usr/bin/env python

"""
This script reads in fan definition and zone definition YAML
files and generates a set of structures for use by the fan control code.
"""

import os
import sys
import yaml
from argparse import ArgumentParser
from mako.template import Template
from mako.lookup import TemplateLookup


def parse_cpp_type(typeName):
    """
    Take a list of dbus types from YAML and convert it to a recursive cpp
    formed data structure. Each entry of the original list gets converted
    into a tuple consisting of the type name and a list with the params
    for this type,
        e.g.
            ['dict', ['string', 'dict', ['string', 'int64']]]
        is converted to
            [('dict', [('string', []), ('dict', [('string', []),
             ('int64', [])]]]
    """

    if not typeName:
        return None

    # Type names are _almost_ valid YAML. Insert a , before each [
    # and then wrap it in a [ ] and it becomes valid YAML (assuming
    # the user gave us a valid typename).
    typeArray = yaml.safe_load("[" + ",[".join(typeName.split("[")) + "]")
    typeTuple = preprocess_yaml_type_array(typeArray).pop(0)
    return get_cpp_type(typeTuple)


def preprocess_yaml_type_array(typeArray):
    """
    Flattens an array type into a tuple list that can be used to get the
    supported cpp type from each element.
    """

    result = []

    for i in range(len(typeArray)):
        # Ignore lists because we merge them with the previous element
        if type(typeArray[i]) is list:
            continue

        # If there is a next element and it is a list, merge it with the
        # current element.
        if i < len(typeArray)-1 and type(typeArray[i+1]) is list:
            result.append(
                (typeArray[i],
                 preprocess_yaml_type_array(typeArray[i+1])))
        else:
            result.append((typeArray[i], []))

    return result


def get_cpp_type(typeTuple):
    """
    Take a list of dbus types and perform validity checking, such as:
        [ variant [ dict [ int32, int32 ], double ] ]
    This function then converts the type-list into a C++ type string.
    """

    propertyMap = {
        'byte': {'cppName': 'uint8_t', 'params': 0},
        'boolean': {'cppName': 'bool', 'params': 0},
        'int16': {'cppName': 'int16_t', 'params': 0},
        'uint16': {'cppName': 'uint16_t', 'params': 0},
        'int32': {'cppName': 'int32_t', 'params': 0},
        'uint32': {'cppName': 'uint32_t', 'params': 0},
        'int64': {'cppName': 'int64_t', 'params': 0},
        'uint64': {'cppName': 'uint64_t', 'params': 0},
        'double': {'cppName': 'double', 'params': 0},
        'string': {'cppName': 'std::string', 'params': 0},
        'array': {'cppName': 'std::vector', 'params': 1},
        'dict': {'cppName': 'std::map', 'params': 2}}

    if len(typeTuple) != 2:
        raise RuntimeError("Invalid typeTuple %s" % typeTuple)

    first = typeTuple[0]
    entry = propertyMap[first]

    result = entry['cppName']

    # Handle 0-entry parameter lists.
    if (entry['params'] == 0):
        if (len(typeTuple[1]) != 0):
            raise RuntimeError("Invalid typeTuple %s" % typeTuple)
        else:
            return result

    # Get the parameter list
    rest = typeTuple[1]

    # Confirm parameter count matches.
    if (entry['params'] != -1) and (entry['params'] != len(rest)):
        raise RuntimeError("Invalid entry count for %s : %s" %
                           (first, rest))

    # Parse each parameter entry, if appropriate, and create C++ template
    # syntax.
    result += '<'
    if entry.get('noparse'):
        # Do not parse the parameter list, just use the first element
        # of each tuple and ignore possible parameters
        result += ", ".join([e[0] for e in rest])
    else:
        result += ", ".join(map(lambda e: get_cpp_type(e),
                                rest))
    result += '>'

    return result


def convertToMap(listOfDict):
    """
    Converts a list of dictionary entries to a std::map initialization list.
    """
    listOfDict = listOfDict.replace('\'', '\"')
    listOfDict = listOfDict.replace('[', '{')
    listOfDict = listOfDict.replace(']', '}')
    listOfDict = listOfDict.replace(':', ',')
    return listOfDict


def genEvent(event):
    """
    Generates the source code of an event and returns it as a string
    """
    e = "SetSpeedEvent{\n"

    e += "Group{\n"
    for group in event['groups']:
        for member in group['members']:
            e += "{\n"
            e += "\"" + member['object'] + "\",\n"
            e += "{\"" + member['interface'] + "\",\n"
            e += " \"" + member['property'] + "\"}\n"
        e += "},\n"
    e += "},\n"

    e += "std::vector<Action>{\n"
    for a in event['action']:
        if len(a['parameters']) != 0:
            e += "make_action(action::" + a['name'] + "(\n"
        else:
            e += "make_action(action::" + a['name'] + "\n"
        for i, p in enumerate(a['parameters']):
            if (i+1) != len(a['parameters']):
                e += p + ",\n"
            else:
                e += p + ")\n"
        e += "),\n"
    e += "},\n"

    e += "TimerConf{\n"
    e += "\t" + event['timer']['interval'] + ",\n"
    e += "\t" + event['timer']['type'] + "\n"
    e += "},\n"

    e += "std::vector<Signal>{\n"
    for s in event['signals']:
        e += "Signal{\n"
        e += "match::" + s['match'] + "(\n"
        for i, mp in enumerate(s['mparams']):
            if (i+1) != len(s['mparams']):
                e += "\"" + mp + "\",\n"
            else:
                e += "\"" + mp + "\"\n"
        e += "),\n"
        e += "make_handler(\n"
        if ('type' in s['sparams']) and (s['sparams']['type'] is not None):
            e += s['signal'] + "<" + s['sparams']['type'] + ">(\n"
        else:
            e += s['signal'] + "(\n"
        for sp in s['sparams']['params']:
            e += s['sparams'][sp] + ",\n"
        if ('type' in s['hparams']) and (s['hparams']['type'] is not None):
            e += ("handler::" + s['handler'] +
                  "<" + s['hparams']['type'] + ">(\n")
        else:
            e += "handler::" + s['handler'] + "(\n)"
        for i, hp in enumerate(s['hparams']['params']):
            if (i+1) != len(s['hparams']['params']):
                e += s['hparams'][hp] + ",\n"
            else:
                e += s['hparams'][hp] + "\n"
        e += "))\n"
        e += ")\n"
    e += "},\n"
    e += "}\n"

    e += "}"

    return e


def getGroups(zNum, zCond, edata, events):
    """
    Extract and construct the groups for the given event.
    """
    groups = []
    for eGroups in edata['groups']:
        if ('zone_conditions' in eGroups) and \
           (eGroups['zone_conditions'] is not None):
            # Zone conditions are optional in the events yaml but skip
            # if this event's condition is not in this zone's conditions
            if all('name' in z and z['name'] is not None and
                   not any(c['name'] == z['name'] for c in zCond)
                   for z in eGroups['zone_conditions']):
                continue

            # Zone numbers are optional in the events yaml but skip if this
            # zone's zone number is not in the event's zone numbers
            if all('zones' in z and z['zones'] is not None and
                   zNum not in z['zones']
                   for z in eGroups['zone_conditions']):
                continue
        eGroup = next(g for g in events['groups']
                      if g['name'] == eGroups['name'])

        group = {}
        members = []
        group['name'] = eGroup['name']
        for m in eGroup['members']:
            member = {}
            member['path'] = eGroup['type']
            member['object'] = (eGroup['type'] + m)
            member['interface'] = eGroups['interface']
            member['property'] = eGroups['property']['name']
            member['type'] = eGroups['property']['type']
            # Use defined service to note member on zone object
            if ('service' in eGroup) and \
               (eGroup['service'] is not None):
                member['service'] = eGroup['service']
            # Add expected group member's property value if given
            if ('value' in eGroups['property']) and \
               (eGroups['property']['value'] is not None):
                    if isinstance(eGroups['property']['value'], str) or \
                            "string" in str(member['type']).lower():
                        member['value'] = (
                            "\"" + eGroups['property']['value'] + "\"")
                    else:
                        member['value'] = eGroups['property']['value']
            members.append(member)
        group['members'] = members
        groups.append(group)
    return groups


def getActions(zNum, zCond, edata, actions, events):
    """
    Extracts and constructs the make_action function call for
    all the actions within the given event.
    """
    action = []
    for eActions in actions['actions']:
        actions = {}
        eAction = next(a for a in events['actions']
                       if a['name'] == eActions['name'])
        actions['name'] = eAction['name']
        params = []
        if ('parameters' in eAction) and \
           (eAction['parameters'] is not None):
            for p in eAction['parameters']:
                param = "static_cast<"
                if type(eActions[p]) is not dict:
                    if p == 'actions':
                        param = "std::vector<Action>{"
                        pActs = getActions(zNum,
                                           zCond,
                                           edata,
                                           eActions,
                                           events)
                        for a in pActs:
                            if (len(a['parameters']) != 0):
                                param += (
                                    "make_action(action::" +
                                    a['name'] +
                                    "(\n")
                                for i, ap in enumerate(a['parameters']):
                                    if (i+1) != len(a['parameters']):
                                        param += (ap + ",")
                                    else:
                                        param += (ap + ")")
                            else:
                                param += ("make_action(action::" + a['name'])
                            param += "),"
                        param += "}"
                    elif p == 'defevents' or p == 'altevents':
                        param = "std::vector<SetSpeedEvent>{\n"
                        for i, e in enumerate(eActions[p]):
                            aEvent = getEvent(zNum, zCond, e, events)
                            if not aEvent:
                                continue
                            if (i+1) != len(eActions[p]):
                                param += genEvent(aEvent) + ",\n"
                            else:
                                param += genEvent(aEvent) + "\n"
                        param += "\t}"
                    elif p == 'property':
                        if isinstance(eActions[p], str) or \
                           "string" in str(eActions[p]['type']).lower():
                            param += (
                                str(eActions[p]['type']).lower() +
                                ">(\"" + str(eActions[p]) + "\")")
                        else:
                            param += (
                                str(eActions[p]['type']).lower() +
                                ">(" + str(eActions[p]['value']).lower() + ")")
                    else:
                        # Default type to 'size_t' when not given
                        param += ("size_t>(" + str(eActions[p]).lower() + ")")
                else:
                    if p == 'timer':
                        param = (
                            "TimerConf{static_cast<std::chrono::seconds>(" +
                            str(eActions[p]['delay']) + "), " +
                            "TimerType::" +
                            str(eActions[p]['type']) + "}")
                    else:
                        param += (str(eActions[p]['type']).lower() + ">(")
                        if p != 'map':
                            if isinstance(eActions[p]['value'], str) or \
                               "string" in str(eActions[p]['type']).lower():
                                param += \
                                    "\"" + str(eActions[p]['value']) + "\")"
                            else:
                                param += \
                                    str(eActions[p]['value']).lower() + ")"
                        else:
                            param += (
                                str(eActions[p]['type']).lower() +
                                convertToMap(str(eActions[p]['value'])) + ")")
                params.append(param)
        actions['parameters'] = params
        action.append(actions)
    return action


def getEvent(zone_num, zone_conditions, e, events_data):
    """
    Parses the sections of an event and populates the properties
    that construct an event within the generated source.
    """
    event = {}

    # Add set speed event groups
    grps = getGroups(zone_num, zone_conditions, e, events_data)
    if not grps:
        return
    event['groups'] = grps

    # Add optional set speed actions and function parameters
    event['action'] = []
    if ('actions' in e) and \
       (e['actions'] is not None):
        event['action'] = getActions(zone_num,
                                     zone_conditions,
                                     e,
                                     e,
                                     events_data)

    # Add signal handlers
    signals = []
    for group in event['groups']:
        for member in group['members']:
            for eMatches in e['matches']:
                signal = {}
                eMatch = next(m for m in events_data['matches']
                              if m['name'] == eMatches['name'])
                # If service not given, subscribe to signal match
                if ('service' not in member):
                    signal['match'] = eMatch['name']
                    params = []
                    if ('parameters' in eMatch) and \
                       (eMatch['parameters'] is not None):
                        for p in eMatch['parameters']:
                            params.append(member[str(p)])
                    signal['mparams'] = params

                if ('parameters' in eMatch['signal']) and \
                   (eMatch['signal']['parameters'] is not None):
                    eSignal = eMatch['signal']
                else:
                    eSignal = next(s for s in events_data['signals']
                                   if s['name'] == eMatch['signal'])
                signal['signal'] = eSignal['name']
                sparams = {}
                if ('parameters' in eSignal) and \
                   (eSignal['parameters'] is not None):
                    splist = []
                    for p in eSignal['parameters']:
                        sp = str(p)
                        if (sp != 'type'):
                            splist.append(sp)
                            if (sp != 'group'):
                                sparams[sp] = "\"" + member[sp] + "\""
                            else:
                                sparams[sp] = "Group{\n"
                                for m in group['members']:
                                    sparams[sp] += (
                                        "{\n" +
                                        "\"" + str(m['object']) + "\",\n" +
                                        "{\"" + str(m['interface']) + "\"," +
                                        "\"" + str(m['property']) + "\"}\n" +
                                        "},\n")
                                sparams[sp] += "}"
                        else:
                            sparams[sp] = member[sp]
                    sparams['params'] = splist
                signal['sparams'] = sparams
                # Add signal handler
                eHandler = next(h for h in events_data['handlers']
                                if h['name'] == eSignal['handler'])
                signal['handler'] = eHandler['name']
                hparams = {}
                if ('parameters' in eHandler) and \
                   (eHandler['parameters'] is not None):
                    hplist = []
                    for p in eHandler['parameters']:
                        hp = str(p)
                        if (hp != 'type'):
                            hplist.append(hp)
                            if (hp != 'group'):
                                hparams[hp] = "\"" + member[hp] + "\""
                            else:
                                hparams[hp] = "Group{\n"
                                for m in group['members']:
                                    hparams[hp] += (
                                        "{\n" +
                                        "\"" + str(m['object']) + "\",\n" +
                                        "{\"" + str(m['interface']) + "\"," +
                                        "\"" + str(m['property']) + "\"}\n" +
                                        "},\n")
                                hparams[hp] += "}"
                        else:
                            hparams[hp] = member[hp]
                    hparams['params'] = hplist
                signal['hparams'] = hparams
                signals.append(signal)
    event['signals'] = signals

    # Add optional action call timer
    timer = {}
    interval = "static_cast<std::chrono::seconds>"
    if ('timer' in e) and \
       (e['timer'] is not None):
        timer['interval'] = (interval +
                             "(" +
                             str(e['timer']['interval']) +
                             ")")
    else:
        timer['interval'] = (interval +
                             "(" + str(0) + ")")
    timer['type'] = "TimerType::repeating"
    event['timer'] = timer

    return event


def addPrecondition(zNum, zCond, event, events_data):
    """
    Parses the precondition section of an event and populates the necessary
    structures to generate a precondition for a set speed event.
    """
    precond = {}
    # Add set speed event precondition group
    grps = getGroups(zNum, zCond, event['precondition'], events_data)
    if not grps:
        return
    precond['pcgrps'] = grps

    # Add set speed event precondition actions
    pc = []
    pcs = {}
    pcs['name'] = event['precondition']['name']
    epc = next(p for p in events_data['preconditions']
               if p['name'] == event['precondition']['name'])
    params = []
    for p in epc['parameters']:
        param = {}
        if p == 'groups':
            param['type'] = "std::vector<PrecondGroup>"
            param['open'] = "{"
            param['close'] = "}"
            values = []
            for group in precond['pcgrps']:
                for pcgrp in group['members']:
                    value = {}
                    value['value'] = (
                        "PrecondGroup{\"" +
                        str(pcgrp['object']) + "\",\"" +
                        str(pcgrp['interface']) + "\",\"" +
                        str(pcgrp['property']) + "\"," +
                        "static_cast<" +
                        str(pcgrp['type']).lower() + ">")
                    if isinstance(pcgrp['value'], str) or \
                       "string" in str(pcgrp['type']).lower():
                        value['value'] += ("(" + str(pcgrp['value']) + ")}")
                    else:
                        value['value'] += \
                            ("(" + str(pcgrp['value']).lower() + ")}")
                    values.append(value)
            param['values'] = values
        params.append(param)
    pcs['params'] = params
    pc.append(pcs)
    precond['pcact'] = pc

    pcevents = []
    for pce in event['precondition']['events']:
        pcevent = getEvent(zNum, zCond, pce, events_data)
        if not pcevent:
            continue
        pcevents.append(pcevent)
    precond['pcevts'] = pcevents

    # Add precondition signal handlers
    signals = []
    for group in precond['pcgrps']:
        for member in group['members']:
            for eMatches in event['precondition']['matches']:
                signal = {}
                eMatch = next(m for m in events_data['matches']
                              if m['name'] == eMatches['name'])
                # If service not given, subscribe to signal match
                if ('service' not in member):
                    signal['match'] = eMatch['name']
                    params = []
                    if ('parameters' in eMatch) and \
                       (eMatch['parameters'] is not None):
                        for p in eMatch['parameters']:
                            params.append(member[str(p)])
                    signal['mparams'] = params

                if ('parameters' in eMatch['signal']) and \
                   (eMatch['signal']['parameters'] is not None):
                    eSignal = eMatch['signal']
                else:
                    eSignal = next(s for s in events_data['signals']
                                   if s['name'] == eMatch['signal'])
                signal['signal'] = eSignal['name']
                sparams = {}
                if ('parameters' in eSignal) and \
                   (eSignal['parameters'] is not None):
                    splist = []
                    for p in eSignal['parameters']:
                        sp = str(p)
                        if (sp != 'type'):
                            splist.append(sp)
                            if (sp != 'group'):
                                sparams[sp] = "\"" + member[sp] + "\""
                            else:
                                sparams[sp] = "Group{\n"
                                for m in group:
                                    sparams[sp] += (
                                        "{\n" +
                                        "\"" + str(m['object']) + "\",\n" +
                                        "{\"" + str(m['interface']) + "\"," +
                                        "\"" + str(m['property']) + "\"}\n" +
                                        "},\n")
                                sparams[sp] += "}"
                        else:
                            sparams[sp] = member[sp]
                    sparams['params'] = splist
                signal['sparams'] = sparams
                # Add signal handler
                eHandler = next(h for h in events_data['handlers']
                                if h['name'] == eSignal['handler'])
                signal['handler'] = eHandler['name']
                hparams = {}
                if ('parameters' in eHandler) and \
                   (eHandler['parameters'] is not None):
                    hplist = []
                    for p in eHandler['parameters']:
                        hp = str(p)
                        if (hp != 'type'):
                            hplist.append(hp)
                            if (hp != 'group'):
                                hparams[hp] = "\"" + member[hp] + "\""
                            else:
                                hparams[hp] = "Group{\n"
                                for m in group:
                                    hparams[hp] += (
                                        "{\n" +
                                        "\"" + str(m['object']) + "\",\n" +
                                        "{\"" + str(m['interface']) + "\"," +
                                        "\"" + str(m['property']) + "\"}\n" +
                                        "},\n")
                                hparams[hp] += "}"
                        else:
                            hparams[hp] = member[hp]
                    hparams['params'] = hplist
                signal['hparams'] = hparams
                signals.append(signal)
    precond['pcsigs'] = signals

    # Add optional action call timer
    timer = {}
    interval = "static_cast<std::chrono::seconds>"
    if ('timer' in event['precondition']) and \
       (event['precondition']['timer'] is not None):
        timer['interval'] = (interval +
                             "(" +
                             str(event['precondition']['timer']['interval']) +
                             ")")
    else:
        timer['interval'] = (interval +
                             "(" + str(0) + ")")
    timer['type'] = "TimerType::repeating"
    precond['pctime'] = timer

    return precond


def getEventsInZone(zone_num, zone_conditions, events_data):
    """
    Constructs the event entries defined for each zone using the events yaml
    provided.
    """
    events = []

    if 'events' in events_data:
        for e in events_data['events']:
            event = {}
            # Add precondition if given
            if ('precondition' in e) and \
               (e['precondition'] is not None):
                event['pc'] = addPrecondition(zone_num,
                                              zone_conditions,
                                              e,
                                              events_data)
            else:
                event = getEvent(zone_num, zone_conditions, e, events_data)
                if not event:
                    continue
            events.append(event)

    return events


def getFansInZone(zone_num, profiles, fan_data):
    """
    Parses the fan definition YAML files to find the fans
    that match both the zone passed in and one of the
    cooling profiles.
    """

    fans = []

    for f in fan_data['fans']:

        if zone_num != f['cooling_zone']:
            continue

        # 'cooling_profile' is optional (use 'all' instead)
        if f.get('cooling_profile') is None:
            profile = "all"
        else:
            profile = f['cooling_profile']

        if profile not in profiles:
            continue

        fan = {}
        fan['name'] = f['inventory']
        fan['sensors'] = f['sensors']
        fan['target_interface'] = f.get(
            'target_interface',
            'xyz.openbmc_project.Control.FanSpeed')
        fans.append(fan)

    return fans


def getIfacesInZone(zone_ifaces):
    """
    Parse given interfaces for a zone for associating a zone with an interface
    and set any properties listed to defined values upon fan control starting
    on the zone.
    """

    ifaces = []
    for i in zone_ifaces:
        iface = {}
        # Interface name not needed yet for fan zones but
        # may be necessary as more interfaces are extended by the zones
        iface['name'] = i['name']

        if ('properties' in i) and \
                (i['properties'] is not None):
            props = []
            for p in i['properties']:
                prop = {}
                prop['name'] = str(p['name']).lower()
                prop['type'] = parse_cpp_type(p['type'])
                vals = []
                for v in p['values']:
                    val = v['value']
                    if (val is not None):
                        if (isinstance(val, bool)):
                            # Convert True/False to 'true'/'false'
                            val = 'true' if val else 'false'
                        elif (isinstance(val, str)):
                            # Wrap strings with double-quotes
                            val = "\"" + val + "\""
                        vals.append(val)
                prop['values'] = vals
                props.append(prop)
            iface['props'] = props
        ifaces.append(iface)

    return ifaces


def getConditionInZoneConditions(zone_condition, zone_conditions_data):
    """
    Parses the zone conditions definition YAML files to find the condition
    that match both the zone condition passed in.
    """

    condition = {}

    for c in zone_conditions_data['conditions']:

        if zone_condition != c['name']:
            continue
        condition['type'] = c['type']
        properties = []
        for p in c['properties']:
            property = {}
            property['property'] = p['property']
            property['interface'] = p['interface']
            property['path'] = p['path']
            property['type'] = p['type'].lower()
            property['value'] = str(p['value']).lower()
            properties.append(property)
        condition['properties'] = properties

        return condition


def buildZoneData(zone_data, fan_data, events_data, zone_conditions_data):
    """
    Combines the zone definition YAML and fan
    definition YAML to create a data structure defining
    the fan cooling zones.
    """

    zone_groups = []

    for group in zone_data:
        conditions = []
        # zone conditions are optional
        if 'zone_conditions' in group and group['zone_conditions'] is not None:
            for c in group['zone_conditions']:

                if not zone_conditions_data:
                    sys.exit("No zone_conditions YAML file but " +
                             "zone_conditions used in zone YAML")

                condition = getConditionInZoneConditions(c['name'],
                                                         zone_conditions_data)

                if not condition:
                    sys.exit("Missing zone condition " + c['name'])

                conditions.append(condition)

        zone_group = {}
        zone_group['conditions'] = conditions

        zones = []
        for z in group['zones']:
            zone = {}

            # 'zone' is required
            if ('zone' not in z) or (z['zone'] is None):
                sys.exit("Missing fan zone number in " + zone_yaml)

            zone['num'] = z['zone']

            zone['full_speed'] = z['full_speed']

            zone['default_floor'] = z['default_floor']

            # 'increase_delay' is optional (use 0 by default)
            key = 'increase_delay'
            zone[key] = z.setdefault(key, 0)

            # 'decrease_interval' is optional (use 0 by default)
            key = 'decrease_interval'
            zone[key] = z.setdefault(key, 0)

            # 'cooling_profiles' is optional (use 'all' instead)
            if ('cooling_profiles' not in z) or \
                    (z['cooling_profiles'] is None):
                profiles = ["all"]
            else:
                profiles = z['cooling_profiles']

            # 'interfaces' is optional (no default)
            if ('interfaces' in z) and \
                    (z['interfaces'] is not None):
                ifaces = getIfacesInZone(z['interfaces'])

            fans = getFansInZone(z['zone'], profiles, fan_data)
            events = getEventsInZone(z['zone'], group['zone_conditions'],
                                     events_data)

            if len(fans) == 0:
                sys.exit("Didn't find any fans in zone " + str(zone['num']))

            if (ifaces):
                zone['ifaces'] = ifaces
            zone['fans'] = fans
            zone['events'] = events
            zones.append(zone)

        zone_group['zones'] = zones
        zone_groups.append(zone_group)

    return zone_groups


if __name__ == '__main__':
    parser = ArgumentParser(
        description="Phosphor fan zone definition parser")

    parser.add_argument('-z', '--zone_yaml', dest='zone_yaml',
                        default="example/zones.yaml",
                        help='fan zone definitional yaml')
    parser.add_argument('-f', '--fan_yaml', dest='fan_yaml',
                        default="example/fans.yaml",
                        help='fan definitional yaml')
    parser.add_argument('-e', '--events_yaml', dest='events_yaml',
                        help='events to set speeds yaml')
    parser.add_argument('-c', '--zone_conditions_yaml',
                        dest='zone_conditions_yaml',
                        help='conditions to determine zone yaml')
    parser.add_argument('-o', '--output_dir', dest='output_dir',
                        default=".",
                        help='output directory')
    args = parser.parse_args()

    if not args.zone_yaml or not args.fan_yaml:
        parser.print_usage()
        sys.exit(1)

    with open(args.zone_yaml, 'r') as zone_input:
        zone_data = yaml.safe_load(zone_input) or {}

    with open(args.fan_yaml, 'r') as fan_input:
        fan_data = yaml.safe_load(fan_input) or {}

    events_data = {}
    if args.events_yaml:
        with open(args.events_yaml, 'r') as events_input:
            events_data = yaml.safe_load(events_input) or {}

    zone_conditions_data = {}
    if args.zone_conditions_yaml:
        with open(args.zone_conditions_yaml, 'r') as zone_conditions_input:
            zone_conditions_data = yaml.safe_load(zone_conditions_input) or {}

    zone_config = buildZoneData(zone_data.get('zone_configuration', {}),
                                fan_data, events_data, zone_conditions_data)

    manager_config = zone_data.get('manager_configuration', {})

    if manager_config.get('power_on_delay') is None:
        manager_config['power_on_delay'] = 0

    tmpls_dir = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "templates")
    output_file = os.path.join(args.output_dir, "fan_zone_defs.cpp")
    if sys.version_info < (3, 0):
        lkup = TemplateLookup(
            directories=tmpls_dir.split(),
            disable_unicode=True)
    else:
        lkup = TemplateLookup(
            directories=tmpls_dir.split())
    tmpl = lkup.get_template('fan_zone_defs.mako.cpp')
    with open(output_file, 'w') as output:
        output.write(tmpl.render(zones=zone_config,
                                 mgr_data=manager_config))
OpenPOWER on IntegriCloud