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

Source Code for Module smartcard.pcsc.PCSCContext

 1  """PCSC context singleton. 
 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   
25  from threading import RLock 
26   
27  from smartcard.scard import * 
28  from smartcard.pcsc.PCSCExceptions import EstablishContextException 
29   
30   
31 -class PCSCContext(object):
32 """Manage a singleton pcsc context handle.""" 33
35 """The actual pcsc context class as a singleton.""" 36
37 - def __init__(self):
38 hresult, self.hcontext = SCardEstablishContext(SCARD_SCOPE_USER) 39 if hresult != 0: 40 raise EstablishContextException(hresult)
41
42 - def getContext(self):
43 return self.hcontext
44 45 # the singleton 46 mutex = RLock() 47 instance = None 48
49 - def __init__(self):
50 PCSCContext.mutex.acquire() 51 try: 52 if not PCSCContext.instance: 53 PCSCContext.instance = PCSCContext.__PCSCContextSingleton() 54 finally: 55 PCSCContext.mutex.release()
56
57 - def __getattr__(self, name):
58 if self.instance: 59 return getattr(self.instance, name)
60