From 02716d9ca2728e33e92806f6ed0b3d9375df53a9 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Tue, 12 Nov 2013 21:42:12 +0100 Subject: Remove unnecessary nesting. --- debug_toolbar/middleware.py | 2 +- debug_toolbar/models.py | 2 +- debug_toolbar/toolbar.py | 118 ++++++++++++++++++++++++++++++++++++++ debug_toolbar/toolbar/__init__.py | 0 debug_toolbar/toolbar/loader.py | 118 -------------------------------------- debug_toolbar/views.py | 2 +- tests/__init__.py | 2 +- tests/base.py | 2 +- 8 files changed, 123 insertions(+), 123 deletions(-) create mode 100644 debug_toolbar/toolbar.py delete mode 100644 debug_toolbar/toolbar/__init__.py delete mode 100644 debug_toolbar/toolbar/loader.py diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index c95cade..80e8ae0 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -11,7 +11,7 @@ from django.http import HttpResponseRedirect from django.shortcuts import render from django.utils.encoding import force_text -from debug_toolbar.toolbar.loader import DebugToolbar +from debug_toolbar.toolbar import DebugToolbar from debug_toolbar.utils.settings import CONFIG _HTML_TYPES = ('text/html', 'application/xhtml+xml') diff --git a/debug_toolbar/models.py b/debug_toolbar/models.py index fc78874..d39e7ec 100644 --- a/debug_toolbar/models.py +++ b/debug_toolbar/models.py @@ -5,7 +5,7 @@ from django.conf.urls import include, patterns, url from django.core.urlresolvers import reverse, NoReverseMatch from django.utils.importlib import import_module -from debug_toolbar.toolbar.loader import load_panel_classes +from debug_toolbar.toolbar import load_panel_classes from debug_toolbar.middleware import DebugToolbarMiddleware diff --git a/debug_toolbar/toolbar.py b/debug_toolbar/toolbar.py new file mode 100644 index 0000000..f9262f8 --- /dev/null +++ b/debug_toolbar/toolbar.py @@ -0,0 +1,118 @@ +""" +The main DebugToolbar class that loads and renders the Toolbar. +""" + +from __future__ import unicode_literals + +from django.conf import settings +from django.template.loader import render_to_string +from django.utils.datastructures import SortedDict +from django.utils.importlib import import_module + +from debug_toolbar.utils.settings import CONFIG + + +class DebugToolbar(object): + + def __init__(self, request): + self.request = request + self._panels = SortedDict() + base_url = self.request.META.get('SCRIPT_NAME', '') + self.config = {} + self.config.update(CONFIG) + self.template_context = { + 'BASE_URL': base_url, # for backwards compatibility + 'STATIC_URL': settings.STATIC_URL, + 'TOOLBAR_ROOT_TAG_ATTRS': self.config['ROOT_TAG_ATTRS'], + } + + self.load_panels() + self.stats = {} + + def _get_panels(self): + return list(self._panels.values()) + panels = property(_get_panels) + + def get_panel(self, cls): + return self._panels[cls] + + def load_panels(self): + """ + Populate debug panels + """ + global panel_classes + for panel_class in panel_classes: + panel_instance = panel_class(self, context=self.template_context) + self._panels[panel_class] = panel_instance + + def render_toolbar(self): + """ + Renders the overall Toolbar with panels inside. + """ + context = self.template_context.copy() + context.update({ + 'panels': self.panels, + 'toolbar_id': save_toolbar(self), + }) + return render_to_string('debug_toolbar/base.html', context) + + +panel_classes = [] + + +def load_panel_classes(): + from django.conf import settings + from django.core.exceptions import ImproperlyConfigured + + # Check if settings has a DEBUG_TOOLBAR_PANELS, otherwise use default + panels = getattr(settings, 'DEBUG_TOOLBAR_PANELS', ( + 'debug_toolbar.panels.version.VersionDebugPanel', + 'debug_toolbar.panels.timer.TimerDebugPanel', + 'debug_toolbar.panels.settings_vars.SettingsVarsDebugPanel', + 'debug_toolbar.panels.headers.HeaderDebugPanel', + 'debug_toolbar.panels.request_vars.RequestVarsDebugPanel', + 'debug_toolbar.panels.sql.SQLDebugPanel', + 'debug_toolbar.panels.template.TemplateDebugPanel', + 'debug_toolbar.panels.cache.CacheDebugPanel', + 'debug_toolbar.panels.signals.SignalDebugPanel', + 'debug_toolbar.panels.logger.LoggingPanel', + )) + for panel_path in panels: + try: + dot = panel_path.rindex('.') + except ValueError: + raise ImproperlyConfigured( + "%s isn't a debug panel module" % panel_path) + panel_module, panel_classname = panel_path[:dot], panel_path[dot + 1:] + try: + mod = import_module(panel_module) + except ImportError as e: + raise ImproperlyConfigured( + 'Error importing debug panel %s: "%s"' % + (panel_module, e)) + try: + panel_class = getattr(mod, panel_classname) + except AttributeError: + raise ImproperlyConfigured( + 'Toolbar Panel module "%s" does not define a "%s" class' % + (panel_module, panel_classname)) + panel_classes.append(panel_class) + + +toolbar_counter = 0 +toolbar_results = SortedDict() + + +def save_toolbar(toolbar): + global toolbar_counter, toolbar_results + toolbar_counter += 1 + toolbar_results[toolbar_counter] = toolbar + for _ in range(len(toolbar_results) - CONFIG['RESULTS_CACHE_SIZE']): + # When we drop support for Python 2.6 and switch to + # collections.OrderedDict, use popitem(last=False). + del toolbar_results[toolbar_results.keyOrder[0]] + return toolbar_counter + + +def get_saved_toolbar(toolbar_id): + return toolbar_results.get(toolbar_id) diff --git a/debug_toolbar/toolbar/__init__.py b/debug_toolbar/toolbar/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/debug_toolbar/toolbar/loader.py b/debug_toolbar/toolbar/loader.py deleted file mode 100644 index f9262f8..0000000 --- a/debug_toolbar/toolbar/loader.py +++ /dev/null @@ -1,118 +0,0 @@ -""" -The main DebugToolbar class that loads and renders the Toolbar. -""" - -from __future__ import unicode_literals - -from django.conf import settings -from django.template.loader import render_to_string -from django.utils.datastructures import SortedDict -from django.utils.importlib import import_module - -from debug_toolbar.utils.settings import CONFIG - - -class DebugToolbar(object): - - def __init__(self, request): - self.request = request - self._panels = SortedDict() - base_url = self.request.META.get('SCRIPT_NAME', '') - self.config = {} - self.config.update(CONFIG) - self.template_context = { - 'BASE_URL': base_url, # for backwards compatibility - 'STATIC_URL': settings.STATIC_URL, - 'TOOLBAR_ROOT_TAG_ATTRS': self.config['ROOT_TAG_ATTRS'], - } - - self.load_panels() - self.stats = {} - - def _get_panels(self): - return list(self._panels.values()) - panels = property(_get_panels) - - def get_panel(self, cls): - return self._panels[cls] - - def load_panels(self): - """ - Populate debug panels - """ - global panel_classes - for panel_class in panel_classes: - panel_instance = panel_class(self, context=self.template_context) - self._panels[panel_class] = panel_instance - - def render_toolbar(self): - """ - Renders the overall Toolbar with panels inside. - """ - context = self.template_context.copy() - context.update({ - 'panels': self.panels, - 'toolbar_id': save_toolbar(self), - }) - return render_to_string('debug_toolbar/base.html', context) - - -panel_classes = [] - - -def load_panel_classes(): - from django.conf import settings - from django.core.exceptions import ImproperlyConfigured - - # Check if settings has a DEBUG_TOOLBAR_PANELS, otherwise use default - panels = getattr(settings, 'DEBUG_TOOLBAR_PANELS', ( - 'debug_toolbar.panels.version.VersionDebugPanel', - 'debug_toolbar.panels.timer.TimerDebugPanel', - 'debug_toolbar.panels.settings_vars.SettingsVarsDebugPanel', - 'debug_toolbar.panels.headers.HeaderDebugPanel', - 'debug_toolbar.panels.request_vars.RequestVarsDebugPanel', - 'debug_toolbar.panels.sql.SQLDebugPanel', - 'debug_toolbar.panels.template.TemplateDebugPanel', - 'debug_toolbar.panels.cache.CacheDebugPanel', - 'debug_toolbar.panels.signals.SignalDebugPanel', - 'debug_toolbar.panels.logger.LoggingPanel', - )) - for panel_path in panels: - try: - dot = panel_path.rindex('.') - except ValueError: - raise ImproperlyConfigured( - "%s isn't a debug panel module" % panel_path) - panel_module, panel_classname = panel_path[:dot], panel_path[dot + 1:] - try: - mod = import_module(panel_module) - except ImportError as e: - raise ImproperlyConfigured( - 'Error importing debug panel %s: "%s"' % - (panel_module, e)) - try: - panel_class = getattr(mod, panel_classname) - except AttributeError: - raise ImproperlyConfigured( - 'Toolbar Panel module "%s" does not define a "%s" class' % - (panel_module, panel_classname)) - panel_classes.append(panel_class) - - -toolbar_counter = 0 -toolbar_results = SortedDict() - - -def save_toolbar(toolbar): - global toolbar_counter, toolbar_results - toolbar_counter += 1 - toolbar_results[toolbar_counter] = toolbar - for _ in range(len(toolbar_results) - CONFIG['RESULTS_CACHE_SIZE']): - # When we drop support for Python 2.6 and switch to - # collections.OrderedDict, use popitem(last=False). - del toolbar_results[toolbar_results.keyOrder[0]] - return toolbar_counter - - -def get_saved_toolbar(toolbar_id): - return toolbar_results.get(toolbar_id) diff --git a/debug_toolbar/views.py b/debug_toolbar/views.py index 21f4747..83432bf 100644 --- a/debug_toolbar/views.py +++ b/debug_toolbar/views.py @@ -13,7 +13,7 @@ from django.utils.translation import ugettext as _ from django.views.decorators.csrf import csrf_exempt from debug_toolbar.forms import SQLSelectForm -from debug_toolbar.toolbar.loader import get_saved_toolbar +from debug_toolbar.toolbar import get_saved_toolbar def render_panel(request): diff --git a/tests/__init__.py b/tests/__init__.py index 47fba24..0abb8a3 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,7 +1,7 @@ # Refresh the debug toolbar's configuration when overriding settings. from debug_toolbar.utils.settings import CONFIG, CONFIG_DEFAULTS -from debug_toolbar.toolbar.loader import load_panel_classes, panel_classes # noqa +from debug_toolbar.toolbar import load_panel_classes, panel_classes # noqa from django.dispatch import receiver from django.test.signals import setting_changed diff --git a/tests/base.py b/tests/base.py index e6f6dac..a1a59cc 100644 --- a/tests/base.py +++ b/tests/base.py @@ -6,7 +6,7 @@ from django.http import HttpResponse from django.test import TestCase, RequestFactory from debug_toolbar.middleware import DebugToolbarMiddleware -from debug_toolbar.toolbar.loader import DebugToolbar +from debug_toolbar.toolbar import DebugToolbar rf = RequestFactory() -- cgit v1.2.3