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-2012 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.reader.Reader import Reader 
25  from smartcard.System import readers 
26  from smartcard.util import toHexString 
27   
28   
29 -class Card(object):
30 """Card class.""" 31
32 - def __init__(self, reader, atr):
33 """Card constructor. 34 reader: reader in which the card is inserted 35 atr: ATR of the card""" 36 self.reader = reader 37 self.atr = atr
38
39 - def __repr__(self):
40 """Return a string representing the Card (atr and reader 41 concatenation).""" 42 return toHexString(self.atr) + ' / ' + str(self.reader)
43
44 - def __eq__(self, other):
45 """Return True if self==other (same reader and same atr). 46 Return False otherwise.""" 47 if isinstance(other, Card): 48 return (self.atr == other.atr and 49 repr(self.reader) == repr(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( 76 # 'not a valid reader: ' + str(self.reader)) 77 return None
78