From ed00089a1a6df7ba6439ead4b600ecdb7e9e2acb Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Wed, 7 Nov 2012 22:53:39 -0500 Subject: [PATCH] added user level method to get current user of thread --- python_apps/media-monitor2/media/saas/thread.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/python_apps/media-monitor2/media/saas/thread.py b/python_apps/media-monitor2/media/saas/thread.py index 489ad35a8..f2e5ae28d 100644 --- a/python_apps/media-monitor2/media/saas/thread.py +++ b/python_apps/media-monitor2/media/saas/thread.py @@ -1,10 +1,12 @@ import threading -tc = threading.local() +class UserlessThread(Exception): + def __str__(): + return "Current thread: %s is not an instance of InstanceThread \ + of InstanceInheritingThread" % str(threading.current_thread()) class HasUser(object): - def user(self): - return self._user + def user(self): return self._user class InstanceThread(threading.Thread, HasUser): def __init__(self,user, *args, **kwargs): @@ -15,3 +17,7 @@ class InstanceInheritingThread(threading.Thread, HasUser): def __init__(self, *args, **kwargs): self._user = threading.current_thread().user() super(InstanceInheritingThread, self).__init__(*args, **kwargs) + +def user(): + try: return threading.current_thread().user() + except AttributeError: raise UserlessThread()