1#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import logging
18import sys
19
20from vts.proto import ComponentSpecificationMessage_pb2 as CompSpecMsg
21
22
23def PbEnum2PyValue(var):
24    """Converts VariableSecificationMessage (Enum) to Python value.
25
26    Args:
27        var: VariableSpecificationMessage to convert.
28
29    Returns:
30        a converted value.
31    """
32    return getattr(var.scalar_value, var.scalar_type)
33
34
35def PbMask2PyValue(var):
36    """Converts VariableSecificationMessage (Mask) to Python value.
37
38    Args:
39        var: VariableSpecificationMessage to convert.
40
41    Returns:
42        a converted value.
43    """
44    return getattr(var.scalar_value, var.scalar_type)
45
46
47def PbScalar2PyValue(var):
48    """Converts VariableSecificationMessage (Scalar) to Python value.
49
50    Args:
51        message: VariableSpecificationMessage to convert.
52
53    Returns:
54        Converted scalar value.
55    """
56    return getattr(var.scalar_value, var.scalar_type)
57
58
59def PbString2PyString(var):
60    """Converts VTS VariableSecificationMessage (String) to Python string.
61
62    Args:
63        var: VariableSpecificationMessage to convert.
64
65    Returns:
66        Converted string.
67    """
68    return var.string_value.message
69
70
71def PbVector2PyList(var):
72    """Converts VariableSecificationMessage (Vector) to a Python list.
73
74    Args:
75        var: VariableSpecificationMessage to convert.
76
77    Returns:
78        A converted list if valid, None otherwise.
79    """
80    result = []
81    for curr_value in var.vector_value:
82        if curr_value.type == CompSpecMsg.TYPE_SCALAR:
83            result.append(PbScalar2PyValue(curr_value))
84        elif curr_value.type == CompSpecMsg.TYPE_STRUCT:
85            result.append(PbStruct2PyDict(curr_value))
86        else:
87            logging.error("unsupported type %s", curr_value.type)
88            return None
89    return result
90
91
92def PbArray2PyList(var):
93    """Converts VariableSecificationMessage (Array) to a Python list.
94
95    Args:
96        var: VariableSpecificationMessage to convert.
97
98    Returns:
99        A converted list if valid, None otherwise
100    """
101    result = []
102    for curr_value in var.vector_value:
103        if curr_value.type == CompSpecMsg.TYPE_SCALAR:
104            result.append(PbScalar2PyValue(curr_value))
105        elif curr_value.type == CompSpecMsg.TYPE_STRUCT:
106            result.append(PbStruct2PyDict(curr_value))
107        else:
108            logging.error("unsupported type %s", curr_value.type)
109            return None
110    return result
111
112
113def PbStruct2PyDict(var):
114    """Converts VariableSecificationMessage (struct) to Python dict.
115
116    Args:
117        var: VariableSpecificationMessage to convert.
118
119    Returns:
120        a dict, containing the converted data if valid. None otherwise.
121    """
122    result = {}
123    for attr in var.struct_value:
124        if attr.type == CompSpecMsg.TYPE_ENUM:
125            result[attr.name] = PbEnum2PyValue(attr)
126        elif attr.type == CompSpecMsg.TYPE_SCALAR:
127            result[attr.name] = PbScalar2PyValue(attr)
128        elif attr.type == CompSpecMsg.TYPE_STRING:
129            result[attr.name] = PbString2PyString(attr)
130        elif attr.type == CompSpecMsg.TYPE_VECTOR:
131            result[attr.name] = PbVector2PyList(attr)
132        elif attr.type == CompSpecMsg.TYPE_STRUCT:
133            result[attr.name] = PbStruct2PyDict(attr)
134        elif attr.type == CompSpecMsg.TYPE_Array:
135            result[attr.name] = PbArray2PyList(attr)
136        else:
137            logging.error("PyDict2PbStruct: unsupported type %s",
138                          attr.type)
139            return None
140    return result
141
142
143def PbPredefined2PyValue(var):
144    """Converts VariableSecificationMessage (PREDEFINED_TYPE) to Python value.
145
146    Args:
147        var: VariableSpecificationMessage to convert.
148
149    Returns:
150        a converted value.
151    """
152    return var.predefined_type
153
154
155def Convert(var):
156    """Converts VariableSecificationMessage to Python native data structure.
157
158    Args:
159        var: VariableSpecificationMessage of a target variable to convert.
160
161    Returns:
162        A list containing the converted Python values if valid. None otherwise.
163    """
164    if var.type == CompSpecMsg.TYPE_PREDEFINED:
165        return PbPredefined2PyValue(var)
166    elif var.type == CompSpecMsg.TYPE_SCALAR:
167        return PbScalar2PyValue(var)
168    elif var.type == CompSpecMsg.TYPE_VECTOR:
169        return PbVector2PyList(var)
170    elif var.type == CompSpecMsg.TYPE_STRUCT:
171        return PbStruct2PyDict(var)
172    elif var.type == CompSpecMsg.TYPE_ENUM:
173        return PbEnum2PyValue(var)
174    elif var.type == CompSpecMsg.TYPE_STRING:
175        return PbString2PyString(var)
176    elif var.type == CompSpecMsg.TYPE_MASK:
177        return PbMask2PyValue(var)
178    else:
179        logging.error("Got unsupported callback arg type %s" % var.type)
180        return None
181
182    return message
183