"Fossies" - the Fresh Open Source Software Archive

Member "murano-8.0.0/murano/dsl/virtual_exceptions.py" (16 Oct 2019, 5660 Bytes) of package /linux/misc/openstack/murano-8.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "virtual_exceptions.py" see the Fossies "Dox" file reference documentation.

    1 #    Copyright (c) 2014 Mirantis, Inc.
    2 #
    3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 #    not use this file except in compliance with the License. You may obtain
    5 #    a copy of the License at
    6 #
    7 #         http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 #    Unless required by applicable law or agreed to in writing, software
   10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 #    License for the specific language governing permissions and limitations
   13 #    under the License.
   14 
   15 import six
   16 
   17 from murano.dsl import constants
   18 from murano.dsl import dsl_exception
   19 from murano.dsl import expressions
   20 from murano.dsl import helpers
   21 from murano.dsl import macros
   22 from murano.dsl.principal_objects import stack_trace
   23 from murano.dsl import yaql_integration
   24 
   25 
   26 class ThrowMacro(expressions.DslExpression):
   27     def __init__(self, Throw, Message=None, Extra=None, Cause=None):
   28         if not Throw:
   29             raise ValueError()
   30         if not isinstance(Throw, list):
   31             Throw = [Throw]
   32 
   33         self._names = Throw
   34         self._message = Message
   35         self._extra = Extra or {}
   36         self._cause = Cause
   37 
   38     def _resolve_names(self, names, context):
   39         murano_class = helpers.get_type(context)
   40         for name in names:
   41             yield murano_class.namespace_resolver.resolve_name(name)
   42 
   43     def execute(self, context):
   44         stacktrace = stack_trace.create_stack_trace(context, False)
   45         cause = None
   46         if self._cause:
   47             cause = helpers.evaluate(self._cause, context).get_property(
   48                 'nativeException')
   49         raise dsl_exception.MuranoPlException(
   50             list(self._resolve_names(helpers.evaluate(self._names, context),
   51                                      context)),
   52             helpers.evaluate(self._message, context),
   53             stacktrace, self._extra, cause)
   54 
   55     def __unicode__(self):
   56         if self._message:
   57             return u'Throw {0}: {1}'.format(self._names, self._message)
   58         return u'Throw ' + six.text_type(self._names)
   59 
   60 
   61 class CatchBlock(expressions.DslExpression):
   62     def __init__(self, With=None, As=None, Do=None):
   63         if With is not None and not isinstance(With, list):
   64             With = [With]
   65         self._with = With
   66         self._as = As
   67         self._code_block = None if Do is None else macros.CodeBlock(Do)
   68 
   69     def _resolve_names(self, names, context):
   70         murano_class = helpers.get_type(context)
   71         for name in names:
   72             yield murano_class.namespace_resolver.resolve_name(name)
   73 
   74     def execute(self, context):
   75         exception = helpers.get_current_exception(context)
   76         names = (
   77             None if self._with is None
   78             else list(self._resolve_names(self._with, context))
   79         )
   80 
   81         for name in exception.names:
   82             if self._with is None or name in names:
   83                 if self._code_block:
   84                     if self._as:
   85                         wrapped = self._wrap_internal_exception(
   86                             exception, context, name)
   87                         context[self._as] = wrapped
   88                     self._code_block.execute(context)
   89                 return True
   90         return False
   91 
   92     def _wrap_internal_exception(self, exception, context, name):
   93         obj = yaql_integration.call_func(context, 'new', 'io.murano.Exception')
   94         obj.set_property('name', name)
   95         obj.set_property('message', exception.message)
   96         obj.set_property('stackTrace', exception.stacktrace)
   97         obj.set_property('extra', exception.extra)
   98         obj.set_property('nativeException', exception)
   99         return obj
  100 
  101 
  102 class TryBlockMacro(expressions.DslExpression):
  103     def __init__(self, Try, Catch=None, Finally=None, Else=None):
  104         self._try_block = macros.CodeBlock(Try)
  105         self._catch_block = None
  106         if Catch is not None:
  107             if not isinstance(Catch, list):
  108                 Catch = [Catch]
  109             self._catch_block = [CatchBlock(**c) for c in Catch]
  110         self._finally_block = (
  111             None if Finally is None
  112             else macros.CodeBlock(Finally))
  113         self._else_block = (
  114             None if Else is None
  115             else macros.CodeBlock(Else))
  116 
  117     def execute(self, context):
  118         try:
  119             self._try_block.execute(context)
  120         except dsl_exception.MuranoPlException as e:
  121             caught = False
  122             if self._catch_block:
  123                 try:
  124                     context[constants.CTX_CURRENT_EXCEPTION] = e
  125                     for cb in self._catch_block:
  126                         if cb.execute(context):
  127                             caught = True
  128                             break
  129                     if not caught:
  130                         raise
  131                 finally:
  132                     context[constants.CTX_CURRENT_EXCEPTION] = None
  133             else:
  134                 raise
  135         else:
  136             if self._else_block:
  137                 self._else_block.execute(context)
  138         finally:
  139             if self._finally_block:
  140                 self._finally_block.execute(context)
  141 
  142 
  143 class RethrowMacro(expressions.DslExpression):
  144     def __init__(self, Rethrow):
  145         pass
  146 
  147     def execute(self, context):
  148         exception = context['$?currentException']
  149         if not exception:
  150             raise TypeError('Rethrow must be inside Catch')
  151         raise exception
  152 
  153     def __str__(self):
  154         return 'Rethrow'
  155 
  156 
  157 def register():
  158     expressions.register_macro(ThrowMacro)
  159     expressions.register_macro(TryBlockMacro)
  160     expressions.register_macro(RethrowMacro)