# Path: feature_toggle/constants.py
# class Attribute(Container):
# class Environment(Container):
#     def __init__(self):
#     def __init__(self):
# 
# Path: feature_toggle/exceptions.py
# class FeatureToggleDoesNotExist(Exception):
#     """
#     This exception is to aid consumers in providing an common exception to catch instead of relying on
#     model.DoesNotExist.
#     """
#     pass
# 
# Path: feature_toggle/toggle.py
# class BaseToggle(object):
#     """
#     Interface for implementing toggle class
#     """
# 
#     class Attributes(object):
#         def __init__(self, attribs):
#             assert isinstance(attribs, dict)
# 
#             self.attribs = attribs
# 
#         def __getattr__(self, item):
#             try:
#                 return self.attribs[item]
#             except KeyError:
#                 raise AttributeError(item)
# 
#     def __init__(self, uid, name, environment, is_active, attributes=None, **kwargs):
#         self.uid = uid
#         self.name = name
#         assert environment in Environments
#         self.environment = environment
#         self.is_active = is_active
# 
#         if attributes:
#             self.attributes = self.Attributes(attributes)
# 
#         self.kwargs = kwargs
# 
#     def __bool__(self):
#         return bool(self.is_active)
# 
#     __nonzero__ = __bool__
# 
# class Toggle(BaseToggle):
# 
#     def __init__(self, uid, name, environment, is_active,
#                  start_date_time, end_date_time,
#                  attributes=None, **kwargs):
#         assert isinstance(start_date_time, datetime.datetime)  # should be a time stamp
#         assert start_date_time.tzinfo is not None  # should have a time zone. otherwise causes confusions
#         self.start_date_time = start_date_time
# 
#         assert isinstance(end_date_time, datetime.datetime)  # should be a time stamp
#         assert end_date_time.tzinfo is not None  # should have a time zone. otherwise causes confusions
#         self.end_date_time = end_date_time
#         super().__init__(uid, name, environment, is_active, attributes, **kwargs)
# 
#     def __bool__(self):
#         active = super().__bool__()
# 
#         if self.end_date_time and self.end_date_time < utc_now():
#             # this check because toggles can quickly get overwhelming
#             warnings.warn('{t} has expired. Get rid of me and help me attain salvation'.format(t=str(self)))
# 
#         return active
# 
# Path: feature_toggle/models.py
# class FeatureToggle(TimeStampedModel):
#     uid = models.CharField(max_length=20)
#     name = models.CharField(max_length=20)
#     environment = models.CharField(max_length=50, choices=ENV_CHOICES)
#     is_active = models.BooleanField(default=True)
#     start_date_time = models.DateTimeField(auto_now_add=True)
#     end_date_time = models.DateTimeField(auto_now_add=True)  # todo: add 3 months from now by default
#     time_bomb = models.BooleanField(default=False)
# 
#     class Meta:
#         unique_together = (('name', 'environment'), ('uid',))
#         managed = True
# 
#     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
#         if not self.uid:
#             self.uid = make_meanigful_id(self.name, length=5)
#         super().save(force_insert, force_update, using, update_fields)
# 
#     def set_attribute(self, key, value=None, update_if_existing=True):
#         attrib, created = self.attributes.get_or_create(key=key)
#         if not created and not update_if_existing:
#             raise FeatureToggleAttributeAlreadyExists(**dict(key=key))
# 
#         attrib.value = value
#         attrib.save()
# 
#     def __str__(self):
#         return "{e}: {n}({u})".format(e=self.environment, n=self.name, u=self.uid)
# 
#     def __unicode__(self):
#         return self.__str__()


import logging
from feature_toggle.constants import Environments
from feature_toggle.exceptions import FeatureToggleDoesNotExist
from feature_toggle.toggle import BaseToggle, Toggle
from .models import FeatureToggle
# -*- coding: utf-8 -*-




logger = logging.getLogger(__name__)


class FeatureToggleService(object):

    @classmethod
    def get(cls, uid):
        tgl = cls.get_model(uid)
        return cls.convert(toggle_model=tgl)

    @classmethod
    def create(cls, toggle):
        assert isinstance(toggle, BaseToggle)
        tgl = FeatureToggle.objects.create(name=toggle.name)
        for key, value in tgl.attributes.items():
            tgl.set_attribute(key, value)

    @classmethod
    def get_model(cls, uid):
        try:

# Path: feature_toggle/constants.py
# class Attribute(Container):
# class Environment(Container):
#     def __init__(self):
#     def __init__(self):
# 
# Path: feature_toggle/exceptions.py
# class FeatureToggleDoesNotExist(Exception):
#     """
#     This exception is to aid consumers in providing an common exception to catch instead of relying on
#     model.DoesNotExist.
#     """
#     pass
# 
# Path: feature_toggle/toggle.py
# class BaseToggle(object):
#     """
#     Interface for implementing toggle class
#     """
# 
#     class Attributes(object):
#         def __init__(self, attribs):
#             assert isinstance(attribs, dict)
# 
#             self.attribs = attribs
# 
#         def __getattr__(self, item):
#             try:
#                 return self.attribs[item]
#             except KeyError:
#                 raise AttributeError(item)
# 
#     def __init__(self, uid, name, environment, is_active, attributes=None, **kwargs):
#         self.uid = uid
#         self.name = name
#         assert environment in Environments
#         self.environment = environment
#         self.is_active = is_active
# 
#         if attributes:
#             self.attributes = self.Attributes(attributes)
# 
#         self.kwargs = kwargs
# 
#     def __bool__(self):
#         return bool(self.is_active)
# 
#     __nonzero__ = __bool__
# 
# class Toggle(BaseToggle):
# 
#     def __init__(self, uid, name, environment, is_active,
#                  start_date_time, end_date_time,
#                  attributes=None, **kwargs):
#         assert isinstance(start_date_time, datetime.datetime)  # should be a time stamp
#         assert start_date_time.tzinfo is not None  # should have a time zone. otherwise causes confusions
#         self.start_date_time = start_date_time
# 
#         assert isinstance(end_date_time, datetime.datetime)  # should be a time stamp
#         assert end_date_time.tzinfo is not None  # should have a time zone. otherwise causes confusions
#         self.end_date_time = end_date_time
#         super().__init__(uid, name, environment, is_active, attributes, **kwargs)
# 
#     def __bool__(self):
#         active = super().__bool__()
# 
#         if self.end_date_time and self.end_date_time < utc_now():
#             # this check because toggles can quickly get overwhelming
#             warnings.warn('{t} has expired. Get rid of me and help me attain salvation'.format(t=str(self)))
# 
#         return active
# 
# Path: feature_toggle/models.py
# class FeatureToggle(TimeStampedModel):
#     uid = models.CharField(max_length=20)
#     name = models.CharField(max_length=20)
#     environment = models.CharField(max_length=50, choices=ENV_CHOICES)
#     is_active = models.BooleanField(default=True)
#     start_date_time = models.DateTimeField(auto_now_add=True)
#     end_date_time = models.DateTimeField(auto_now_add=True)  # todo: add 3 months from now by default
#     time_bomb = models.BooleanField(default=False)
# 
#     class Meta:
#         unique_together = (('name', 'environment'), ('uid',))
#         managed = True
# 
#     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
#         if not self.uid:
#             self.uid = make_meanigful_id(self.name, length=5)
#         super().save(force_insert, force_update, using, update_fields)
# 
#     def set_attribute(self, key, value=None, update_if_existing=True):
#         attrib, created = self.attributes.get_or_create(key=key)
#         if not created and not update_if_existing:
#             raise FeatureToggleAttributeAlreadyExists(**dict(key=key))
# 
#         attrib.value = value
#         attrib.save()
# 
#     def __str__(self):
#         return "{e}: {n}({u})".format(e=self.environment, n=self.name, u=self.uid)
# 
#     def __unicode__(self):
#         return self.__str__()


# Path: feature_toggle/services.py
import logging
from feature_toggle.constants import Environments
from feature_toggle.exceptions import FeatureToggleDoesNotExist
from feature_toggle.toggle import BaseToggle, Toggle
from .models import FeatureToggle

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




logger = logging.getLogger(__name__)


class FeatureToggleService(object):

    @classmethod
    def get(cls, uid):
        tgl = cls.get_model(uid)
        return cls.convert(toggle_model=tgl)

    @classmethod
    def create(cls, toggle):
        assert isinstance(toggle, BaseToggle)
        tgl = FeatureToggle.objects.create(name=toggle.name)
        for key, value in tgl.attributes.items():
            tgl.set_attribute(key, value)

    @classmethod
    def get_model(cls, uid):
        try:

            return FeatureToggle.objects.get(uid=uid)