def perf(func): """ Decorator for (optionally) printing performance statistics """ def handler(*args, **kwargs): # Early Exit. Can't do this in up a level # because logging hasn't been configured yet. lvl = perf_log.getEffectiveLevel() if lvl > logging.DEBUG: return func(*args, **kwargs) try: self = args[0] mod = self.__class__.__module__ cls = self.__class__.__name__ tag = "%s.%s.%s" % (mod, cls, func.func_name) except: tag = func.func_name start = time.time() try: rv = func(*args, **kwargs) return rv finally: stop = time.time() diff = stop - start startMillis = int(start * 1000) timeMillis = int(diff * 1000) perf_log.debug("start[%d] time[%d] tag[%s]", startMillis, timeMillis, tag) handler = wraps(func)(handler) return handler
def stamped(func, update = False): """ Decorator which takes the first argument after "self" and compares that to the last modification time. If the stamp is older, then the method call will throw an omero.OptimisticLockException. Otherwise, execution will complete normally. If update is True, then the last modification time will be updated after the method call if it is successful. Note: stamped implies locked """ def check_and_update_stamp(*args, **kwargs): self = args[0] stamp = args[1] if stamp < self._stamp: raise omero.OptimisticLockException(None, None, "Resource modified by another thread") try: return func(*args, **kwargs) finally: if update: self._stamp = time.time() checked_and_update_stamp = wraps(func)(check_and_update_stamp) return locked(check_and_update_stamp)
def stamped(func, update=False): """ Decorator which takes the first argument after "self" and compares that to the last modification time. If the stamp is older, then the method call will throw an omero.OptimisticLockException. Otherwise, execution will complete normally. If update is True, then the last modification time will be updated after the method call if it is successful. Note: stamped implies locked """ def check_and_update_stamp(*args, **kwargs): self = args[0] stamp = args[1] if stamp < self._stamp: raise omero.OptimisticLockException( None, None, "Resource modified by another thread") try: return func(*args, **kwargs) finally: if update: self._stamp = time.time() check_and_update_stamp = wraps(func)(check_and_update_stamp) return locked(check_and_update_stamp)
def with_process(func, Popen = MockPopen): """ Decorator for running a test with a Process """ def handler(*args, **kwargs): self = args[0] self.process = omero.processor.ProcessI(self.ctx, sys.executable, self.props(), self.params(), Popen = Popen, callback_cast = pass_through) try: rv = func(*args, **kwargs) finally: self.process.cleanup() return wraps(func)(handler)
def locked(func): """ Decorator for using the self._lock argument of the calling instance """ def with_lock(*args, **kwargs): self = args[0] self._lock.acquire() try: return func(*args, **kwargs) finally: self._lock.release() with_lock = wraps(func)(with_lock) return with_lock
def modifies(func): """ Decorator which always calls flush() on the first argument after the method call """ def flush_after(*args, **kwargs): self = args[0] try: return func(*args, **kwargs) finally: self.flush() return wraps(func)(flush_after)
def with_process(func, Popen=MockPopen): """ Decorator for running a test with a Process """ def handler(*args, **kwargs): self = args[0] self.process = omero.processor.ProcessI(self.ctx, sys.executable, self.props(), self.params(), Popen=Popen, callback_cast=pass_through) try: rv = func(*args, **kwargs) finally: self.process.cleanup() return wraps(func)(handler)
self = args[0] log.info(" Meth: %s.%s", self.__class__.__name__, func.func_name) rv = func(*args, **kwargs) log.info(__RESULT, rv) return rv except exceptions.Exception, e: log.info(__EXCEPT, e) if isinstance(e, omero.ServerError): raise else: log.warn("%s raised a non-ServerError (%s): %s", func, type(e), e) msg = traceback.format_exc() raise omero.InternalException(msg, None, "Internal exception") exc_handler = wraps(func)(exc_handler) return exc_handler def locked(func): """ Decorator for using the self._lock argument of the calling instance """ def with_lock(*args, **kwargs): self = args[0] self._lock.acquire() try: return func(*args, **kwargs) finally: self._lock.release() with_lock = wraps(func)(with_lock) return with_lock
def exc_handler(*args, **kwargs): try: self = args[0] log.info(" Meth: %s.%s", self.__class__.__name__, func.func_name) rv = func(*args, **kwargs) log.info(__RESULT, rv) return rv except Exception, e: log.info(__EXCEPT, e) if isinstance(e, omero.ServerError): raise else: log.warn("%s raised a non-ServerError (%s): %s", func, type(e), e) msg = traceback.format_exc() raise omero.InternalException(msg, None, "Internal exception") exc_handler = wraps(func)(exc_handler) return exc_handler def locked(func): """ Decorator for using the self._lock argument of the calling instance """ def with_lock(*args, **kwargs): self = args[0] self._lock.acquire() try: return func(*args, **kwargs) finally: self._lock.release() with_lock = wraps(func)(with_lock) return with_lock