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

# Source Code for Module Products.ZenUtils.mock

     1  ##############################################################################
     2  #
     3  # Copyright (C) Zenoss, Inc. 2009, 2010, all rights reserved.
     4  #
     5  # This content is made available according to terms specified in
     6  # License.zenoss under the directory where your Zenoss product is installed.
     7  #
     8  ##############################################################################
     9
    10
    11 -class MockObject(object):
    12      """
    13      An object that takes a hashmap and uses it for the attributes on
    14      the object.  Setting attributes is ignored.  Retrieving an
    15      unknown attribute returns an empty MockObject.  The key 'return__' is
    16      special in that its corresponding value will be returned if the object
    17      is called as a function.
    18
    19      >>> a=MockObject(b='c')
    20      >>> a.b
    21      'c'
    22      >>> a.d
    23      {}
    24      >>> a.d.e
    25      {}
    26      >>> x=MockObject(return__=5)
    27      >>> y=MockObject(z=x)
    28      >>> y.z()
    29      5
    30      """
    31 -    def __call__(self, *args, **kw):
    32          return self.attrs.get( 'return__', None )
    33
    34 -    def __init__(self, **kw):
    35          self.attrs = kw.copy()
    36
    37 -    def __getattr__(self, item):
    38          if item == 'attrs':
    39              return self.__dict__['attrs']
    40
    41          try:
    42              return self.attrs[item]
    43          except KeyError:
    44              return MockObject()
    45
    46 -    def __repr__(self):
    47          return str(self.attrs)
    48
    49 -    def __str__(self):
    50          return self.__repr__()
    51