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