Package Products :: Package ZenUtils :: Module mock |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.mock

     1  ###########################################################################
     2  #
     3  # This program is part of Zenoss Core, an open source monitoring platform.
     4  # Copyright (C) 2009, 2010, Zenoss Inc.
     5  #
     6  # This program is free software; you can redistribute it and/or modify it
     7  # under the terms of the GNU General Public License version 2 or (at your
     8  # option) any later version as published by the Free Software Foundation.
     9  #
    10  # For complete information please visit: http://www.zenoss.com/oss/
    11  #
    12  ###########################################################################
    13
    14 -class MockObject(object):
    15      """
    16      An object that takes a hashmap and uses it for the attributes on
    17      the object.  Setting attributes is ignored.  Retrieving an
    18      unknown attribute returns an empty MockObject.  The key 'return__' is
    19      special in that its corresponding value will be returned if the object
    20      is called as a function.
    21
    22      >>> a=MockObject(b='c')
    23      >>> a.b
    24      'c'
    25      >>> a.d
    26      {}
    27      >>> a.d.e
    28      {}
    29      >>> x=MockObject(return__=5)
    30      >>> y=MockObject(z=x)
    31      >>> y.z()
    32      5
    33      """
    34 -    def __call__(self, *args, **kw):
    35          return self.attrs.get( 'return__', None )
    36
    37 -    def __init__(self, **kw):
    38          self.attrs = kw.copy()
    39
    40 -    def __getattr__(self, item):
    41          if item == 'attrs':
    42              return self.__dict__['attrs']
    43
    44          try:
    45              return self.attrs[item]
    46          except KeyError:
    47              return MockObject()
    48
    49 -    def __repr__(self):
    50          return str(self.attrs)
    51
    52 -    def __str__(self):
    53          return self.__repr__()
    54