1 | # $Id: ErrorCatchers.py,v 1.7 2005/01/03 19:59:07 tavis_rudd Exp $ |
---|
2 | """ErrorCatcher class for Cheetah Templates |
---|
3 | |
---|
4 | Meta-Data |
---|
5 | ================================================================================ |
---|
6 | Author: Tavis Rudd <tavis@damnsimple.com> |
---|
7 | Version: $Revision: 1.7 $ |
---|
8 | Start Date: 2001/08/01 |
---|
9 | Last Revision Date: $Date: 2005/01/03 19:59:07 $ |
---|
10 | """ |
---|
11 | __author__ = "Tavis Rudd <tavis@damnsimple.com>" |
---|
12 | __revision__ = "$Revision: 1.7 $"[11:-2] |
---|
13 | |
---|
14 | import time |
---|
15 | from Cheetah.NameMapper import NotFound |
---|
16 | |
---|
17 | class Error(Exception): |
---|
18 | pass |
---|
19 | |
---|
20 | class ErrorCatcher: |
---|
21 | _exceptionsToCatch = (NotFound,) |
---|
22 | |
---|
23 | def __init__(self, templateObj): |
---|
24 | pass |
---|
25 | |
---|
26 | def exceptions(self): |
---|
27 | return self._exceptionsToCatch |
---|
28 | |
---|
29 | def warn(self, exc_val, code, rawCode, lineCol): |
---|
30 | return rawCode |
---|
31 | ## make an alias |
---|
32 | Echo = ErrorCatcher |
---|
33 | |
---|
34 | class BigEcho(ErrorCatcher): |
---|
35 | def warn(self, exc_val, code, rawCode, lineCol): |
---|
36 | return "="*15 + "<" + rawCode + " could not be found>" + "="*15 |
---|
37 | |
---|
38 | class KeyError(ErrorCatcher): |
---|
39 | def warn(self, exc_val, code, rawCode, lineCol): |
---|
40 | raise KeyError("no '%s' in this Template Object's Search List" % rawCode) |
---|
41 | |
---|
42 | class ListErrors(ErrorCatcher): |
---|
43 | """Accumulate a list of errors.""" |
---|
44 | _timeFormat = "%c" |
---|
45 | |
---|
46 | def __init__(self, templateObj): |
---|
47 | ErrorCatcher.__init__(self, templateObj) |
---|
48 | self._errors = [] |
---|
49 | |
---|
50 | def warn(self, exc_val, code, rawCode, lineCol): |
---|
51 | dict = locals().copy() |
---|
52 | del dict['self'] |
---|
53 | dict['time'] = time.strftime(self._timeFormat, |
---|
54 | time.localtime(time.time())) |
---|
55 | self._errors.append(dict) |
---|
56 | return rawCode |
---|
57 | |
---|
58 | def listErrors(self): |
---|
59 | """Return the list of errors.""" |
---|
60 | return self._errors |
---|
61 | |
---|
62 | |
---|