Package smartcard :: Package pcsc :: Module PCSCExceptions
[hide private]
[frames] | no frames]

Source Code for Module smartcard.pcsc.PCSCExceptions

  1  """Smartcard module exceptions. 
  2   
  3  This module defines the exceptions raised by the smartcard.pcsc modules. 
  4   
  5  __author__ = "http://www.gemalto.com" 
  6   
  7  Copyright 2001-2012 gemalto 
  8  Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com 
  9   
 10  This file is part of pyscard. 
 11   
 12  pyscard is free software; you can redistribute it and/or modify 
 13  it under the terms of the GNU Lesser General Public License as published by 
 14  the Free Software Foundation; either version 2.1 of the License, or 
 15  (at your option) any later version. 
 16   
 17  pyscard is distributed in the hope that it will be useful, 
 18  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  GNU Lesser General Public License for more details. 
 21   
 22  You should have received a copy of the GNU Lesser General Public License 
 23  along with pyscard; if not, write to the Free Software 
 24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 25  """ 
 26   
 27  # gemalto scard library 
 28  import smartcard.scard 
 29   
 30   
31 -class BaseSCardException(Exception):
32 """Base class for scard (aka PCSC) exceptions. 33 34 scard exceptions are raised by the scard module, i.e. 35 low-level PCSC access to readers and cards. 36 37 """ 38
39 - def __init__(self, hresult):
40 """Constructor that stores the pcsc error status.""" 41 self.hresult = hresult
42
43 - def __str__(self):
44 """Returns a string representation of the exception.""" 45 return repr("scard exception: " + 46 smartcard.scard.SCardGetErrorMessage(self.hresult))
47 48
49 -class AddReaderToGroupException(BaseSCardException):
50 """Raised when scard fails to add a new reader to a PCSC reader group.""" 51
52 - def __init__(self, hresult, readername="", groupname=""):
53 BaseSCardException.__init__(self, hresult) 54 self.readername = readername 55 self.groupname = groupname
56
57 - def __str__(self):
58 return repr('Failure to add reader: ' + self.readername + 59 ' to group: ' + self.groupname + ' ' + 60 smartcard.scard.SCardGetErrorMessage(self.hresult))
61 62
63 -class EstablishContextException(BaseSCardException):
64 """Raised when scard failed to establish context with PCSC.""" 65
66 - def __str__(self):
67 """Returns a string representation of the exception.""" 68 return repr('Failure to establish context: ' + 69 smartcard.scard.SCardGetErrorMessage(self.hresult))
70 71
72 -class ListReadersException(BaseSCardException):
73 """Raised when scard failed to list readers.""" 74
75 - def __str__(self):
76 return repr('Failure to list readers: ' + 77 smartcard.scard.SCardGetErrorMessage(self.hresult))
78 79
80 -class IntroduceReaderException(BaseSCardException):
81 """Raised when scard fails to introduce a new reader to PCSC.""" 82
83 - def __init__(self, hresult, readername=""):
84 BaseSCardException.__init__(self, hresult) 85 self.readername = readername
86
87 - def __str__(self):
88 return repr('Failure to introduce a new reader: ' + self.readername 89 + ' ' + smartcard.scard.SCardGetErrorMessage(self.hresult))
90 91
92 -class ReleaseContextException(BaseSCardException):
93 """Raised when scard failed to release PCSC context.""" 94
95 - def __str__(self):
96 return repr('Failure to release context: ' + 97 smartcard.scard.SCardGetErrorMessage(self.hresult))
98 99
100 -class RemoveReaderFromGroupException(BaseSCardException):
101 """Raised when scard fails to remove a reader from a PCSC reader group.""" 102
103 - def __init__(self, hresult, readername="", groupname=""):
104 BaseSCardException.__init__(self, hresult) 105 self.readername = readername 106 self.groupname = groupname
107
108 - def __str__(self):
109 return repr('Failure to remove reader: ' + self.readername + 110 ' from group: ' + self.groupname + ' ' + 111 smartcard.scard.SCardGetErrorMessage(self.hresult))
112 113 if __name__ == "__main__": 114 try: 115 raise EstablishContextException(smartcard.scard.SCARD_E_NO_MEMORY) 116 except BaseSCardException as exc: 117 print(exc) 118