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

Source Code for Module smartcard.CardNames

  1  """Card Names class 
  2   
  3  __author__ = "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 __future__ import print_function 
 25  from _bsddb import DBNotFoundError 
 26  from bsddb import hashopen 
 27  from os import environ 
 28  from os.path import join 
 29  from pickle import dumps, loads, HIGHEST_PROTOCOL 
 30   
 31   
 32  from smartcard.Synchronization import Synchronization, synchronize 
 33  from smartcard.util import toBytes 
 34   
 35   
36 -class __CardNames__(Synchronization):
37 """__CardNames__ inner class. 38 39 Stores card names and card types into a bsddb hash database. 40 41 The smartcard.CardNames.CardNames singleton manages the creation 42 of the unique instance of this class. 43 """ 44
45 - def __init__(self):
46 Synchronization.__init__(self) 47 carddb_dir = environ['ALLUSERSPROFILE'] 48 carddb_file = 'cardnames.bdb' 49 carddb_file = join(carddb_dir, carddb_file) 50 self.db = hashopen(carddb_file, 'w')
51
52 - def __del__(self):
53 self.db.sync() 54 self.db.close()
55
56 - def add(self, cardname, cardtype):
57 self.db[cardname] = dumps(cardtype, HIGHEST_PROTOCOL) 58 self.db.sync()
59
60 - def delete(self, cardname):
61 try: 62 del self.db[cardname] 63 except DBNotFoundError: 64 pass
65
66 - def dump(self):
67 for k, v in list(self.db.items()): 68 print(k, repr(loads(v)))
69
70 - def find(self, atr, reader=None):
71 for k, v in list(self.db.items()): 72 if loads(v).matches(atr, reader): 73 return k
74 75 synchronize(__CardNames__, "add delete dump find") 76 77
78 -class CardNames(object):
79 """The CardNames organizes cards by a unique name and an associated 80 smartcard.CardType.CardType.""" 81 82 """The single instance of __CardNames__""" 83 instance = None 84
85 - def __init__(self):
86 """Constructor: create a single instance of __readergroups on 87 first call""" 88 if None == CardNames.instance: 89 CardNames.instance = __CardNames__()
90
91 - def __getattr__(self, name):
92 """All operators redirected to inner class.""" 93 return getattr(self.instance, name)
94 95 96 if __name__ == '__main__': 97 from smartcard.CardType import ATRCardType 98 99 # define a card by its ATR 100 ct = ATRCardType([0x3B, 0x16, 0x94, 0x20, 0x02, 0x01, 0x00, 0x00, 0x0D]) 101 102 # create CardName 103 cn = CardNames() 104 cn.add("Palmera Protect V2", ct) 105 cn.dump() 106 print(cn.find([0x3B, 0x16, 0x94, 0x20, 0x02, 0x01, 0x00, 0x00, 0x0D])) 107 print(cn.find([0x3B, 0x16, 0x94, 0x20, 0x02, 0x01, 0x00, 0x00])) 108 cn.delete("Palmera Protect V2") 109 print('---------') 110 cn.dump() 111