# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2014 Smile (<http://www.smile.fr>).
#
#    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/>.
#
##############################################################################

import csv
import inspect
import os
import time

try:
    from openerp import tools  # From Odoo v6.1
except ImportError:
    import tools

from tools.func import wraps
from tools.safe_eval import safe_eval as eval

CSV_SEPARATOR = ','
KEYS = ['module', 'result', 'code', 'file', 'line', 'exception', 'duration']


def _get_filename(filepath, module):
    dirname, basename = os.path.split(filepath)
    dirname.replace(dirname.split(module)[0], '')
    return os.path.join(dirname, basename)


def logger(mapping):
    def decorator(method):
        @wraps(method)
        def wrapper(*args):
            vals = _get_vals(method, args, mapping)
            if vals.get('module'):
                try:
                    ignore_tests = eval(tools.config.get('ignored_tests')).get(vals['module']) or []
                except (NameError, SyntaxError):
                    ignore_tests = []
                if ignore_tests == 'all' or \
                        (vals.get('file') and _get_filename(vals['file'], vals['module']) in ignore_tests):
                    vals['result'] = 'ignored'
                    _write_log(vals)
                    return
            try:
                t0 = time.time()
                res = method(*args)
            except Exception, e:
                vals['duration'] = time.time() - t0
                vals['result'] = 'error'
                vals['exception'] = repr(e)
                _write_log(vals)
                raise
            else:
                vals['duration'] = time.time() - t0
                vals['result'] = 'success'
                _write_log(vals)
            return res
        return wrapper
    return decorator


def _get_args(method, arg_values):
    arg_names = inspect.getargspec(method).args
    args = {}.fromkeys(arg_names, False)
    for index, arg in enumerate(arg_names):
        if index < len(arg_values):
            args[arg] = arg_values[index]
    return args


def _get_vals(method, arg_values, mapping):
    args = _get_args(method, arg_values)
    vals = {}
    for k, v in mapping.iteritems():
        vals[k] = eval(v, args)
    return vals


def _write_log(vals):
    filename = tools.config.get('test_logfile')
    if not filename:
        return
    if not os.path.exists(filename):
        with open(filename, 'wb') as f:
            writer = csv.writer(f)
            writer.writerow(KEYS)
    with open(filename, 'ab') as f:
        writer = csv.writer(f)
        writer.writerow([vals.get(key, '') for key in KEYS])

@@ -1140,54 +1140,8 @@
 raps
-%0Afrom tools.safe_eval import safe_eval as eval
 %0A%0ACS
@@ -1583,16 +1583,54 @@
      if 
+tools.config.get('ignored_tests') and 
 vals.get
@@ -1680,32 +1680,33 @@
           ignore
+d
 _tests = eval(to
@@ -1835,24 +1835,25 @@
       ignore
+d
 _tests = %5B%5D%0A
@@ -1873,24 +1873,25 @@
    if ignore
+d
 _tests == 'a
@@ -1998,16 +1998,17 @@
 n ignore
+d
 _tests):

Correct ignored_tests management