# Programmer: Daniel Pozmanter # E-mail: drpython@bluebottle.com # Note: You must reply to the verification e-mail to get through. # # Copyright 2003-2005 Daniel Pozmanter # # Distributed under the terms of the GPL (GNU Public License) # # DrPython is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #Regular Expression Dialog import re import wx import drScrolledMessageDialog import drFileDialog wildcard = "Text File (*.txt)|*.txt|All files (*)|*" class drRETextCtrl(wx.TextCtrl): def __init__(self, parent, id, value, pos, size): wx.TextCtrl.__init__(self, parent, id, value, pos, size) self.Bind(wx.EVT_CHAR, self.OnChar) def OnChar(self, event): if (event.GetKeyCode() == wx.WXK_ESCAPE): self.GetParent().OnbtnCancel(event) elif (event.GetKeyCode() == wx.WXK_RETURN): self.GetParent().OnbtnOk(event) else: event.Skip() class drRegularExpressionDialog(wx.Frame): def __init__(self, parent, id, title, prompthasfocus = 0, infiles = 0): if (wx.Platform == '__WXMSW__'): size = wx.Size(500, 160) else: size = wx.Size(500, 110) wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size, wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.THICK_FRAME | wx.RESIZE_BORDER) self.ID_OK = 1001 self.ID_CANCEL = 1002 self.ID_LOAD = 1003 self.ID_SAVE = 1004 self.ID_ANYCHARACTER = 1010 self.ID_ANYCHARACTERD = 10101 self.ID_ANYCHARACTERND = 10102 self.ID_ANYCHARACTERW = 10103 self.ID_ANYCHARACTERNW = 10104 self.ID_ANYCHARACTERA = 10105 self.ID_ANYCHARACTERNA = 10106 self.ID_SETOFCHARACTERS = 10107 self.ID_START = 1011 self.ID_END = 1012 self.ID_STARTD = 1111 self.ID_ENDD = 1112 self.ID_EDGEW = 1211 self.ID_EDGENW = 1212 self.ID_REPSZEROPLUS = 1013 self.ID_REPSONEPLUS = 1014 self.ID_REPSZEROORONE = 1015 self.ID_REPSN = 1016 self.ID_GROUP = 1017 self.ID_OR = 10171 self.ID_POSITIVE_LOOKAHEAD = 1018 self.ID_NEGATIVE_LOOKAHEAD = 1019 self.ID_POSITIVE_LOOKBEHIND = 1118 self.ID_NEGATIVE_LOOKBEHIND = 1119 self.ID_INSERT_NORMAL_TEXT = 1050 self.insert = (title == "Insert Regular Expression") self.theSizer = wx.FlexGridSizer(4, 1, 5, 10) okcancelSizer = wx.BoxSizer(wx.HORIZONTAL) self.parent = parent self.prompthasfocus = prompthasfocus if self.insert: self.defaultdirectory = self.parent.prefs.defaultdirectory self.enablefeedback = self.parent.prefs.enablefeedback self.filedialogparent = self.parent elif not infiles: self.defaultdirectory = self.parent.GetParent().prefs.defaultdirectory self.enablefeedback = self.parent.GetParent().prefs.enablefeedback self.filedialogparent = self.parent.GetParent() else: self.defaultdirectory = self.parent.GetGrandParent().prefs.defaultdirectory self.enablefeedback = self.parent.GetGrandParent().prefs.enablefeedback self.filedialogparent = self.parent.GetGrandParent() FileMenu = wx.Menu() FileMenu.Append(self.ID_LOAD, "&Load", " Load Regular Expression") FileMenu.Append(self.ID_SAVE, "&Save", " Save Regular Expression") TextMenu = wx.Menu() TextMenu.Append(self.ID_INSERT_NORMAL_TEXT, "Normal Text") TextMenu.Append(self.ID_ANYCHARACTER, "Any Character \".\"") TextMenu.Append(self.ID_ANYCHARACTERD, "Any Decimal Digit \"\\d\"") TextMenu.Append(self.ID_ANYCHARACTERND, "Any Non Digit \"\\D\"") TextMenu.Append(self.ID_ANYCHARACTERW, "Any Whitespace Character \"\\s\"") TextMenu.Append(self.ID_ANYCHARACTERNW, "Any Non Whitespace Character \"\\S\"") TextMenu.Append(self.ID_ANYCHARACTERA, "Any AlphaNumeric Character \"\\w\"") TextMenu.Append(self.ID_ANYCHARACTERNA, "Any Non AlphaNumeric Character \"\\W\"") TextMenu.Append(self.ID_SETOFCHARACTERS, "A Set of Characters \"[ ]\"") RepetitionsMenu = wx.Menu() RepetitionsMenu.Append(self.ID_REPSZEROPLUS, "0 Or More \"*\"") RepetitionsMenu.Append(self.ID_REPSONEPLUS, "1 Or More \"+\"") RepetitionsMenu.Append(self.ID_REPSZEROORONE, "0 Or 1 \"?\"") RepetitionsMenu.Append(self.ID_REPSN, "n \"{n}\"") LimitMenu = wx.Menu() LimitMenu.Append(self.ID_START, "The Start Of Each Line \"^\"") LimitMenu.Append(self.ID_END, "The End Of Each Line \"$\"") LimitMenu.Append(self.ID_STARTD, "The Start of the Document \"\\A\"") LimitMenu.Append(self.ID_ENDD, "The End of the Document \"\\Z\"") LimitMenu.Append(self.ID_EDGEW, "The Start or End of a Word \"\\b\"") LimitMenu.Append(self.ID_EDGENW, "Text That is Not at Either End of a Word \"\\B\"") lookMenu = wx.Menu() lookMenu.Append(self.ID_POSITIVE_LOOKAHEAD, "Lookahead: Positive \"(?=)\"") lookMenu.Append(self.ID_NEGATIVE_LOOKAHEAD, "Lookahead: Negative \"(?!)\"") lookMenu.Append(self.ID_POSITIVE_LOOKBEHIND, "Lookbehind: Positive \"(?<=)\"") lookMenu.Append(self.ID_NEGATIVE_LOOKBEHIND, "Lookbehind: Negative \"(? 0): if self.insert: if (self.prompthasfocus): pos = self.parent.txtPrompt.GetCurrentPos() self.parent.txtPrompt.InsertText(pos, result) self.parent.txtPrompt.GotoPos(pos + l) else: pos = self.parent.txtDocument.GetCurrentPos() self.parent.txtDocument.InsertText(pos, result) self.parent.txtDocument.GotoPos(pos + l) else: self.parent.txtSearchFor.SetValue(result) self.Close(1) def OnbtnOr(self, event): self.insertText('|') def OnbtnRepsN(self, event): d = wx.TextEntryDialog(self, "Enter The Desired Number of Repetitions:", "Insert N Repetitions", "") answer = d.ShowModal() v = d.GetValue() d.Destroy() if (answer == wx.ID_OK): self.insertText('{' + v + '}') def OnbtnRepsOnePlus(self, event): self.insertText('+') def OnbtnRepsZeroOrOne(self, event): self.insertText('?') def OnbtnRepsZeroPlus(self, event): self.insertText('*') def OnbtnSetOfCharacters(self, event): self.insertText('[]') pos = self.txtRE.GetInsertionPoint() self.txtRE.SetInsertionPoint(pos - 1) def OnbtnStart(self, event): self.insertText('^') def OnbtnStartD(self, event): self.insertText('\\A') def OnLoad(self, event): dlg = drFileDialog.FileDialog(self.filedialogparent, "Load Regular Expression From", wildcard) if (len(self.defaultdirectory) > 0): try: dlg.SetDirectory(self.defaultdirectory) except: drScrolledMessageDialog.ShowMessage(self, ("Error Setting Default Directory To: " + self.defaultdirectory), "DrPython Error") if (dlg.ShowModal() == wx.ID_OK): refile = dlg.GetPath().replace("\\", "/") try: f = file(refile, 'r') text = f.read() f.close() except: drScrolledMessageDialog.ShowMessage(self, ("Error Reading From: " + refile), "DrPython Error") text = "" if (text.find('\n') > -1) or (text.find('\r') > -1): drScrolledMessageDialog.ShowMessage(self, ("Error Reading From: " + refile), "DrPython Error") text = "" self.txtRE.SetValue(text) dlg.Destroy() self.Raise() def OnSave(self, event): dlg = drFileDialog.FileDialog(self.filedialogparent, "Save Regular Expression As", wildcard, IsASaveDialog=True) if (len(self.defaultdirectory) > 0): try: dlg.SetDirectory(self.defaultdirectory) except: drScrolledMessageDialog.ShowMessage(self, ("Error Setting Default Directory To: " + self.defaultdirectory), "DrPython Error") if (dlg.ShowModal() == wx.ID_OK): refile = dlg.GetPath().replace("\\", "/") try: f = file(refile, 'w') f.write(self.txtRE.GetValue()) f.close() except: drScrolledMessageDialog.ShowMessage(self, ("Error Writing To: " + refile), "DrPython Error") return if self.enablefeedback: drScrolledMessageDialog.ShowMessage(self, ("Successfully Saved: " + refile), "Save Success") dlg.Destroy() self.Raise()