2008-06-02 01:33:51 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 3 only.
|
|
|
|
##
|
|
|
|
## Gajim 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 General Public License for more details.
|
|
|
|
##
|
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
|
|
|
|
|
|
|
'''
|
|
|
|
Helper code related to plug-ins management system.
|
|
|
|
|
|
|
|
:author: Mateusz Biliński <mateusz@bilinski.it>
|
2008-07-18 09:05:07 +02:00
|
|
|
:since: 30th May 2008
|
2008-06-02 01:33:51 +02:00
|
|
|
:copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
|
|
|
|
:license: GPL
|
|
|
|
'''
|
|
|
|
|
|
|
|
__all__ = ['log', 'log_calls', 'Singleton']
|
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger('gajim.plugin_system')
|
|
|
|
'''
|
|
|
|
Logger for code related to plug-in system.
|
|
|
|
|
|
|
|
:type: logging.Logger
|
|
|
|
'''
|
|
|
|
|
|
|
|
consoleloghandler = logging.StreamHandler()
|
2008-06-08 18:36:46 +02:00
|
|
|
#consoleloghandler.setLevel(1)
|
2008-06-02 01:33:51 +02:00
|
|
|
consoleloghandler.setFormatter(
|
|
|
|
logging.Formatter('%(levelname)s: %(message)s'))
|
|
|
|
#logging.Formatter('%(asctime)s %(name)s: %(levelname)s: %(message)s'))
|
2008-06-08 18:36:46 +02:00
|
|
|
#log.setLevel(logging.DEBUG)
|
2008-06-02 01:33:51 +02:00
|
|
|
log.addHandler(consoleloghandler)
|
|
|
|
log.propagate = False
|
|
|
|
|
|
|
|
import functools
|
|
|
|
|
|
|
|
class log_calls(object):
|
|
|
|
'''
|
|
|
|
Decorator class for functions to easily log when they are entered and left.
|
|
|
|
'''
|
|
|
|
|
2008-07-29 21:09:28 +02:00
|
|
|
filter_out_classes = ['PluginManager']
|
|
|
|
'''
|
|
|
|
List of classes from which no logs should be emited when methods are called,
|
|
|
|
eventhough `log_calls` decorator is used.
|
|
|
|
'''
|
|
|
|
|
2008-06-02 01:33:51 +02:00
|
|
|
def __init__(self, classname='', log=log):
|
|
|
|
'''
|
|
|
|
:Keywords:
|
|
|
|
classname : str
|
|
|
|
Name of class to prefix function name (if function is a method).
|
|
|
|
log : logging.Logger
|
|
|
|
Logger to use when outputing debug information on when function has
|
|
|
|
been entered and when left. By default: `plugins.helpers.log`
|
|
|
|
is used.
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.full_func_name = ''
|
|
|
|
'''
|
|
|
|
Full name of function, with class name (as prefix) if given
|
|
|
|
to decorator.
|
|
|
|
|
|
|
|
Otherwise, it's only function name retrieved from function object
|
|
|
|
for which decorator was called.
|
|
|
|
|
|
|
|
:type: str
|
|
|
|
'''
|
2008-07-29 21:09:28 +02:00
|
|
|
self.log_this_class = True
|
|
|
|
'''
|
|
|
|
Determines whether wrapper of given function should log calls of this
|
|
|
|
function or not.
|
|
|
|
|
|
|
|
:type: bool
|
|
|
|
'''
|
2008-06-02 01:33:51 +02:00
|
|
|
|
|
|
|
if classname:
|
|
|
|
self.full_func_name = classname+'.'
|
2008-07-29 21:09:28 +02:00
|
|
|
|
|
|
|
if classname in self.filter_out_classes:
|
|
|
|
self.log_this_class = False
|
2008-06-02 01:33:51 +02:00
|
|
|
|
|
|
|
def __call__(self, f):
|
|
|
|
'''
|
|
|
|
:param f: function to be wrapped with logging statements
|
|
|
|
|
|
|
|
:return: given function wrapped by *log.debug* statements
|
|
|
|
:rtype: function
|
|
|
|
'''
|
2008-07-29 21:09:28 +02:00
|
|
|
|
2008-06-02 01:33:51 +02:00
|
|
|
self.full_func_name += f.func_name
|
2008-07-29 21:09:28 +02:00
|
|
|
if self.log_this_class:
|
|
|
|
@functools.wraps(f)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
|
|
|
log.debug('%(funcname)s() <entered>'%{
|
|
|
|
'funcname': self.full_func_name})
|
|
|
|
result = f(*args, **kwargs)
|
|
|
|
log.debug('%(funcname)s() <left>'%{
|
|
|
|
'funcname': self.full_func_name})
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
@functools.wraps(f)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
result = f(*args, **kwargs)
|
|
|
|
return result
|
|
|
|
|
2008-06-02 01:33:51 +02:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
class Singleton(type):
|
|
|
|
'''
|
|
|
|
Singleton metaclass.
|
|
|
|
'''
|
|
|
|
def __init__(cls,name,bases,dic):
|
|
|
|
super(Singleton,cls).__init__(name,bases,dic)
|
|
|
|
cls.instance=None
|
|
|
|
|
|
|
|
def __call__(cls,*args,**kw):
|
|
|
|
if cls.instance is None:
|
|
|
|
cls.instance=super(Singleton,cls).__call__(*args,**kw)
|
|
|
|
log.debug('%(classname)s - new instance created'%{
|
|
|
|
'classname' : cls.__name__})
|
|
|
|
else:
|
|
|
|
log.debug('%(classname)s - returning already existing instance'%{
|
|
|
|
'classname' : cls.__name__})
|
|
|
|
|
|
|
|
return cls.instance
|