/usr/share/pyshared/Wammu/PhoneValidator.py is in wammu 0.36-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | # -*- coding: UTF-8 -*-
# vim: expandtab sw=4 ts=4 sts=4:
'''
Wammu - Phone manager
Phone number validator.
'''
__author__ = 'Michal Čihař'
__email__ = 'michal@cihar.com'
__license__ = '''
Copyright © 2003 - 2010 Michal Čihař
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
'''
import wx
import re
MATCHER_NORMAL = re.compile('^[0-9*#+]+$')
MATCHER_PAUSE = re.compile('^[Pp0-9*#+]+$')
MATCH_SPLIT = re.compile('[\s;,]+')
def SplitNumbers(text):
'''
Splits text to list of phone numbers.
'''
lst = MATCH_SPLIT.split(text)
if lst[0] == '':
del lst[0]
if len(lst) > 0 and lst[len(lst) - 1] == '':
del lst[len(lst) - 1]
return lst
class PhoneValidator(wx.PyValidator):
'''
Validator for phone numbers.
'''
def __init__(self, multi=False, pause=False, empty=False):
wx.PyValidator.__init__(self)
self.multi = multi
self.pause = pause
self.empty = empty
self.Bind(wx.EVT_CHAR, self.OnChar)
def Clone(self):
return PhoneValidator(self.multi, self.pause, self.empty)
def TransferToWindow(self):
return True
def TransferFromWindow(self):
return True
def CheckText(self, text, immediate = False):
'''
Verifies whether enterd text is correct.
'''
if self.multi:
values = SplitNumbers(text)
else:
values = [text]
for val in values:
if val == '' and not self.empty:
if immediate:
continue
else:
return False
elif self.pause and MATCHER_PAUSE.match(val) == None:
return False
elif not self.pause and MATCHER_NORMAL.match(val) == None:
return False
elif immediate and val == '+':
continue
return True
def Validate(self, win = None):
textcontrol = self.GetWindow()
val = textcontrol.GetValue()
result = self.CheckText(val)
if not result and win != None:
wx.MessageDialog(win,
_('You did not specify valid phone number.'),
_('Invalid phone number'),
wx.OK | wx.ICON_WARNING).ShowModal()
textcontrol.SetFocus()
return result
def OnChar(self, event):
key = event.GetKeyCode()
# control chars
if (key < wx.WXK_SPACE or
key == wx.WXK_DELETE or
key > 255 or
event.AltDown() or
event.CmdDown() or
event.ControlDown() or
event.MetaDown()):
event.Skip()
return
try:
char = chr(key)
textcontrol = self.GetWindow()
pos = textcontrol.GetInsertionPoint()
val = textcontrol.GetValue()
newval = val[0:pos] + char + val[pos:len(val)]
if self.CheckText(newval, immediate = True):
event.Skip()
return
except UnicodeDecodeError:
pass
# should we bell?
if not wx.Validator_IsSilent():
wx.Bell()
# Returning without calling even.Skip eats the event before it
# gets to the text control
return
|