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. 16 17 18class DictObject(dict): 19 """Optional convenient base type for creating simple objects that are 20 naturally serializable. 21 22 A DictObject provides object-oriented access semantics to a dictionary, 23 allowing it to look like a class with defined members. By ensuring that 24 all of the class members are serializable, the object can be serialized 25 as a dictionary/de-serialized from a dictionary. 26 """ 27 28 def __init__(self, *args, **kwargs): 29 """Constructor for a dictionary-as-object representation of kwargs 30 31 Args: 32 args: Currently unused - included for completeness 33 kwargs: keyword arguments used to construct the underlying dict 34 35 Returns: 36 Instance of DictObject 37 """ 38 super(DictObject, self).update(**kwargs) 39 40 def __getattr__(self, name): 41 """Returns a key from the superclass dictionary as an attribute 42 43 Args: 44 name: name of the pseudo class attribute 45 46 Returns: 47 Dictionary item stored at "name" 48 49 Raises: 50 AttributeError if the item is not found 51 """ 52 try: 53 return self[name] 54 except KeyError as ke: 55 raise AttributeError(ke) 56 57 def __setattr__(self, name, value): 58 """Updates the value of a key=name to a given value 59 60 Args: 61 name: name of the pseudo class attribute 62 value: value of the key 63 64 Raises: 65 AttributeError if the item is not found 66 """ 67 if name in super(DictObject, self).keys(): 68 super(DictObject, self).__setitem__(name, value) 69 else: 70 raise AttributeError("Class does not have attribute {}" 71 .format(value)) 72 73 @classmethod 74 def from_dict(cls, dictionary): 75 """Factory method for constructing a DictObject from a dictionary 76 77 Args: 78 dictionary: Dictionary used to construct the DictObject 79 80 Returns: 81 Instance of DictObject 82 """ 83 c = cls() 84 c.update(dictionary) 85 return c 86