recursiveMapper.py 5.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#
# (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
Maxime Perrotin's avatar
Maxime Perrotin committed
11
# with the terms of the GNU Lesser General Public License version 3.
12
#
Maxime Perrotin's avatar
Maxime Perrotin committed
13
# GNU LLGPL v. 2.1:
14
15
#       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
Maxime Perrotin's avatar
Maxime Perrotin committed
16
# GNU Lesser General Public License version 3.
17
18
19
20
21
#
# Note that in both cases, there are no charges (royalties) for the
# generated code.
#
import re
22
from typing import Union, List, Dict
23
24

from commonPy.utility import panicWithCallStack
25
26
27
28
from commonPy.asnAST import (
    AsnBasicNode, AsnSequence, AsnSet, AsnChoice, AsnSequenceOf,
    AsnSetOf, AsnEnumerated, AsnMetaMember, AsnNode
)
29
30
31
32


class RecursiveMapper:

33
    def maybeElse(self, childNo: int) -> str:
34
35
36
37
38
        if childNo == 1:
            return ""
        else:
            return "else "

39
    def CleanName(self, fieldName: str) -> str:
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
        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...")

75
76
77
78
79
80
81
82
83
84
85
    def Map(self,
            srcVar,
            destVar,
            node_or_str: Union[str, AsnNode],
            leafTypeDict: Dict[str, str],
            names: Dict[str, AsnNode]) -> List[str]:
        if isinstance(node_or_str, str):
            node = names[node_or_str]  # type: AsnNode
        else:
            node = node_or_str
        lines = []  # type: List[str]
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
        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