0001"""
0002Contains some data structures.
0003"""
0004
0005from webob.util.dictmixin import DictMixin
0006
0007class EnvironHeaders(DictMixin):
0008    """An object that represents the headers as present in a
0009    WSGI environment.
0010
0011    This object is a wrapper (with no internal state) for a WSGI
0012    request object, representing the CGI-style HTTP_* keys as a
0013    dictionary.  Because a CGI environment can only hold one value for
0014    each key, this dictionary is single-valued (unlike outgoing
0015    headers).
0016    """
0017
0018    def __init__(self, environ):
0019        self.environ = environ
0020
0021    def _trans_name(self, name):
0022        key = 'HTTP_'+name.replace('-', '_').upper()
0023        if key == 'HTTP_CONTENT_LENGTH':
0024            key = 'CONTENT_LENGTH'
0025        elif key == 'HTTP_CONTENT_TYPE':
0026            key = 'CONTENT_TYPE'
0027        return key
0028
0029    def _trans_key(self, key):
0030        if key == 'CONTENT_TYPE':
0031            return 'Content-Type'
0032        elif key == 'CONTENT_LENGTH':
0033            return 'Content-Length'
0034        elif key.startswith('HTTP_'):
0035            return key[5:].replace('_', '-').title()
0036        else:
0037            return None
0038
0039    def __getitem__(self, item):
0040        return self.environ[self._trans_name(item)]
0041
0042    def __setitem__(self, item, value):
0043        self.environ[self._trans_name(item)] = value
0044
0045    def __delitem__(self, item):
0046        del self.environ[self._trans_name(item)]
0047
0048    def __iter__(self):
0049        for key in self.environ:
0050            name = self._trans_key(key)
0051            if name is not None:
0052                yield name
0053
0054    def keys(self):
0055        return list(iter(self))
0056
0057    def __contains__(self, item):
0058        return self._trans_name(item) in self.environ