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

Source Code for Module smartcard.pcsc.PCSCReaderGroups

  1  """PCSCReaderGroups organizes smartcard readers as groups. 
  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   
 25  from __future__ import print_function 
 26  from smartcard.scard import * 
 27  from smartcard.reader.ReaderGroups import readergroups, innerreadergroups 
 28  from smartcard.pcsc.PCSCExceptions import * 
 29   
 30   
31 -class pcscinnerreadergroups(innerreadergroups):
32 """Smartcard PCSC readers groups inner class. 33 34 The PCSCReaderGroups singleton manages the creation of the unique 35 instance of this class. 36 """ 37
38 - def __init__(self, initlist=None):
39 """Constructor.""" 40 innerreadergroups.__init__(self, initlist) 41 self.unremovablegroups = ['SCard$DefaultReaders']
42
43 - def getreadergroups(self):
44 """ Returns the list of smartcard reader groups.""" 45 innerreadergroups.getreadergroups(self) 46 47 hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) 48 if hresult != 0: 49 raise EstablishContextException(hresult) 50 hresult, readers = SCardListReaderGroups(hcontext) 51 if hresult != 0: 52 raise ListReadersException(hresult) 53 hresult = SCardReleaseContext(hcontext) 54 if hresult != 0: 55 raise ReleaseContextException(hresult) 56 return readers
57
58 - def addreadergroup(self, newgroup):
59 """Add a reader group""" 60 61 hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) 62 if 0 != hresult: 63 raise error( 64 'Failed to establish context: ' + \ 65 SCardGetErrorMessage(hresult)) 66 try: 67 hresult = SCardIntroduceReaderGroup(hcontext, newgroup) 68 if 0 != hresult: 69 raise error( 70 'Unable to introduce reader group: ' + \ 71 SCardGetErrorMessage(hresult)) 72 else: 73 innerreadergroups.addreadergroup(self, newgroup) 74 75 finally: 76 hresult = SCardReleaseContext(hcontext) 77 if 0 != hresult: 78 raise error( 79 'Failed to release context: ' + \ 80 SCardGetErrorMessage(hresult))
81
82 - def removereadergroup(self, group):
83 """Remove a reader group""" 84 85 hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) 86 if 0 != hresult: 87 raise error( 88 'Failed to establish context: ' + \ 89 SCardGetErrorMessage(hresult)) 90 try: 91 hresult = SCardForgetReaderGroup(hcontext, group) 92 if hresult != 0: 93 raise error( 94 'Unable to forget reader group: ' + \ 95 SCardGetErrorMessage(hresult)) 96 else: 97 innerreadergroups.removereadergroup(self, group) 98 99 finally: 100 hresult = SCardReleaseContext(hcontext) 101 if 0 != hresult: 102 raise error( 103 'Failed to release context: ' + \ 104 SCardGetErrorMessage(hresult))
105 106
107 -class PCSCReaderGroups(readergroups):
108 """PCSC readers groups.""" 109
110 - def __init__(self, initlist=None):
111 """Create a single instance of pcscinnerreadergroups on first call""" 112 self.innerclazz = pcscinnerreadergroups 113 readergroups.__init__(self, initlist)
114 115 if __name__ == '__main__': 116 print(PCSCReaderGroups().getreadergroups()) 117