# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from __future__ import print_function

# ripped from djangotoolbox
# duplicated here to avoid external dependency


def make_tls_property(default=None):
    """Creates a class-wide instance property with a thread-specific value."""
    class TLSProperty(object):
        def __init__(self):
            from threading import local
            self.local = local()

        def __get__(self, instance, cls):
            if not instance:
                return self
            return self.value

        def __set__(self, instance, value):
            self.value = value

        def _get_value(self):
            return getattr(self.local, 'value', default)

        def _set_value(self, value):
            self.local.value = value

        value = property(_get_value, _set_value)

    return TLSProperty()
