# Utilities

class headerparser.NormalizedDict(data=None, normalizer=None, body=None)

  A generalization of a case-insensitive dictionary. `NormalizedDict` takes a callable (the “normalizer”) that is applied to any key passed to its `__getitem__`, `__setitem__`, or `__delitem__` method, and the result of the call is then used for the actual lookup. When iterating over a `NormalizedDict`, each key is returned as the “pre-normalized” form passed to `__setitem__` the last time the key was set (but see `normalized()` below). Aside from this, `NormalizedDict` behaves like a normal `MutableMapping` class.

  If a normalizer is not specified upon instantiation, a default will be used that converts strings to lowercase and leaves everything else unchanged, so `NormalizedDict` defaults to yet another case-insensitive dictionary.

  Two `NormalizedDict` instances compare equal iff their normalizers, bodies, and `normalized_dict()` return values are equal. When comparing a `NormalizedDict` to any other type of mapping, the other mapping is first converted to a `NormalizedDict` using the same normalizer.

  Parameters

    - data (mapping) – a mapping or iterable of `(key, value)` pairs with which to initialize the instance

    - normalizer (callable) – A callable to apply to keys before looking them up; defaults to `lower`. The callable MUST be idempotent (i.e., `normalizer(x)` must equal `normalizer(normalizer(x))` for all inputs) or else bad things will happen to your dictionary.

    - body (string or `None`) – initial value for the `body` attribute

  body

    This is where `HeaderParser` stores the message body (if any) accompanying the header section represented by the mapping

  copy()

    Create a shallow copy of the mapping

  normalized()

    Return a copy of the instance such that iterating over it will return normalized keys instead of the keys passed to `__setitem__`

        >>> normdict = NormalizedDict()
        >>> normdict['Foo'] = 23
        >>> normdict['bar'] = 42
        >>> sorted(normdict)
        ['Foo', 'bar']
        >>> sorted(normdict.normalized())
        ['bar', 'foo']

    Return type

      NormalizedDict

  normalized_dict()

    Convert to a `dict` with all keys normalized. (A `dict` with non-normalized keys can be obtained with `dict(normdict)`.)

    Return type

      dict

headerparser.BOOL(s)

  Convert boolean-like strings to `bool` values. The strings `'yes'`, `'y'`, `'on'`, `'true'`, and `'1'` are converted to `True`, and the strings `'no'`, `'n'`, `'off'`, `'false'`, and `'0'` are converted to `False`. The conversion is case-insensitive and ignores leading & trailing whitespace. Any value that cannot be converted to a `bool` results in a `ValueError`.

  Parameters

    s (string) – a boolean-like string to convert to a `bool`

  Return type

    bool

  Raises

    ValueError – if `s` is not one of the values listed above

headerparser.lower(s)

  New in version 0.2.0.

  Convert `s` to lowercase by calling its `lower()` method if it has one; otherwise, return `s` unchanged

headerparser.unfold(s)

  New in version 0.2.0.

  Remove folding whitespace from a string by converting line breaks (and any whitespace adjacent to line breaks) to a single space and removing leading & trailing whitespace.

      >>> unfold('This is a \n folded string.\n')
      'This is a folded string.'

  Parameters

    s (string) – a string to unfold

  Return type

    string