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