#!ipy

"""
++++++++++
 utils.py
++++++++++

 author: Jesse Wiles
 copyright: Quadralay Corporation, 2007.
 
 A collection of utility classes.
"""

class DictAsObject(dict):
    def __init__(self, initdict):
        self.update(initdict)

    def __getattr__(self, name):
        if self.has_key(name):
            return self[name]
        elif self.has_key(name.upper()):
            return self[name.upper()]
        else:
            possible_dict = {}
            for key in self.keys():
                spl = key.split('.')
                if name == spl[0]:
                    possible_dict[spl[1]] = self[key]

            if len(possible_dict) > 0:
                return DictAsObject(possible_dict)
            else:
                raise AttributeError

def get_web_uri(uri):
    from System.IO import StreamReader
    from System.Net import CredentialCache, WebRequest

    result = None

    req = WebRequest.Create(uri)
    req.Credentials = CredentialCache.DefaultCredentials
    response = req.GetResponse()

    data = response.GetResponseStream()
    reader = StreamReader(data)
    result = reader.ReadToEnd()

    reader.Close()
    data.Close()
    response.Close()
    
    return result

if __name__ == '__main__':
    h = {'a': 'apple', 'b': 'banana', 'c': 'cantelope', 'd': 'dinosaur'}
    e = DictAsObject(h)
    print e.a, e.b, e.c, e.d