Package smartcard :: Package sw :: Module ErrorCheckingChain
[hide private]
[frames] | no frames]

Source Code for Module smartcard.sw.ErrorCheckingChain

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