diff options
| author | Aymeric Augustin | 2013-12-15 13:16:12 +0100 |
|---|---|---|
| committer | Aymeric Augustin | 2013-12-15 13:16:12 +0100 |
| commit | 35a1cb5e27889931a3a94ad095b742a91399eee5 (patch) | |
| tree | a3db8169a9c23e0cf02a6dd7b5f7eae9e97e1f23 /debug_toolbar/utils.py | |
| parent | f9420501367bb4e8eb65c3b131185f2544710bf5 (diff) | |
| parent | 38de94d2d6586c716256838e41abe518898030d6 (diff) | |
| download | django-debug-toolbar-35a1cb5e27889931a3a94ad095b742a91399eee5.tar.bz2 | |
Merge branch 'master' into 1.0-release
Diffstat (limited to 'debug_toolbar/utils.py')
| -rw-r--r-- | debug_toolbar/utils.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/debug_toolbar/utils.py b/debug_toolbar/utils.py index d84c79c..0aedb54 100644 --- a/debug_toolbar/utils.py +++ b/debug_toolbar/utils.py @@ -4,6 +4,10 @@ import inspect import os.path import re import sys +try: + import threading +except ImportError: + threading = None import django from django.core.exceptions import ImproperlyConfigured @@ -199,3 +203,32 @@ def get_stack(context=1): framelist.append((frame,) + getframeinfo(frame, context)) frame = frame.f_back return framelist + + +class ThreadCollector(object): + def __init__(self): + if threading is None: + raise NotImplementedError( + "threading module is not available, " + "this panel cannot be used without it") + self.collections = {} # a dictionary that maps threads to collections + + def get_collection(self, thread=None): + """ + Returns a list of collected items for the provided thread, of if none + is provided, returns a list for the current thread. + """ + if thread is None: + thread = threading.currentThread() + if thread not in self.collections: + self.collections[thread] = [] + return self.collections[thread] + + def clear_collection(self, thread=None): + if thread is None: + thread = threading.currentThread() + if thread in self.collections: + del self.collections[thread] + + def collect(self, item, thread=None): + self.get_collection(thread).append(item) |
