recursiveMapper.py 5.77 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
    # noinspection PyMethodMayBeStatic
34
    def maybeElse(self, childNo: int) -> str:  # pylint: disable=no-self-use
35
36
37
38
39
        if childNo == 1:
            return ""
        else:
            return "else "

40
    # noinspection PyMethodMayBeStatic
41
    def CleanName(self, fieldName: str) -> str:  # pylint: disable=no-self-use
42
43
        return re.sub(r'[^a-zA-Z0-9_]', '_', fieldName)

44
    # noinspection PyMethodMayBeStatic
45
    def Version(self):  # pylint: disable=no-self-use
46
47
        panicWithCallStack("Method undefined in a RecursiveMapper...")

48
    def MapInteger(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):  # pylint: disable=no-self-use
49
50
        panicWithCallStack("Method undefined in a RecursiveMapper...")

51
    def MapReal(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):  # pylint: disable=no-self-use
52
53
        panicWithCallStack("Method undefined in a RecursiveMapper...")

54
    def MapBoolean(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):  # pylint: disable=no-self-use
55
56
        panicWithCallStack("Method undefined in a RecursiveMapper...")

57
    def MapOctetString(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):  # pylint: disable=no-self-use
58
59
        panicWithCallStack("Method undefined in a RecursiveMapper...")

60
    def MapEnumerated(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):  # pylint: disable=no-self-use
61
62
        panicWithCallStack("Method undefined in a RecursiveMapper...")

63
    def MapSequence(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):  # pylint: disable=no-self-use
64
65
        panicWithCallStack("Method undefined in a RecursiveMapper...")

66
    def MapSet(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):  # pylint: disable=no-self-use
67
68
        panicWithCallStack("Method undefined in a RecursiveMapper...")

69
    def MapChoice(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):  # pylint: disable=no-self-use
70
71
        panicWithCallStack("Method undefined in a RecursiveMapper...")

72
    def MapSequenceOf(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):  # pylint: disable=no-self-use
73
74
        panicWithCallStack("Method undefined in a RecursiveMapper...")

75
    def MapSetOf(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):  # pylint: disable=no-self-use
76
77
        panicWithCallStack("Method undefined in a RecursiveMapper...")

78
    def Map(self,
79
80
            srcVar: str,
            destVar: str,
81
82
            node_or_str: Union[str, AsnNode],
            leafTypeDict: Dict[str, str],
83
            names: Dict[str, AsnNode]) -> List[str]:  # pylint: disable=invalid-sequence-index
84
85
86
87
88
        if isinstance(node_or_str, str):
            node = names[node_or_str]  # type: AsnNode
        else:
            node = node_or_str
        lines = []  # type: List[str]
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
        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