Package smartcard :: Package wx :: Module APDUHexValidator
[hide private]
[frames] | no frames]

Source Code for Module smartcard.wx.APDUHexValidator

 1  # -*- coding: iso-8859-15 -*- 
 2  """ 
 3  A wxValidator that matches APDU in hexadecimal such as: 
 4      A4 A0 00 00 02 
 5      A4A0000002 
 6   
 7  __author__ = "http://www.gemalto.com" 
 8   
 9  Copyright 2001-2011 gemalto 
10  Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com 
11   
12  This file is part of pyscard. 
13   
14  pyscard is free software; you can redistribute it and/or modify 
15  it under the terms of the GNU Lesser General Public License as published by 
16  the Free Software Foundation; either version 2.1 of the License, or 
17  (at your option) any later version. 
18   
19  pyscard is distributed in the hope that it will be useful, 
20  but WITHOUT ANY WARRANTY; without even the implied warranty of 
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
22  GNU Lesser General Public License for more details. 
23   
24  You should have received a copy of the GNU Lesser General Public License 
25  along with pyscard; if not, write to the Free Software 
26  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
27  """ 
28  import re 
29  import string 
30  import wx 
31   
32  # a regexp to match ATRs and APDUs 
33  hexbyte = "[0-9a-fA-F]{1,2}" 
34  apduregexp = re.compile("((%s)[ ]*)*" % hexbyte) 
35   
36   
37 -class APDUHexValidator(wx.PyValidator):
38 '''A wxValidator that matches APDU in hexadecimal such as: 39 A4 A0 00 00 02 40 A4A0000002''' 41
42 - def __init__(self):
43 wx.PyValidator.__init__(self) 44 self.Bind(wx.EVT_CHAR, self.OnChar)
45
46 - def Clone(self):
47 return APDUHexValidator()
48
49 - def Validate(self, win):
50 tc = self.GetWindow() 51 val = tc.GetValue() 52 53 if not apduregexp.match(value): 54 return False 55 56 return True
57
58 - def OnChar(self, event):
59 key = event.GetKeyCode() 60 61 if wx.WXK_SPACE == key or chr(key) in string.hexdigits: 62 value = event.GetEventObject().GetValue() + chr(key) 63 if apduregexp.match(value): 64 event.Skip() 65 return 66 67 if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: 68 event.Skip() 69 return 70 71 if not wx.Validator_IsSilent(): 72 wx.Bell() 73 74 return
75