recursiveMapper.py 4.9 KB
Newer Older
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
#
# (C) Semantix Information Technologies.
#
# Semantix Information Technologies is licensing the code of the
# Data Modelling Tools (DMT) in the following dual-license mode:
#
# Commercial Developer License:
#       The DMT Commercial Developer License is the suggested version
# to use for the development of proprietary and/or commercial software.
# This version is for developers/companies who do not want to comply
# with the terms of the GNU Lesser General Public License version 2.1.
#
# GNU LGPL v. 2.1:
#       This version of DMT is the one to use for the development of
# applications, when you are willing to comply with the terms of the
# GNU Lesser General Public License version 2.1.
#
# Note that in both cases, there are no charges (royalties) for the
# generated code.
#
import re

from commonPy.utility import panicWithCallStack
from commonPy.asnAST import AsnBasicNode, AsnSequence, AsnSet, AsnChoice, AsnSequenceOf, AsnSetOf, AsnEnumerated, AsnMetaMember


class RecursiveMapper:

    def maybeElse(self, childNo):
        if childNo == 1:
            return ""
        else:
            return "else "

    def CleanName(self, fieldName):
        return re.sub(r'[^a-zA-Z0-9_]', '_', fieldName)

    def Version(self):
        panicWithCallStack("Method undefined in a RecursiveMapper...")

    def MapInteger(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
        panicWithCallStack("Method undefined in a RecursiveMapper...")

    def MapReal(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
        panicWithCallStack("Method undefined in a RecursiveMapper...")

    def MapBoolean(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
        panicWithCallStack("Method undefined in a RecursiveMapper...")

    def MapOctetString(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
        panicWithCallStack("Method undefined in a RecursiveMapper...")

    def MapEnumerated(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
        panicWithCallStack("Method undefined in a RecursiveMapper...")

    def MapSequence(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
        panicWithCallStack("Method undefined in a RecursiveMapper...")

    def MapSet(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
        panicWithCallStack("Method undefined in a RecursiveMapper...")

    def MapChoice(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
        panicWithCallStack("Method undefined in a RecursiveMapper...")

    def MapSequenceOf(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
        panicWithCallStack("Method undefined in a RecursiveMapper...")

    def MapSetOf(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
        panicWithCallStack("Method undefined in a RecursiveMapper...")

    def Map(self, srcVar, destVar, node, leafTypeDict, names):
        if isinstance(node, str):
            node = names[node]
        lines = []
        if isinstance(node, AsnBasicNode):
            realLeafType = leafTypeDict[node._leafType]
            if realLeafType == "INTEGER":
                lines.extend(self.MapInteger(srcVar, destVar, node, leafTypeDict, names))
            elif realLeafType == "REAL":
                lines.extend(self.MapReal(srcVar, destVar, node, leafTypeDict, names))
            elif realLeafType == "BOOLEAN":
                lines.extend(self.MapBoolean(srcVar, destVar, node, leafTypeDict, names))
            elif realLeafType == "OCTET STRING":
                lines.extend(self.MapOctetString(srcVar, destVar, node, leafTypeDict, names))
            else:
                panicWithCallStack("Basic type %s can't be mapped..." % realLeafType)
        elif isinstance(node, AsnSequence):
            lines.extend(self.MapSequence(srcVar, destVar, node, leafTypeDict, names))
        elif isinstance(node, AsnSet):
            lines.extend(self.MapSet(srcVar, destVar, node, leafTypeDict, names))
        elif isinstance(node, AsnChoice):
            lines.extend(self.MapChoice(srcVar, destVar, node, leafTypeDict, names))
        elif isinstance(node, AsnSequenceOf):
            lines.extend(self.MapSequenceOf(srcVar, destVar, node, leafTypeDict, names))
        elif isinstance(node, AsnSetOf):
            lines.extend(self.MapSetOf(srcVar, destVar, node, leafTypeDict, names))
        elif isinstance(node, AsnEnumerated):
            lines.extend(self.MapEnumerated(srcVar, destVar, node, leafTypeDict, names))
        elif isinstance(node, AsnMetaMember):
            lines.extend(self.Map(srcVar, destVar, names[node._containedType], leafTypeDict, names))
        else:
            panicWithCallStack("unsupported %s (%s)" % (str(node.__class__), node.Location()))
        return lines