Python3: PIP requirement
[csit.git] / resources / libraries / python / PythonThree.py
1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Library holding utility functions to be replaced by later Python builtins."""
15
16 from robot.api import logger
17
18
19 def raise_from(raising, excepted, level="WARN"):
20     """Function to be replaced by "raise from" in Python 3.
21
22     Neither "six" nor "future" offer good enough implementation right now.
23     chezsoi.org/lucas/blog/displaying-chained-exceptions-stacktraces-in-python-2
24
25     Current implementation just logs the excepted error, and raises the new one.
26     For allower log level values, see:
27     robot-framework.readthedocs.io/en/latest/autodoc/robot.api.html#log-levels
28
29     :param raising: The exception to raise.
30     :param excepted: The exception we excepted and want to log.
31     :param level: Robot logger logging level to log with.
32     :type raising: BaseException
33     :type excepted: BaseException
34     :type level: str
35     :raises: raising
36     """
37     logger.write("Excepted: {exc!r}\nRaising: {rai!r}".format(
38         exc=excepted, rai=raising), level)
39     raise raising