Package smartcard :: Module Card
[hide private]
[frames] | no frames]

Source Code for Module smartcard.Card

 1  """Card class. 
 2   
 3  __author__ = "gemalto 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  from smartcard.Exceptions import CardConnectionException, NoCardException 
25  from smartcard.reader.Reader import Reader 
26  from smartcard.System import readers 
27  from smartcard.util import toHexString 
28   
29   
30 -class Card:
31 """Card class.""" 32
33 - def __init__(self, reader, atr):
34 """Card constructor. 35 reader: reader in which the card is inserted 36 atr: ATR of the card""" 37 self.reader = reader 38 self.atr = atr
39
40 - def __repr__(self):
41 """Return a string representing the Card (atr and reader 42 concatenation).""" 43 return toHexString(self.atr) + ' / ' + str(self.reader)
44
45 - def __eq__(self, other):
46 """Return True if self==other (same reader and same atr). 47 Return False otherwise.""" 48 if isinstance(other, Card): 49 return (self.atr == other.atr and `self.reader` == `other.reader`) 50 else: 51 return False
52
53 - def __ne__(self, other):
54 """Return True if self!=other (same reader and same atr).Returns 55 False otherwise.""" 56 return not self.__eq__(other)
57
58 - def __hash__(self):
59 """Returns a hash value for this object (str(self) is unique).""" 60 return hash(str(self))
61
62 - def createConnection(self):
63 """Return a CardConnection to the Card object.""" 64 readerobj = None 65 if isinstance(self.reader, Reader): 66 readerobj = self.reader 67 elif type(self.reader) == str: 68 for reader in readers(): 69 if self.reader == str(reader): 70 readerobj = reader 71 72 if readerobj: 73 return readerobj.createConnection() 74 else: 75 #raise CardConnectionException('not a valid reader: ' + str(self.reader)) 76 return None
77