vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / utils / exceptions.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """
17 Utilities for extracting and formatting Python exceptions.
18 """
19
20 import sys
21 import linecache
22 import StringIO
23 import traceback as tb
24
25 import jsonpickle
26
27 from .console import (puts, indent, Colored)
28
29
30 ENTRY_FORMAT = 'File "{filename}", line {lineno}, in {name}'
31
32
33 def print_exception(e, full=True, cause=False, traceback=None):
34     """
35     Prints the exception with nice colors and such.
36     """
37     def format_heading(e):
38         return '{0}{1}: {2}'.format(
39             Colored.red('Caused by ') if cause else '',
40             Colored.red(e.__class__.__name__, bold=True),
41             Colored.red(e))
42
43     puts(format_heading(e))
44     if full:
45         if cause:
46             if traceback:
47                 print_traceback(traceback, True)
48         else:
49             print_traceback()
50     if hasattr(e, 'cause') and e.cause:
51         traceback = e.cause_traceback if hasattr(e, 'cause_traceback') else None
52         print_exception(e.cause, full=full, cause=True, traceback=traceback)
53
54
55 def print_traceback(traceback=None, print_last_stack=False):
56     """
57     Prints the traceback with nice colors and such.
58     """
59
60     if traceback is None:
61         _, _, traceback = sys.exc_info()
62     while traceback is not None:
63         frame = traceback.tb_frame
64         code = frame.f_code
65         filename = code.co_filename
66         lineno = traceback.tb_lineno
67         name = code.co_name
68         with indent(2):
69             puts(ENTRY_FORMAT.format(filename=Colored.blue(filename),
70                                      lineno=Colored.cyan(lineno),
71                                      name=Colored.cyan(name)))
72             linecache.checkcache(filename)
73             line = linecache.getline(filename, lineno, frame.f_globals)
74             if line:
75                 with indent(2):
76                     puts(line.strip())
77         traceback = traceback.tb_next
78         if print_last_stack and (traceback is None):
79             # Print stack of *last* traceback
80             _print_stack(frame)
81
82
83 def _print_stack(frame):
84     entries = tb.extract_stack(frame)
85     if not entries:
86         return
87     puts(Colored.red('Call stack:'))
88     with indent(2):
89         for filename, lineno, name, line in entries:
90             puts(ENTRY_FORMAT.format(filename=Colored.blue(filename),
91                                      lineno=Colored.cyan(lineno),
92                                      name=Colored.cyan(name)))
93             with indent(2):
94                 puts(line)
95
96
97 def get_exception_as_string(exc_type, exc_val, traceback):
98     s_traceback = StringIO.StringIO()
99     tb.print_exception(
100         etype=exc_type,
101         value=exc_val,
102         tb=traceback,
103         file=s_traceback)
104     return s_traceback.getvalue()
105
106
107 class _WrappedException(Exception):
108
109     def __init__(self, exception_type, exception_str):
110         super(_WrappedException, self).__init__(exception_type, exception_str)
111         self.exception_type = exception_type
112         self.exception_str = exception_str
113
114
115 def wrap_if_needed(exception):
116     try:
117         jsonpickle.loads(jsonpickle.dumps(exception))
118         return exception
119     except BaseException:
120         return _WrappedException(type(exception).__name__, str(exception))