76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
##############################################################################
|
|
#
|
|
# OpenERP, Open Source Management Solution
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
|
# Copyright (C) 2010 OpenERP s.a. (<http://openerp.com>).
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
##############################################################################
|
|
|
|
__all__ = ['synchronized']
|
|
|
|
from functools import wraps
|
|
from inspect import getsourcefile
|
|
|
|
def synchronized(lock_attr='_lock'):
|
|
def decorator(func):
|
|
@wraps(func)
|
|
def wrapper(self, *args, **kwargs):
|
|
lock = getattr(self, lock_attr)
|
|
try:
|
|
lock.acquire()
|
|
return func(self, *args, **kwargs)
|
|
finally:
|
|
lock.release()
|
|
return wrapper
|
|
return decorator
|
|
|
|
def frame_codeinfo(fframe, back=0):
|
|
""" Return a (filename, line) pair for a previous frame .
|
|
@return (filename, lineno) where lineno is either int or string==''
|
|
"""
|
|
|
|
try:
|
|
if not fframe:
|
|
return "<unknown>", ''
|
|
for i in range(back):
|
|
fframe = fframe.f_back
|
|
try:
|
|
fname = getsourcefile(fframe)
|
|
except TypeError:
|
|
fname = '<builtin>'
|
|
lineno = fframe.f_lineno or ''
|
|
return fname, lineno
|
|
except Exception:
|
|
return "<unknown>", ''
|
|
|
|
def compose(a, b):
|
|
""" Composes the callables ``a`` and ``b``. ``compose(a, b)(*args)`` is
|
|
equivalent to ``a(b(*args))``.
|
|
|
|
Can be used as a decorator by partially applying ``a``::
|
|
|
|
@partial(compose, a)
|
|
def b():
|
|
...
|
|
"""
|
|
@wraps(b)
|
|
def wrapper(*args, **kwargs):
|
|
return a(b(*args, **kwargs))
|
|
return wrapper
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|