parameter -nrpv not fuctioning in toscaparser
[parser.git] / tosca2heat / tosca-parser / toscaparser / common / exception.py
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13 '''
14 TOSCA exception classes
15 '''
16 import logging
17 import sys
18 import traceback
19
20 from toscaparser.utils.gettextutils import _
21
22
23 log = logging.getLogger('tosca')
24
25
26 class TOSCAException(Exception):
27     '''Base exception class for TOSCA
28
29     To correctly use this class, inherit from it and define
30     a 'msg_fmt' property.
31
32     '''
33
34     _FATAL_EXCEPTION_FORMAT_ERRORS = False
35
36     message = _('An unknown exception occurred.')
37
38     def __init__(self, **kwargs):
39         try:
40             self.message = self.msg_fmt % kwargs
41         except KeyError:
42             exc_info = sys.exc_info()
43             log.exception(_('Exception in string format operation: %s')
44                           % exc_info[1])
45
46             if TOSCAException._FATAL_EXCEPTION_FORMAT_ERRORS:
47                 raise exc_info[0]
48
49     def __str__(self):
50         return self.message
51
52     @staticmethod
53     def generate_inv_schema_property_error(self, attr, value, valid_values):
54         msg = (_('Schema definition of "%(propname)s" has '
55                  '"%(attr)s" attribute with invalid value '
56                  '"%(value1)s". The value must be one of '
57                  '"%(value2)s".') % {"propname": self.name,
58                                      "attr": attr,
59                                      "value1": value,
60                                      "value2": valid_values})
61         ExceptionCollector.appendException(
62             InvalidSchemaError(message=msg))
63
64     @staticmethod
65     def set_fatal_format_exception(flag):
66         if isinstance(flag, bool):
67             TOSCAException._FATAL_EXCEPTION_FORMAT_ERRORS = flag
68
69
70 class UnsupportedTypeError(TOSCAException):
71     msg_fmt = _('Type "%(what)s" is valid TOSCA type'
72                 ' but not supported at this time.')
73
74
75 class MissingRequiredFieldError(TOSCAException):
76     msg_fmt = _('%(what)s is missing required field "%(required)s".')
77
78
79 class UnknownFieldError(TOSCAException):
80     msg_fmt = _('%(what)s contains unknown field "%(field)s". Refer to the '
81                 'definition to verify valid values.')
82
83
84 class TypeMismatchError(TOSCAException):
85     msg_fmt = _('%(what)s must be of type "%(type)s".')
86
87
88 class InvalidNodeTypeError(TOSCAException):
89     msg_fmt = _('Node type "%(what)s" is not a valid type.')
90
91
92 class InvalidTypeError(TOSCAException):
93     msg_fmt = _('Type "%(what)s" is not a valid type.')
94
95
96 class InvalidTypeAdditionalRequirementsError(TOSCAException):
97     msg_fmt = _('Additional requirements for type "%(type)s" not met.')
98
99
100 class RangeValueError(TOSCAException):
101     msg_fmt = _('The value "%(pvalue)s" of property "%(pname)s" is out of '
102                 'range "(min:%(vmin)s, max:%(vmax)s)".')
103
104
105 class InvalidSchemaError(TOSCAException):
106     msg_fmt = _('%(message)s')
107
108
109 class ValidationError(TOSCAException):
110     msg_fmt = _('%(message)s')
111
112
113 class UnknownInputError(TOSCAException):
114     msg_fmt = _('Unknown input "%(input_name)s".')
115
116
117 class UnknownOutputError(TOSCAException):
118     msg_fmt = _('Unknown output "%(output_name)s" in %(where)s.')
119
120
121 class MissingRequiredInputError(TOSCAException):
122     msg_fmt = _('%(what)s is missing required input definition '
123                 'of input "%(input_name)s".')
124
125
126 class MissingRequiredParameterError(TOSCAException):
127     msg_fmt = _('%(what)s is missing required parameter for input '
128                 '"%(input_name)s".')
129
130
131 class MissingDefaultValueError(TOSCAException):
132     msg_fmt = _('%(what)s is missing required default value '
133                 'of input "%(input_name)s".')
134
135
136 class MissingRequiredOutputError(TOSCAException):
137     msg_fmt = _('%(what)s is missing required output definition '
138                 'of output "%(output_name)s".')
139
140
141 class InvalidPropertyValueError(TOSCAException):
142     msg_fmt = _('Value of property "%(what)s" is invalid.')
143
144
145 class InvalidTemplateVersion(TOSCAException):
146     msg_fmt = _('The template version "%(what)s" is invalid. '
147                 'Valid versions are "%(valid_versions)s".')
148
149
150 class InvalidTOSCAVersionPropertyException(TOSCAException):
151     msg_fmt = _('Value of TOSCA version property "%(what)s" is invalid.')
152
153
154 class URLException(TOSCAException):
155     msg_fmt = _('%(what)s')
156
157
158 class ToscaExtImportError(TOSCAException):
159     msg_fmt = _('Unable to import extension "%(ext_name)s". '
160                 'Check to see that it exists and has no '
161                 'language definition errors.')
162
163
164 class ToscaExtAttributeError(TOSCAException):
165     msg_fmt = _('Missing attribute in extension "%(ext_name)s". '
166                 'Check to see that it has required attributes '
167                 '"%(attrs)s" defined.')
168
169
170 class InvalidGroupTargetException(TOSCAException):
171     msg_fmt = _('"%(message)s"')
172
173
174 class ExceptionCollector(object):
175
176     exceptions = []
177     collecting = False
178
179     @staticmethod
180     def clear():
181         del ExceptionCollector.exceptions[:]
182
183     @staticmethod
184     def start():
185         ExceptionCollector.clear()
186         ExceptionCollector.collecting = True
187
188     @staticmethod
189     def stop():
190         ExceptionCollector.collecting = False
191
192     @staticmethod
193     def contains(exception):
194         for ex in ExceptionCollector.exceptions:
195             if str(ex) == str(exception):
196                 return True
197         return False
198
199     @staticmethod
200     def appendException(exception):
201         if ExceptionCollector.collecting:
202             if not ExceptionCollector.contains(exception):
203                 exception.trace = traceback.extract_stack()[:-1]
204                 ExceptionCollector.exceptions.append(exception)
205         else:
206             raise exception
207
208     @staticmethod
209     def removeException(exception_type):
210         # if ExceptionCollector.collecting and ExceptionCollector.exceptions:
211         if ExceptionCollector.exceptions:
212             tmp_exceptions = []
213             for i, e in enumerate(ExceptionCollector.exceptions):
214                 if not isinstance(e, exception_type):
215                     tmp_exceptions.append(e)
216                     # del ExceptionCollector.exceptions[i]
217             ExceptionCollector.exceptions = tmp_exceptions
218
219     @staticmethod
220     def exceptionsCaught():
221         return len(ExceptionCollector.exceptions) > 0
222
223     @staticmethod
224     def getTraceString(traceList):
225         traceString = ''
226         for entry in traceList:
227             f, l, m, c = entry[0], entry[1], entry[2], entry[3]
228             traceString += (_('\t\tFile %(file)s, line %(line)s, in '
229                               '%(method)s\n\t\t\t%(call)s\n')
230                             % {'file': f, 'line': l, 'method': m, 'call': c})
231         return traceString
232
233     @staticmethod
234     def getExceptionReportEntry(exception, full=True):
235         entry = exception.__class__.__name__ + ': ' + str(exception)
236         if full:
237             entry += '\n' + ExceptionCollector.getTraceString(exception.trace)
238         return entry
239
240     @staticmethod
241     def getExceptions():
242         return ExceptionCollector.exceptions
243
244     @staticmethod
245     def getExceptionsReport(full=True):
246         report = []
247         for exception in ExceptionCollector.exceptions:
248             report.append(
249                 ExceptionCollector.getExceptionReportEntry(exception, full))
250         return report
251
252     @staticmethod
253     def assertExceptionMessage(exception, message):
254         err_msg = exception.__name__ + ': ' + message
255         report = ExceptionCollector.getExceptionsReport(False)
256         assert err_msg in report, (_('Could not find "%(msg)s" in "%(rep)s".')
257                                    % {'rep': report.__str__(), 'msg': err_msg})