diff options
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) | 
