summaryrefslogtreecommitdiff
path: root/tools/InterfaceGenerator/generator/parsers/SDLRPCV2.py
blob: 24974a55272f55ef8ebe5a238c9b3ec789b9b40f (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
"""SDLRPCV2 parser.

Contains parser for SDLRPCV2 XML format.

"""

import collections

from generator import Model
from generator.parsers import RPCBase
import xml.etree.ElementTree as ET


class Parser(RPCBase.Parser):

    """SDLRPCV2 parser."""

    def _initialize_enums(self):
        """Initialize enums.

        This implementation returns empty OrderedDict because in SDLRPCV2
        all enums must be declared explicitly in the XML file.

        """
        return collections.OrderedDict()

    def _check_enum_name(self, enum):
        """Check enum name.

        This method is called to check whether the newly parsed enum's name
        conflicts with some predefined enum.
        As SDLRPCV2 has no predefined enums this implementation does nothing.

        """

        pass

    def _parse_function_id_type(self, function_name, attrib):
        """Parse function id and message type according to XML format.

        This implementation extracts attribute "FunctionID" as function id
        and messagetype as message type and searches them in enums
        "FunctionID" and "messageType". If at least one of them (or the entire
        enum) is missing it raises an error.

        Returns function id and message type as an instances of EnumElement.

        """
        if "functionID" not in attrib:
            raise RPCBase.ParseError(
                "No functionID specified for function '" +
                function_name + "'")

        if "messagetype" not in attrib:
            raise RPCBase.ParseError(
                "No messagetype specified for function '" +
                function_name + "'")

        function_id = self._get_enum_element_for_function(
            "FunctionID",
            self._extract_attrib(attrib, "functionID"))
        message_type = self._get_enum_element_for_function(
            "messageType",
            self._extract_attrib(attrib, "messagetype"))

        return function_id, message_type

    def _get_enum_element_for_function(self, enum_name, element_name):
        """Get enum element with given name from given enumeration.

        Returns an instance of generator.Model.EnumElement.

        """
        if enum_name not in self._types:
            raise RPCBase.ParseError(
                "Enumeration '" + enum_name +
                "' must be declared before any function")

        enum = self._types[enum_name]

        if type(enum) is not Model.Enum:
            raise RPCBase.ParseError("'" + enum_name +
                                     "' is not an enumeration")

        if element_name not in enum.elements:
            raise RPCBase.ParseError(
                "'" + element_name +
                "' is not a member of enum '" + enum_name + "'")

        return enum.elements[element_name]