Added debug_toolbar app to project.
[wolnelektury.git] / apps / debug_toolbar / toolbar / loader.py
1 """
2 The main DebugToolbar class that loads and renders the Toolbar.
3 """
4 from django.template.loader import render_to_string
5 from debug_toolbar.settings import DEBUG_TOOLBAR_PANELS
6
7 class DebugToolbar(object):
8
9     def __init__(self, request):
10         self.request = request
11         self.panels = []
12         self.panel_list = []
13         self.content_list = []
14     
15     def load_panels(self):
16         """
17         Populate debug panel lists from settings.DEBUG_TOOLBAR_PANELS.
18         """
19         from django.conf import settings
20         from django.core import exceptions
21
22         for panel_path in DEBUG_TOOLBAR_PANELS:
23             try:
24                 dot = panel_path.rindex('.')
25             except ValueError:
26                 raise exceptions.ImproperlyConfigured, '%s isn\'t a debug panel module' % panel_path
27             panel_module, panel_classname = panel_path[:dot], panel_path[dot+1:]
28             try:
29                 mod = __import__(panel_module, {}, {}, [''])
30             except ImportError, e:
31                 raise exceptions.ImproperlyConfigured, 'Error importing debug panel %s: "%s"' % (panel_module, e)
32             try:
33                 panel_class = getattr(mod, panel_classname)
34             except AttributeError:
35                 raise exceptions.ImproperlyConfigured, 'Toolbar Panel module "%s" does not define a "%s" class' % (panel_module, panel_classname)
36
37             try:
38                 panel_instance = panel_class(self.request)
39             except:
40                 raise
41                 continue # Some problem loading panel
42
43             self.panels.append(panel_instance)
44
45     def render_toolbar(self):
46         """
47         Renders the overall Toolbar with panels inside.
48         """
49         return render_to_string('debug_toolbar/base.html', {'panels': self.panels})