# Path: smartsnippets/exceptions.py
# class WidgetAlreadyRegistered(Exception):
#     pass
# 
# class WidgetNotRegistered(Exception):
#     pass
# 
# Path: smartsnippets/widgets_base.py
# class SmartSnippetWidgetBase(object):
#     name = 'Base Widget'
#     model = SmartSnippetVariable
#     template = None
# 
#     def __init__(self, variable, **kwargs):
#         self.variable = variable
# 
#     @property
#     def formatted_value(self):
#         return self.variable.value
# 
#     def get_extra_data(self, request):
#         return {}
# 
#     def render(self, request, context=None):
#         if self.template is None:
#             raise NotImplementedError(
#                 "render needs to be implemented or set a default template")
# 
#         default_data = {'field': self.variable}
#         select_template = list(self.variable.templates) + [self.template]
#         return render_to_string(
#             select_template,
#             dict(self.get_extra_data(request).items() + default_data.items()),
#             context_instance=(context or RequestContext(request))
#         )


from django.core.exceptions import ImproperlyConfigured
from cms.utils.django_load import load
from smartsnippets.exceptions import WidgetAlreadyRegistered, WidgetNotRegistered
from smartsnippets.widgets_base import SmartSnippetWidgetBase
from models import SmartSnippetVariable
from collections import defaultdict


class WidgetPool(object):
    def __init__(self):
        self.widgets = {}
        self._settings = defaultdict(dict)
        self.discovered = False

    def discover_widgets(self):
        if self.discovered:
            return
        self.discovered = True
        load('widgets')

    def register_settings(self, namespace, settings_dict):
        self._settings[namespace].update(settings_dict)

    def unregister_settings(self, namespace, settings_keys=None):
        if settings_keys:
            for item in settings_keys:
                self._settings[namespace].pop(item, None)
        else:
            self._settings.pop(namespace, None)

    def register_widget(self, widget):

# Path: smartsnippets/exceptions.py
# class WidgetAlreadyRegistered(Exception):
#     pass
# 
# class WidgetNotRegistered(Exception):
#     pass
# 
# Path: smartsnippets/widgets_base.py
# class SmartSnippetWidgetBase(object):
#     name = 'Base Widget'
#     model = SmartSnippetVariable
#     template = None
# 
#     def __init__(self, variable, **kwargs):
#         self.variable = variable
# 
#     @property
#     def formatted_value(self):
#         return self.variable.value
# 
#     def get_extra_data(self, request):
#         return {}
# 
#     def render(self, request, context=None):
#         if self.template is None:
#             raise NotImplementedError(
#                 "render needs to be implemented or set a default template")
# 
#         default_data = {'field': self.variable}
#         select_template = list(self.variable.templates) + [self.template]
#         return render_to_string(
#             select_template,
#             dict(self.get_extra_data(request).items() + default_data.items()),
#             context_instance=(context or RequestContext(request))
#         )


# Path: smartsnippets/widgets_pool.py
from django.core.exceptions import ImproperlyConfigured
from cms.utils.django_load import load
from smartsnippets.exceptions import WidgetAlreadyRegistered, WidgetNotRegistered
from smartsnippets.widgets_base import SmartSnippetWidgetBase
from models import SmartSnippetVariable
from collections import defaultdict



class WidgetPool(object):
    def __init__(self):
        self.widgets = {}
        self._settings = defaultdict(dict)
        self.discovered = False

    def discover_widgets(self):
        if self.discovered:
            return
        self.discovered = True
        load('widgets')

    def register_settings(self, namespace, settings_dict):
        self._settings[namespace].update(settings_dict)

    def unregister_settings(self, namespace, settings_keys=None):
        if settings_keys:
            for item in settings_keys:
                self._settings[namespace].pop(item, None)
        else:
            self._settings.pop(namespace, None)

    def register_widget(self, widget):

        if not issubclass(widget, SmartSnippetWidgetBase):