1#
2#  This file is part of Bakefile (http://www.bakefile.org)
3#
4#  Copyright (C) 2003,2004 Vaclav Slavik
5#
6#  Permission is hereby granted, free of charge, to any person obtaining a copy
7#  of this software and associated documentation files (the "Software"), to
8#  deal in the Software without restriction, including without limitation the
9#  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10#  sell copies of the Software, and to permit persons to whom the Software is
11#  furnished to do so, subject to the following conditions:
12#
13#  The above copyright notice and this permission notice shall be included in
14#  all copies or substantial portions of the Software.
15#
16#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22#  IN THE SOFTWARE.
23#
24#  $Id: errors.py 1108 2007-11-18 17:25:50Z vaclavslavik $
25#
26#  Exceptions classes and errors handling code
27#
28
29import copy
30import xmlparser
31
32_readerContext = []
33
34def pushCtx(desc):
35    if isinstance(desc, xmlparser.Element):
36        _readerContext.append('at %s' % desc.location())
37    else:
38        _readerContext.append(desc)
39
40def popCtx():
41    _readerContext.pop()
42
43def getCtx():
44    return copy.deepcopy(_readerContext)
45
46def getCtxLocationStr(context=None):
47    if context == None:
48        context = getCtx()
49    s = ''
50    for ctx in range(len(context)-1,-1,-1):
51        s += "    %s\n" % context[ctx]
52    return s
53
54class ErrorBase(Exception):
55    def __init__(self, desc, context=None):
56        if context == None:
57            context = _readerContext
58        self.desc = desc
59        self.context = copy.deepcopy(context)
60    def getErrorMessage(self):
61        return self.desc
62    def __str__(self):
63        return getCtxLocationStr(self.context)
64
65class Error(ErrorBase):
66    def __init__(self, desc, context=None):
67        ErrorBase.__init__(self, desc, context)
68
69        # include additional debugging information if debugging bakefile itself:
70        from config import debug
71        if debug:
72            import traceback
73            self.debuginfo = "\nBacktrace:\n"
74            for i in traceback.format_stack()[-2::-1]:
75                self.debuginfo += i
76            try:
77                exc = traceback.format_exc()
78                if exc != None:
79                    self.debuginfo += "\nException:"
80                    for i in exc.split('\n'):
81                        self.debuginfo += "\n  " + i
82            except ImportError:
83                pass # it's only available in Python 2.4+
84        else:
85            self.debuginfo = None
86
87    def __str__(self):
88        r = 'error: %s\n%s' % (self.desc, ErrorBase.__str__(self))
89        if self.debuginfo != None:
90            r += self.debuginfo
91        return r
92
93class ReaderError(ErrorBase):
94    def __init__(self, el, desc, context=None):
95        ErrorBase.__init__(self, desc, context)
96        self.element = el
97    def __str__(self):
98        s = ''
99        if self.element != None:
100            loc = self.element.location()
101            s += "%s: error: %s\n" % (loc, self.desc)
102        else:
103            s += "error: %s\n" % self.desc
104        s += ErrorBase.__str__(self)
105        return s
106