Package omero :: Package util :: Module decorators
[hide private]
[frames] | no frames]

Source Code for Module omero.util.decorators

 1  #!/usr/bin/env python 
 2  # 
 3  # OMERO Decorators 
 4  # Copyright 2009 Glencoe Software, Inc.  All Rights Reserved. 
 5  # Use is subject to license terms supplied in LICENSE.txt 
 6  # 
 7   
 8  import time 
 9  import logging 
10  import threading 
11  import traceback 
12  import exceptions 
13   
14  import omero 
15   
16  from omero_ext.functional import wraps 
17   
18 -def perf(func):
19 """ Decorator for (optionally) printing performance statistics """ 20 log = logging.getLogger("omero.perf") 21 if not log.isEnabledFor(logging.DEBUG): 22 return func 23 def handler(*args, **kwargs): 24 try: 25 self = args[0] 26 mod = self.__class__.__module__ 27 cls = self.__class__.__name__ 28 tag = "%s.%s.%s" % (mod, cls, func.func_name) 29 except: 30 tag = func.func_name 31 start = time.time() 32 try: 33 rv = func(*args, **kwargs) 34 return rv 35 finally: 36 stop = time.time() 37 diff = stop - start 38 startMillis = int(start * 1000) 39 timeMillis = int(diff * 1000) 40 log.debug("start[%d] time[%d] tag[%s]", startMillis, timeMillis, tag)
41 handler = wraps(func)(handler) 42 return handler 43 44
45 -def remoted(func):
46 """ Decorator for catching any uncaught exception and converting it to an InternalException """ 47 log = logging.getLogger("omero.remote") 48 def exc_handler(*args, **kwargs): 49 try: 50 rv = func(*args, **kwargs) 51 #log.info("%s(%s,%s)=>%s" % (func, args, kwargs, rv)) 52 return rv 53 except exceptions.Exception, e: 54 if isinstance(e, omero.ServerError): 55 raise 56 else: 57 log.warn("%s raised a non-ServerError (%s): %s", func, type(e), e) 58 msg = traceback.format_exc() 59 raise omero.InternalException(msg, None, "Internal exception")
60 exc_handler = wraps(func)(exc_handler) 61 return exc_handler 62
63 -def locked(func):
64 """ Decorator for using the self._lock argument of the calling instance """ 65 def with_lock(*args, **kwargs): 66 self = args[0] 67 self._lock.acquire() 68 try: 69 return func(*args, **kwargs) 70 finally: 71 self._lock.release()
72 with_lock = wraps(func)(with_lock) 73 return with_lock 74