1 """The error checking chain is a list of status word (sw1, sw2) error check strategies.
2
3 __author__ = "http://www.gemalto.com"
4
5 Copyright 2001-2011 gemalto
6 Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
7
8 This file is part of pyscard.
9
10 pyscard is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 pyscard is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with pyscard; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 """
24
25 from sys import exc_info
26
27
29 """The error checking chain is a list of response apdu status word
30 (sw1, sw2) error check strategies. Each strategy in the chain is
31 called until an error is detected. A L{smartcard.sw.SWException}
32 exception is raised when an error is detected. No exception is
33 raised if no error is detected.
34
35 Implementation derived from Bruce Eckel, Thinking in Python. The
36 L{ErrorCheckingChain} implements the Chain Of Responsibility design
37 pattern.
38 """
39
41 """constructor. Appends a strategy to the L{ErrorCheckingChain}
42 chain."""
43 self.strategy = strategy
44 self.chain = chain
45 self.chain.append(self)
46 self.excludes = []
47
49 """Returns next error checking strategy."""
50
51 location = self.chain.index(self)
52 if not self.end():
53 return self.chain[location + 1]
54
56 """Add an exception filter to the error checking chain.
57
58 @param exClass: the exception to exclude, e.g.
59 L{smartcard.sw.SWExceptions.WarningProcessingException} A filtered
60 exception will not be raised when the sw1,sw2 conditions that
61 would raise the excption are met.
62 """
63
64 self.excludes.append(exClass)
65 if self.end():
66 return
67 self.next().addFilterException(exClass)
68
70 """Returns True if this is the end of the error checking
71 strategy chain."""
72 return (self.chain.index(self) + 1 >= len(self.chain))
73
75 """Called to test data, sw1 and sw2 for error on the chain."""
76 try:
77 self.strategy(data, sw1, sw2)
78 except:
79
80 for exception in self.excludes:
81 if exception == exc_info()[0]:
82 return
83
84 raise
85
86
87 if self.end():
88 return
89 return self.next()(data, sw1, sw2)
90