• Home
  • 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
17
18class ApiMessage(object):
19    """The class for the messages defined by TFC API."""
20
21    def __init__(self, all_keys, **kwargs):
22        """Initializes the attributes.
23
24        Args:
25            all_keys: A collection of attribute names.
26            **kwargs: The names and values of the attributes. The names must be
27                      in all_keys.
28
29        Raises:
30            KeyError if any key in kwargs is not in all_keys.
31        """
32        for key, value in kwargs.iteritems():
33            if key not in all_keys:
34                raise KeyError(key)
35            setattr(self, key, value)
36
37    def ToJson(self, keys):
38        """Creates a JSON object containing the specified keys.
39
40        Args:
41            keys: The attributes to be included in the object.
42
43        Returns:
44            A JSON object.
45        """
46        return dict((x, getattr(self, x)) for x in keys if hasattr(self, x))
47