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  perf_log = logging.getLogger("omero.perf") 
 19   
20 -def perf(func):
21 """ Decorator for (optionally) printing performance statistics """ 22 def handler(*args, **kwargs): 23 24 # Early Exit. Can't do this in up a level 25 # because logging hasn't been configured yet. 26 lvl = perf_log.getEffectiveLevel() 27 if lvl > logging.DEBUG: 28 return func(*args, **kwargs) 29 30 try: 31 self = args[0] 32 mod = self.__class__.__module__ 33 cls = self.__class__.__name__ 34 tag = "%s.%s.%s" % (mod, cls, func.func_name) 35 except: 36 tag = func.func_name 37 start = time.time() 38 try: 39 rv = func(*args, **kwargs) 40 return rv 41 finally: 42 stop = time.time() 43 diff = stop - start 44 startMillis = int(start * 1000) 45 timeMillis = int(diff * 1000) 46 perf_log.debug("start[%d] time[%d] tag[%s]", startMillis, timeMillis, tag)
47 handler = wraps(func)(handler) 48 return handler 49 50 51 __FORMAT = "%-.120s" 52 __RESULT = " Rslt: " + __FORMAT 53 __EXCEPT = " Excp: " + __FORMAT
54 -def remoted(func):
55 """ Decorator for catching any uncaught exception and converting it to an InternalException """ 56 log = logging.getLogger("omero.remote") 57 def exc_handler(*args, **kwargs): 58 try: 59 self = args[0] 60 log.info(" Meth: %s.%s", self.__class__.__name__, func.func_name) 61 rv = func(*args, **kwargs) 62 log.info(__RESULT, rv) 63 return rv 64 except exceptions.Exception, e: 65 log.info(__EXCEPT, e) 66 if isinstance(e, omero.ServerError): 67 raise 68 else: 69 log.warn("%s raised a non-ServerError (%s): %s", func, type(e), e) 70 msg = traceback.format_exc() 71 raise omero.InternalException(msg, None, "Internal exception")
72 exc_handler = wraps(func)(exc_handler) 73 return exc_handler 74
75 -def locked(func):
76 """ Decorator for using the self._lock argument of the calling instance """ 77 def with_lock(*args, **kwargs): 78 self = args[0] 79 self._lock.acquire() 80 try: 81 return func(*args, **kwargs) 82 finally: 83 self._lock.release()
84 with_lock = wraps(func)(with_lock) 85 return with_lock 86 87
88 -class TimeIt (object):
89 """ 90 Decorator to measure the execution time of a function. Assumes that a C{logger} global var 91 is available and is the logger instance from C{logging.getLogger()}. 92 93 @param level: the level to use for logging 94 """ 95 logger = logging.getLogger('omero.timeit') 96
97 - def __init__ (self, level=logging.DEBUG):
98 self._level = level
99
100 - def __call__ (self, func):
101 def wrapped (*args, **kwargs): 102 self.logger.log(self._level, "timing %s" % (func.func_name)) 103 now = time.time() 104 rv = func(*args, **kwargs) 105 self.logger.log(self._level, "timed %s: %f" % (func.func_name, time.time()-now)) 106 return rv
107 return wrapped
108
109 -def timeit (func):
110 """ 111 Shortcut version of the L{TimeIt} decorator class. 112 Logs at logging.DEBUG level. 113 """ 114 def wrapped (*args, **kwargs): 115 TimeIt.logger.log(self._level, "timing %s" % (func.func_name)) 116 now = time.time() 117 rv = func(*args, **kwargs) 118 TimeIt.logger.log(self._level, "timed %s: %f" % (func.func_name, time.time()-now)) 119 return rv
120 return TimeIt()(func) 121