def dep_wrapper(*args, **kwargs): from porcupine.core.runtime import logger upper_stack = traceback.extract_stack()[-2] pfile, line_no, where, line = upper_stack logger.warning("DEPRECATION WARNING\n" + "File \"%s\".\n" % pfile + "Line %d:\n %s\nin \"%s\". " % (line_no, line, where) + "Use \"%s\" instead." % (member or compat.get_func_name(function))) return function(*args, **kwargs)
def wrap(f): def new_function(*args, **kw): lock.acquire() try: return f(*args, **kw) finally: lock.release() compat.set_func_name(new_function, compat.get_func_name(f)) return new_function
def dep_wrapper(*args, **kwargs): from porcupine.core.runtime import logger upper_stack = traceback.extract_stack()[-2] pfile, line_no, where, line = upper_stack logger.warning( "DEPRECATION WARNING\n" + "File \"%s\".\n" % pfile + "Line %d:\n %s\nin \"%s\". " % (line_no, line, where) + "Use \"%s\" instead." % (member or compat.get_func_name(function))) return function(*args, **kwargs)
def __init__(self, function, of_type, conditions, content_type, encoding, max_age, template, template_engine): self.func = function self.conditions = conditions self.func_name = "WM_%s#%s" % (compat.get_func_name(function), utils.misc.hash(*self.conditions).hexdigest()) # response parameters self.content_type = content_type self.encoding = encoding self.max_age = max_age # template parameters self.template = template self.t_engine = template_engine # monkey patching self.of_type = of_type setattr(of_type, self.func_name, self)
def requires_transactional_context(function): """ Use this descriptor to ensure that a function or method is run in a transactional context. Required for functions/methods that perform database updates. """ def rtc_wrapper(*args, **kwargs): if not context._is_txnal: raise exceptions.InternalServerError( "Not in a transactional context. Use @db.transactional().") return function(*args, **kwargs) compat.set_func_name(rtc_wrapper, compat.get_func_name(function)) compat.set_func_doc(rtc_wrapper, compat.get_func_doc(function)) rtc_wrapper.__module__ = function.__module__ return rtc_wrapper
def __init__(self, function, of_type, conditions, content_type, encoding, max_age, template, template_engine): self.func = function self.conditions = conditions self.func_name = 'WM_%s#%s' % (compat.get_func_name(function), utils.misc.hash( *self.conditions).hexdigest()) # response parameters self.content_type = content_type self.encoding = encoding self.max_age = max_age #template parameters self.template = template self.t_engine = template_engine # monkey patching self.of_type = of_type setattr(of_type, self.func_name, self)
def deprecated(function, member=None): """ Wrapper for deprecated API calls """ def dep_wrapper(*args, **kwargs): from porcupine.core.runtime import logger upper_stack = traceback.extract_stack()[-2] pfile, line_no, where, line = upper_stack logger.warning( "DEPRECATION WARNING\n" + 'File "%s".\n' % pfile + 'Line %d:\n %s\nin "%s". ' % (line_no, line, where) + 'Use "%s" instead.' % (member or compat.get_func_name(function)) ) return function(*args, **kwargs) compat.set_func_name(dep_wrapper, compat.get_func_name(function)) return dep_wrapper
def transactional_decorator(function): """ This is the descriptor for making a function or a Web method transactional. """ def transactional_wrapper(*args): # check if running in a replicated site and is master rep_mgr = _db.get_replication_manager() if rep_mgr is not None and not rep_mgr.is_master(): # TODO: abort txn in chained calls? raise exceptions.DBReadOnly( 'Attempted write operation in read-only database.') has_txn = False txn_flags = 0 if not context._is_txnal: if context._trans is not None: # abort previous txn context._trans.abort() has_txn = True txn_flags = context._trans._flags # create new txn handle txn = _db.get_transaction(nosync=nosync) context._trans = txn is_top_level = True context._is_txnal = True else: txn = context._trans is_top_level = False retries = 0 sleep_time = _min_sleep_time try: while retries < txn.txn_max_retries: try: if is_top_level: cargs = copy.deepcopy(args, {'_dup_ext_': False}) if retries > 0: time.sleep(sleep_time) sleep_time *= 2 if sleep_time > _max_sleep_time: sleep_time = _max_sleep_time + \ (retries * _min_sleep_time) txn._retry() else: cargs = args val = function(*cargs) if is_top_level and auto_commit: txn.commit() return val except exceptions.DBRetryTransaction: if is_top_level: retries += 1 else: # allow propagation raise except: txn.abort() raise # maximum retries exceeded raise exceptions.DBDeadlockError finally: if is_top_level: if has_txn: context._trans = _db.get_transaction(flags=txn_flags) else: context._trans = None context._is_txnal = False compat.set_func_name(transactional_wrapper, compat.get_func_name(function)) compat.set_func_doc(transactional_wrapper, compat.get_func_doc(function)) transactional_wrapper.__module__ = function.__module__ return transactional_wrapper