#
# Author: Satoru SATOH <ssato redhat.com>
# License: MIT
#
# pylint: disable=unused-import,import-error,invalid-name,wrong-import-position
"""
Compatiblity module
"""
from __future__ import absolute_import

import itertools
import sys

try:
    from logging import NullHandler
except ImportError:  # python < 2.7 doesn't have it.
    import logging

    class NullHandler(logging.Handler):
        """
        Logging handler does nothing.
        """
        def emit(self, record):
            pass


IS_PYTHON_3 = sys.version_info[0] == 3
IS_PYTHON_2_6 = sys.version_info[0] == 2 and sys.version_info[1] == 6


# Borrowed from library doc, 9.7.1 Itertools functions:
def _from_iterable(iterables):
    """
    itertools.chain.from_iterable alternative.

    >>> list(_from_iterable([[1, 2], [3, 4]]))
    [1, 2, 3, 4]
    """
    for itr in iterables:
        for element in itr:
            yield element


def py3_iteritems(dic):
    """wrapper for dict.items() in python 3.x.

    >>> list(py3_iteritems({}))
    []
    >>> sorted(py3_iteritems(dict(a=1, b=2)))
    [('a', 1), ('b', 2)]
    """
    return dic.items()


# pylint: disable=redefined-builtin
if IS_PYTHON_3:
    import configparser  # flake8: noqa
    from io import StringIO  # flake8: noqa
    iteritems = py3_iteritems
    from_iterable = itertools.chain.from_iterable
    raw_input = input
    STR_TYPES = (str, )
else:
    import ConfigParser as configparser  # flake8: noqa
    try:
        from cStringIO import StringIO  # flake8: noqa
    except ImportError:
        from StringIO import StringIO  # flake8: noqa

    try:
        from_iterable = itertools.chain.from_iterable
    except AttributeError:
        from_iterable = _from_iterable

    assert configparser  # silence pyflakes
    assert StringIO  # ditto

    def py_iteritems(dic):
        """wrapper for dict.iteritems() in python < 3.x

        >>> list(py_iteritems({}))
        []
        >>> sorted(py_iteritems(dict(a=1, b=2)))
        [('a', 1), ('b', 2)]
        """
        return dic.iteritems()

    iteritems = py_iteritems
    raw_input = raw_input
    STR_TYPES = (str, unicode)

try:
    from collections import OrderedDict
except ImportError:
    from ordereddict import OrderedDict  # Python 2.6

# vim:sw=4:ts=4:et:

@@ -498,27 +498,38 @@
  pass%0A%0A%0A
-IS_PYTHON_3
+(_PY_MAJOR, _PY_MINOR)
  = sys.v
@@ -544,80 +544,144 @@
 nfo%5B
-0%5D == 3%0AIS_PYTHON_2_6 = sys.version_info%5B0%5D == 2 and sys.version_info%5B1%5D
+:2%5D%0AIS_PYTHON_3 = _PY_MAJOR == 3%0AIS_PYTHON_3_3_OR_OLDER = _PY_MAJOR == 3 and _PY_MINOR %3C= 3%0AIS_PYTHON_2_6 = _PY_MAJOR == 2 and _PY_MINOR
  == 

add a global IS_PYTHON_3_3_OR_OLDER to identify it