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 134
|
#!/usr/bin/python
"""
__version__ = "$Revision: 1.16 $"
__date__ = "$Date: 2004/08/08 18:31:53 $"
"""
import wx
from PythonCard import model
import os, sys
import re
import webbrowser
class ReDemo(model.Background):
def on_initialize(self, event):
self.compiled = None
self.previousSource = self.components.fldSource.text
def recompile(self):
pattern = self.components.fldPattern.text
text = self.components.fldSource.text
flags = self.getflags()
try:
self.compiled = re.compile(pattern, flags)
self.components.stcStatusMessage.text = ''
except re.error, msg:
self.compiled = None
self.components.stcStatusMessage.text = "re.error: %s" % str(msg)
self.components.stcStatusMessage.backgroundColor = 'red'
except TypeError, msg:
self.components.stcStatusMessage.text = "TypeError: %s" % str(msg)
self.components.stcStatusMessage.backgroundColor = 'red'
self.reevaluate()
# the logic of the tkinter version
# eludes me
def getflags(self):
flags = 0
comp = self.components
if comp.chkIGNORECASE.checked:
flags += re.IGNORECASE
if comp.chkLOCALE.checked:
flags += re.LOCALE
if comp.chkMULTILINE.checked:
flags += re.MULTILINE
if comp.chkDOTALL.checked:
flags += re.DOTALL
if comp.chkVERBOSE.checked:
flags += re.VERBOSE
return flags
def reevaluate(self):
text = self.components.fldSource.text
# effectively clear any existing matches
self.components.fldSource.SetStyle(0, len(text), wx.TextAttr("black", "white"))
if not self.compiled:
return
# first item in the radio is the display once button
# this will check it regardless of any future language
# changes made to the resource
displayJustOne = self.components.radSearchString.stringSelection == \
self.components.radSearchString.items[0]
last = 0
nmatches = 0
listGroups = self.components.listGroups
listGroups.items = []
while last <= len(text):
m = self.compiled.search(text, last)
if m is None:
break
first, last = m.span()
if last == first:
last = first+1
self.components.fldSource.SetStyle(first, last, wx.TextAttr("black", "yellow"))
if nmatches == 0:
groups = list(m.groups())
groups.insert(0, m.group())
for i in range(len(groups)):
g = "%2d: %s" % (i, `groups[i]`)
listGroups.append(g)
nmatches = nmatches + 1
if displayJustOne:
break
if nmatches == 0:
self.components.stcStatusMessage.text = '(no match)'
self.components.stcStatusMessage.backgroundColor = 'yellow'
#print "nmatches", nmatches, "\n"
def on_fldPattern_textUpdate(self, event):
self.recompile()
# textUpdate events occur on SetStyle
# so a variable is needed to know that we are in
# the midst of an update
def on_fldSource_textUpdate(self, event):
if self.previousSource != self.components.fldSource.text:
self.previousSource = self.components.fldSource.text
self.recompile()
# checkboxes
def on_mouseClick(self, event):
self.recompile()
# radiobutton
def on_radSearchString_select(self, event):
self.reevaluate()
def on_menuHelpReModule_select(self, event):
module_url = "http://docs.python.org/lib/module-re.html"
if sys.platform.startswith('win'):
fn = os.path.dirname(os.__file__)
fn = os.path.join(fn, os.pardir, "Doc", "lib", "module-re.html")
fn = os.path.normpath(fn)
if os.path.isfile(fn):
module_url = fn
del fn
webbrowser.open(module_url)
def on_menuHelpReHowTo_select(self, event):
webbrowser.open('http://py-howto.sourceforge.net/regex/regex.html')
if __name__ == '__main__':
app = model.Application(ReDemo)
app.MainLoop()
|