"Fossies" - the Fresh Open Source Software Archive

Member "murano-8.0.0/murano/dsl/principal_objects/stack_trace.py" (16 Oct 2019, 3654 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 "stack_trace.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 7.0.0_vs_8.0.0.

    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 inspect
   16 import os.path
   17 
   18 import six
   19 from yaql import specs
   20 
   21 from murano.dsl import constants
   22 from murano.dsl import dsl
   23 from murano.dsl import dsl_types
   24 from murano.dsl import helpers
   25 from murano.dsl import yaql_integration
   26 
   27 
   28 @dsl.name('io.murano.StackTrace')
   29 class StackTrace(object):
   30     def __init__(self, this, context, include_native_frames=True):
   31         frames = []
   32         caller_context = context
   33         while True:
   34             if not caller_context:
   35                 break
   36             frame = compose_stack_frame(caller_context)
   37             frames.append(frame)
   38             caller_context = helpers.get_caller_context(caller_context)
   39         frames.reverse()
   40         frames.pop()
   41 
   42         if include_native_frames:
   43             native_frames = []
   44             for frame in inspect.trace()[1:]:
   45                 location = dsl_types.ExpressionFilePosition(
   46                     os.path.abspath(frame[1]), frame[2],
   47                     -1, frame[2], -1)
   48                 method = frame[3]
   49                 native_frames.append({
   50                     'instruction': frame[4][0].strip(),
   51                     'location': location,
   52                     'methodName': method,
   53                     'typeName': None
   54                 })
   55             frames.extend(native_frames)
   56 
   57         this.properties.frames = frames
   58 
   59     @specs.meta(constants.META_NO_TRACE, True)
   60     @specs.meta('Usage', 'Action')
   61     def to_string(self, this, prefix=''):
   62         return '\n'.join([format_frame(t, prefix)for t in this['frames']])
   63 
   64 
   65 def compose_stack_frame(context):
   66     instruction = helpers.get_current_instruction(context)
   67     method = helpers.get_current_method(context)
   68     return {
   69         'instruction': None if instruction is None
   70         else six.text_type(instruction),
   71 
   72         'location': None if instruction is None
   73         else instruction.source_file_position,
   74 
   75         'methodName': None if method is None else method.name,
   76         'typeName': None if method is None else method.declaring_type.name
   77     }
   78 
   79 
   80 def format_frame(frame, prefix=''):
   81     instruction = frame['instruction']
   82     method_name = frame['methodName']
   83     type_name = frame['typeName']
   84     location = frame['location']
   85     if type_name:
   86         method_name += ' of type ' + type_name
   87 
   88     if location:
   89         args = (
   90             os.path.abspath(location.file_path),
   91             location.start_line,
   92             ':' + str(location.start_column)
   93             if location.start_column >= 0 else '',
   94             method_name,
   95             instruction,
   96             prefix
   97         )
   98         return (u'{5}File "{0}", line {1}{2} in method {3}\n'
   99                 u'{5}    {4}').format(*args)
  100     else:
  101         return u'{2}File <unknown> in method {0}\n{2}    {1}'.format(
  102             method_name, instruction, prefix)
  103 
  104 
  105 def create_stack_trace(context, include_native_frames=True):
  106     stacktrace = yaql_integration.call_func(
  107         context, 'new', 'io.murano.StackTrace',
  108         includeNativeFrames=include_native_frames)
  109     return dsl.MuranoObjectInterface.create(stacktrace)