krb_traceback.py :  » Language-Interface » Python-Knowledge-Engine » pyke-1.1.1 » pyke » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » Python Knowledge Engine 
Python Knowledge Engine » pyke 1.1.1 » pyke » krb_traceback.py
# $Id: krb_traceback.py 081917d30609 2010-03-05 mtnyogi $
# coding=utf-8
# 
# Copyright  2008 Bruce Frederiksen
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import linecache
import traceback
import os.path
import sys

def print_tb(traceback, limit=None, file=None):
    if file is None: file = sys.stderr
    for line in format_list(extract_tb(traceback, limit)): file.write(line)

def print_exception(type, value, traceback, limit=None, file=None):
    if file is None: file = sys.stderr
    if traceback:
        file.write('Traceback (most recent call last):\n')
        print_tb(traceback, limit, file)
    lines = format_exception_only(type, value)
    file.write(lines[0])
    for line in lines[1:]: file.write(' ' + line)

def print_exc(limit=None, file=None):
    type, value, traceback = sys.exc_info()
    print_exception(type, value, traceback, limit, file)

def format_exc(limit=None):
    type, value, traceback = sys.exc_info()
    return format_exception(type, value, traceback, limit)

def print_last(limit=None, file=None):
    print_exception(sys.last_type, sys.last_value, sys.last_traceback,
                    limit, file)

def print_stack(f=None, limit=None, file=None):
    if file is None: file = sys.stderr
    for line in format_list(extract_stack(f, limit)): file.write(line)

def extract_tb(tb, limit=None):
    ans = convert_tb(traceback.extract_tb(tb))
    if limit is not None and len(ans) > limit:
        return ans[len(ans) - limit:]
    return ans

def extract_stack(f=None, limit=None):
    ans = convert_tb(traceback.extract_stack(f))
    if limit is not None and len(ans) > limit:
        return ans[len(ans) - limit:]
    return ans

format_list = traceback.format_list

format_exception_only = traceback.format_exception_only

def format_exception(type, value, traceback, limit=None):
    ans = []
    if traceback:
        ans.append('Traceback (most recent call last):\n')
        ans.extend(format_list(extract_tb(traceback, limit)))
    lines = format_exception_only(type, value)
    ans.append(lines[0])
    for line in lines[1:]: ans.append(' ' + line)
    return ''.join(ans)

def format_tb(tb, limit=None):
    return format_list(extract_tb(tb, limit))

def format_stack(f=None, limit=None):
    return format_list(extract_stack(f, limit))

def convert_lineno(module, lineno):
    for (py_start, py_end), (krb_start, krb_end) in module.Krb_lineno_map:
        if py_start <= lineno <= py_end: return krb_start

def convert_tb(extracted_tb):
    '''
        extracted_tb is list of (filename, lineno, functionname, line)
    '''
    ans = []
    batch = []
    for info in extracted_tb:
        filename, lineno, functionname, line = info
        if filename.endswith('_fc.py') or filename.endswith('_bc.py'):
            pathname = filename[:-3]
            while True:
                module_name = pathname.replace(os.path.sep, '.')
                if module_name in sys.modules:
                    module = sys.modules[module_name]
                    if hasattr(module, 'Krb_filename'):
                        krb_lineno = convert_lineno(module, lineno)
                        if krb_lineno is not None:
                            if not ans: ans = batch
                            batch = []
                            krb_filename = \
                                os.path.normpath(
                                  os.path.join(os.path.dirname(module.__file__),
                                               module.Krb_filename))
                            linecache.checkcache(krb_filename)
                            line = linecache.getline(krb_filename, krb_lineno)
                            if line: line = line.strip()
                            else: line = None
                            ans.append((krb_filename, krb_lineno, functionname,
                                        line))
                            info = None
                        else:
                            ans.extend(batch)
                            ans.append(info)
                            batch = []
                            info = None
                    break
                sep_index = pathname.find(os.path.sep)
                if sep_index < 0: break
                pathname = pathname[sep_index + 1:]
        if info: batch.append(info)
    return ans + batch

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.