• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2016 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16from acts.controllers.relay_lib.errors import RelayConfigError
17from six import string_types
18
19MISSING_KEY_ERR_MSG = 'key "%s" missing from %s. Offending object:\n %s'
20TYPE_MISMATCH_ERR_MSG = 'Key "%s" is of type %s. Expecting %s.' \
21                        ' Offending object:\n %s'
22
23
24def validate_key(key, dictionary, expected_type, source):
25    """Validates if a key exists and its value is the correct type.
26    Args:
27        key: The key in dictionary.
28        dictionary: The dictionary that should contain key.
29        expected_type: the type that key's value should have.
30        source: The name of the object being checked. Used for error messages.
31
32    Returns:
33        The value of dictionary[key] if no error was raised.
34
35    Raises:
36        RelayConfigError if the key does not exist, or is not of expected_type.
37    """
38    if key not in dictionary:
39        raise RelayConfigError(MISSING_KEY_ERR_MSG % (key, source, dictionary))
40    if expected_type == str:
41        if not isinstance(dictionary[key], string_types):
42            raise RelayConfigError(TYPE_MISMATCH_ERR_MSG %
43                                   (key, dictionary[key], expected_type,
44                                    dictionary))
45    elif not isinstance(dictionary[key], expected_type):
46        raise RelayConfigError(TYPE_MISMATCH_ERR_MSG %
47                               (key, dictionary[key], expected_type,
48                                dictionary))
49    return dictionary[key]
50