1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2013 LEAP
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 Set of functions to help checking situations
25 logger = logging.getLogger(__name__)
28 def leap_assert(condition, message=""):
30 Asserts the condition and displays the message if that's not
31 met. It also logs the error and its backtrace.
33 :param condition: condition to check
35 :param message: message to display if the condition isn't met
39 logger.error("Bug: %s" % (message,))
41 for formatted_line in traceback.format_list(
42 traceback.extract_stack()[:-1]):
43 for line in formatted_line.split("\n"):
44 if len(line.strip()) > 0:
46 except Exception as e:
47 logger.error("Bug in leap_assert: %r" % (e,))
48 assert condition, message
51 def leap_assert_type(var, expectedType):
53 Helper assert check for a variable's expected type
55 :param var: variable to check
57 :param expectedType: type to check agains
58 :type expectedType: type
60 leap_assert(isinstance(var, expectedType),
61 "Expected type %r instead of %r" %
62 (expectedType, type(var)))
65 def leap_check(condition, message="", exception=Exception):
67 Asserts the condition and displays the message if that's not
68 met. It also logs the error and its backtrace.
70 :param condition: condition to check
72 :param message: message to display if the condition isn't met
74 :param exception: the exception to raise in case of condition being false.
75 :type exception: Exception
79 raise exception(message)