""" A function that helps develop code for both Python 2 and 3.

Source: https://github.com/mitsuhiko/flask/blob/0.10.1/flask/_compat.py
"""


def with_metaclass(meta, *bases):
    # This requires a bit of explanation: the basic idea is to make a
    # dummy metaclass for one level of class instantiation that replaces
    # itself with the actual metaclass. Because of internal type checks
    # we also need to make sure that we downgrade the custom metaclass
    # for one level to something closer to type (that's why __call__ and
    # __init__ comes back from type etc.).
    #
    # This has the advantage over six.with_metaclass in that it does not
    # introduce dummy classes into the final MRO.
    class metaclass(meta):
        __call__ = type.__call__
        __init__ = type.__init__

        def __new__(cls, name, this_bases, d):
            if this_bases is None:
                return type.__new__(cls, name, (), d)
            return meta(name, bases, d)

    return metaclass('temporary_class', None, {})


class Meta(type):

    def __new__(cls, name, bases, d):
        new_attr = {}

        for name, val in d.items():
            if name.startswith('__'):
                new_attr[name] = val
            else:
                new_attr[name.upper()] = val
        return type.__new__(cls, name, bases, new_attr)


class Foo(with_metaclass(Meta, object)):
    bar = 'test'


def main():
    print Foo.BAR


if __name__ == '__main__':
    main()
