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

Source Code for Module omero.util.decorators

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # OMERO Decorators 
  5  # Copyright 2009 Glencoe Software, Inc.  All Rights Reserved. 
  6  # Use is subject to license terms supplied in LICENSE.txt 
  7  # 
  8   
  9  import time 
 10  import logging 
 11  import threading 
 12  import traceback 
 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 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 @param name: the name to use when logging, function name is used if None 95 """ 96 logger = logging.getLogger('omero.timeit') 97
98 - def __init__ (self, level=logging.DEBUG, name=None):
99 self._level = level 100 self._name = name
101
102 - def __call__ (self, func):
103 def wrapped (*args, **kwargs): 104 name = self._name or func.func_name 105 self.logger.log(self._level, "timing %s" % (name)) 106 now = time.time() 107 rv = func(*args, **kwargs) 108 self.logger.log(self._level, "timed %s: %f" % (name, time.time()-now)) 109 return rv
110 return wrapped
111
112 -def timeit (func):
113 """ 114 Shortcut version of the L{TimeIt} decorator class. 115 Logs at logging.DEBUG level. 116 """ 117 def wrapped (*args, **kwargs): 118 TimeIt.logger.log(self._level, "timing %s" % (func.func_name)) 119 now = time.time() 120 rv = func(*args, **kwargs) 121 TimeIt.logger.log(self._level, "timed %s: %f" % (func.func_name, time.time()-now)) 122 return rv
123 return TimeIt()(func) 124
125 -def setsessiongroup (func):
126 """ 127 For BlitzObjectWrapper class derivate functions, sets the session group to match 128 the object group. 129 """ 130 def wrapped (self, *args, **kwargs): 131 rev = self._conn.setGroupForSession(self.getDetails().getGroup().getId()) 132 try: 133 return func(self, *args, **kwargs) 134 finally: 135 if rev: 136 self._conn.revertGroupForSession()
137 return wrapped 138