• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PbScalar2PyValue(var):
36    """Converts VariableSecificationMessage (Scalar) to Python value.
37
38    Args:
39        message: VariableSpecificationMessage to convert.
40
41    Returns:
42        Converted scalar value.
43    """
44    return getattr(var.scalar_value, var.scalar_type)
45
46
47def PbString2PyString(var):
48    """Converts VTS VariableSecificationMessage (String) to Python string.
49
50    Args:
51        var: VariableSpecificationMessage to convert.
52
53    Returns:
54        Converted string.
55    """
56    return var.string_value.message
57
58
59def PbVector2PyList(var):
60    """Converts VariableSecificationMessage (Vector) to a Python list.
61
62    Args:
63        var: VariableSpecificationMessage to convert.
64
65    Returns:
66        A converted list.
67    """
68    result = []
69    for curr_value in var.vector_value:
70        if curr_value.type == CompSpecMsg.TYPE_SCALAR:
71            result.append(PbScalar2PyValue(curr_value))
72        elif curr_value.type == CompSpecMsg.TYPE_STRUCT:
73            result.append(PbStruct2PyDict(curr_value))
74        else:
75            logging.error("unsupported type %s", curr_value.type)
76            sys.exit(-1)
77    return result
78
79
80def PbStruct2PyDict(var):
81    """Converts VariableSecificationMessage (struct) to Python dict.
82
83    Args:
84        var: VariableSpecificationMessage to convert.
85
86    Returns:
87        a dict, containing the converted data.
88    """
89    result = {}
90    for attr in var.struct_value:
91        if attr.type == CompSpecMsg.TYPE_ENUM:
92            result[attr.name] = PbEnum2PyValue(attr)
93        elif attr.type == CompSpecMsg.TYPE_SCALAR:
94            result[attr.name] = PbScalar2PyValue(attr)
95        elif attr.type == CompSpecMsg.TYPE_STRING:
96            result[attr.name] = PbString2PyString(attr)
97        elif attr.type == CompSpecMsg.TYPE_VECTOR:
98            result[attr.name] = PbVector2PyList(attr)
99        elif attr.type == CompSpecMsg.TYPE_STRUCT:
100            result[attr.name] = PbStruct2PyDict(attr)
101        else:
102            logging.error("PyDict2PbStruct: unsupported type %s",
103                          attr.type)
104            sys.exit(-1)
105    return result
106
107
108def PbPredefined2PyValue(var):
109    """Converts VariableSecificationMessage (PREDEFINED_TYPE) to Python value.
110
111    Args:
112        var: VariableSpecificationMessage to convert.
113
114    Returns:
115        a converted value.
116    """
117    return var.predefined_type
118
119
120def Convert(var):
121    """Converts VariableSecificationMessage to Python native data structure.
122
123    Args:
124        var: VariableSpecificationMessage of a target variable to convert.
125
126    Returns:
127        A list containing the converted Python values.
128    """
129    if var.type == CompSpecMsg.TYPE_PREDEFINED:
130        return PbPredefined2PyValue(var)
131    elif var.type == CompSpecMsg.TYPE_SCALAR:
132        return PbScalar2PyValue(var)
133    elif var.type == CompSpecMsg.TYPE_VECTOR:
134        return PbVector2PyList(var)
135    elif var.type == CompSpecMsg.TYPE_STRUCT:
136        return PbStruct2PyDict(var)
137    elif var.type == CompSpecMsg.TYPE_ENUM:
138        return PbEnum2PyValue(var)
139    elif var.type == CompSpecMsg.TYPE_STRING:
140        return PbString2PyString(var)
141    else:
142        logging.error("Got unsupported callback arg type %s" % var.type)
143        sys.exit(-1)
144
145    return message
146