#! /usr/bin/env python
# -*- python -*-
# Time-stamp: <2009-07-06 21:18:41 rozen>
import sys
py2 = py30 = py31 = False
version = sys.hexversion
if version >= 0x020600F0 and version < 0x03000000 :
py2 = True # Python 2.6 or 2.7
from Tkinter import *
import ttk
elif version >= 0x03000000 and version < 0x03010000 :
py30 = True
from tkinter import *
import ttk
elif version >= 0x03010000:
py31 = True
from tkinter import *
import tkinter.ttk as ttk
else:
print ("""
You do not have a version of python supporting ttk widgets..
You need a version >= 2.6 to execute PAGE modules.
""")
sys.exit()
'''
If you use the following functions, change the names 'w' and
'w_win'. Use as a template for creating a new Top-level window.
w = None
def create_Vrex_for_Python ()
global w
global w_win
if w: # So we have only one instance of window.
return
w = Toplevel (root)
w.title('Vrex for Python')
w.geometry('609x577+769+138')
w_win = Vrex_for_Python (w)
Template for routine to destroy a top level window.
def destroy():
global w
w.destroy()
w = None
'''
def vp_start_gui():
global val, w, root
root = Tk()
root.option_add('*font', 'helvetica 12')
root.title('Vrex_for_Python')
root.geometry('609x577+769+138')
w = Vrex_for_Python (root)
init()
root.mainloop()
import vrex_help
def init():
pass
def clear_textbox(o):
o.delete(1.0, END)
def display(level):
go()
clear_textbox(o_m)
for i in range(len(mo)-1):
if mo[i] == None:
insert_text(o_m, "Match failed\n")
else:
if level <= len(mo[i].groups()):
# Insert the text corresponding to the match group.
start, end = mo[i].span(level)
str = s_lines[i][start:end]
insert_text(o_m, str+'\n')
line = '%d.0' % (i+1,)
last = '%d.%d' % (i+1,end)
o_m.tag_add(levels[level], line, last) # Colorize text.
def do_match():
import re
global mo, rexp, sample, s_lines
rexp = read_textbox(o_e)
rexp = rexp.strip()
sample = read_textbox(o_s)
s_lines = sample.split('\n')
rc = re.compile(rexp)
mo = []
for line in s_lines:
mo.append(rc.search(line))
def go():
regexp_colorize()
do_match()
sample_colorize()
def help():
vrex_help.vp_start_gui()
def init():
# This is executed after the GUI is drawn.
global colors, color_noreport, color_lookahead, levels, o_e, o_s, o_m
# Define the object names for the three text windows;
o_e = w.tPa33_f1_tSc35
o_s = w.tPa33_f2_tSc34
o_m = w.tLa35_tSc36
# the colors for the different matching groups
colors = ['red', 'blue', 'darkgreen', 'magenta', 'sienna', 'purple',
'firebrick', 'deeppink', 'deeppink', 'deepskyblue1']
# background color to visualize the non-reporting group (?:...)
color_noreport = 'cyan'
# background color to visualize the lookhead group (?=...) and (?!...)
color_lookahead = 'plum'
# define levels
levels = ['e0', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'e9']
# configure the tags to be used.
for i in range(len(levels)):
o_e.tag_configure(levels[i], foreground=colors[i])
o_s.tag_configure(levels[i], foreground=colors[i])
o_m.tag_configure(levels[i], foreground=colors[i])
o_e.tag_configure('lookahead', background=color_lookahead)
o_e.tag_configure('noreport', background=color_noreport)
def insert_text(o, str):
o.insert(END, str)
def load_regular_expression():
import tkFileDialog
regex_file = tkFileDialog.askopenfilename(filetypes=[("all files", "*")])
if regex_file:
clear_textbox(o_e)
rd = open(regex_file)
exp = rd.read()
o_e.insert(END,exp)
rd.close()
def load_sample():
import tkFileDialog
sample_file = tkFileDialog.askopenfilename(filetypes=[("all files", "*")])
if sample_file:
clear_textbox(o_s)
sd = open(sample_file)
sample = sd.read()
sample = sample.rstrip()
o_s.insert(END,sample)
def quit():
import sys
sys.exit()
def read_textbox(o):
# Note how one can read all of the characters from a text box. The
# 1.0 refers to the first character (line position 0) in the first
# line (line 1).
return o.get(1.0, END)
def regexp_colorize():
global nblevels
import types
stack = []
indicies = []
# The format of indicies is a list of tuple each looking like
# (type, min, max). Type can be report, no report, or lookahead.
exp = read_textbox(o_e).rstrip()
indicies.append(('report', 0, len(exp)))
nblevels = 1
for i in range(len(exp)):
c = exp[i]
if c == '\\':
i += 1
continue
elif c == '(':
c = exp[i+1]
if c == '?':
what = exp[1+2]
if what == ':':
type = 'lookahead'
else:
type = 'noreport'
else:
type = 'report'
nblevels += 1
min = i
max = 0
indicies.append((type, min, max))
stack.append(len(indicies)-1)
elif c == ')':
idx = stack.pop()
type, min, max = indicies[idx]
indicies[idx] = (type, min, i)
# Remove old colors.
for level in range(nblevels):
o_e.tag_remove(levels[level], 1.0, END)
o_e.tag_remove("lookahead", 1.0, END)
o_e.tag_remove("noreport", 1.0, END)
# Colorize the regular expression
i = 0
for type,min,max in indicies:
min_c = '1.0+%dchars' % min
max_1 = max + 1
max_c = '1.0+%dchars' % max_1
o_e.tag_add(levels[i], min_c, max_c)
i += 1
# Colorize special items.
for type,min,max in indicies:
if type == 'report':
continue
min_c = '1.0+%dchars' % min
max_1 = max + 1
max_c = '1.0+%dchars' % (max + 1,)
o_e.tag_add(type, min_c, max_c)
def sample_colorize():
# Remove colors
for level in range(nblevels):
o_s.tag_remove(levels[level], 1.0, END)
for i in range(len(mo)):
if mo[i] == None:
continue
# Add the new colors
if level <= len(mo[i].groups()):
for level in range(nblevels):
start, end = mo[i].span(level)
min_c = '%d.0+%dchars' % (i+1, start)
max_c = '%d.0+%dchars' % (i+1, end)
o_s.tag_add(levels[level], min_c, max_c)
def save_regular_expression():
import tkFileDialog
regex_file = tkFileDialog.asksaveasfilename(filetypes=[("all files", "*")])
if regex_file:
rd = open(regex_file, 'w')
rxp = read_textbox(o_e)
rd.write(rxp)
rd.close()
def save_sample():
import tkFileDialog
sample_file = tkFileDialog.asksaveasfilename(filetypes=[("all files", "*")])
if sample_file:
sd = open(sample_file, 'w')
sample = read_textbox(o_s)
sd.read(sample)
sd.close()
class Vrex_for_Python:
def __init__(self, master=None):
# Set background of toplevel window to match
# current style
style = ttk.Style()
theme = style.theme_use()
default = style.lookup(theme, 'background')
master.configure(background=default)
self.tPa33 = ttk.Panedwindow (master, orient="vertical")
self.tPa33.place(relx=0.02,rely=0.02,relheight=0.49,relwidth=0.96)
self.tPa33.configure(width="585")
self.tPa33.configure(height="280")
self.tPa33_f1 = ttk.Labelframe(height=75, text='Regular Expression')
self.tPa33.add(self.tPa33_f1)
self.tPa33_f2 = ttk.Labelframe(text='Sample')
self.tPa33.add(self.tPa33_f2)
self.__funcid0 = self.tPa33.bind('<Map>', self.__adjust_sash0)
self.tPa33_f1_tSc35 = ScrolledText (self.tPa33_f1)
self.tPa33_f1_tSc35.place(relx=0.04,rely=0.2,relheight=0.75
,relwidth=0.92)
self.tPa33_f1_tSc35.configure(height="3")
self.tPa33_f1_tSc35.configure(selectbackground="#c4c4c4")
self.tPa33_f1_tSc35.configure(width="10")
self.tPa33_f1_tSc35.configure(wrap="none")
self.tPa33_f2_tSc34 = ScrolledText (self.tPa33_f2)
self.tPa33_f2_tSc34.place(relx=0.04,rely=0.08,relheight=0.86
,relwidth=0.92)
self.tPa33_f2_tSc34.configure(height="3")
self.tPa33_f2_tSc34.configure(selectbackground="#c4c4c4")
self.tPa33_f2_tSc34.configure(width="10")
self.m32 = Menu(master,font=('Nimbus Sans L',14,'normal','roman',))
master.configure(menu = self.m32)
self.m32_men33 = Menu(master,tearoff=0,font=('Nimbus Sans L',14,'normal','roman',))
self.m32.add_cascade(label="File",menu=self.m32_men33)
self.m32_men33.add_command(label="Load regular expression",command=load_regular_expression)
self.m32_men33.add_command(label="Save regular expression",command=save_regular_expression)
self.m32_men33.add_separator()
self.m32_men33.add_command(label="Load sample",command=load_sample)
self.m32_men33.add_command(label="Save sample",command=save_sample)
self.m32_men33.add_separator()
self.m32_men33.add_command(label="Quit",command=quit)
self.m32.add_command(label="Help",command=help)
self.but34 = Button (master)
self.but34.place(relx=0.89,rely=0.54)
self.but34.configure(activebackground="#f9f9f9")
self.but34.configure(command=quit)
self.but34.configure(text="Quit")
self.tLa35 = ttk.Labelframe (master)
self.tLa35.place(relx=0.03,rely=0.62,relheight=0.31,relwidth=0.95)
self.tLa35.configure(text="Matches")
self.tLa35.configure(width="580")
self.tLa35.configure(height="180")
self.tLa35_tSc36 = ScrolledText (self.tLa35)
self.tLa35_tSc36.place(relx=0.04,rely=0.13,relheight=0.81,relwidth=0.92)
self.tLa35_tSc36.configure(height="3")
self.tLa35_tSc36.configure(selectbackground="#c4c4c4")
self.tLa35_tSc36.configure(width="10")
self.but32 = Button (master)
self.but32.place(relx=0.17,rely=0.54)
self.but32.configure(activebackground="#f9f9f9")
self.but32.configure(command=lambda:display(0))
self.but32.configure(text="Match")
self.but33 = Button (master)
self.but33.place(relx=0.28,rely=0.54)
self.but33.configure(activebackground="#f9f9f9")
self.but33.configure(command=lambda:display(1))
self.but33.configure(foreground="blue")
self.but33.configure(text="1")
self.but35 = Button (master)
self.but35.place(relx=0.34,rely=0.54)
self.but35.configure(activebackground="#f9f9f9")
self.but35.configure(command=lambda:display(2))
self.but35.configure(foreground="darkgreen")
self.but35.configure(text="2")
self.but36 = Button (master)
self.but36.place(relx=0.39,rely=0.54)
self.but36.configure(activebackground="#f9f9f9")
self.but36.configure(command=lambda:display(3))
self.but36.configure(foreground="magenta")
self.but36.configure(text="3")
self.but37 = Button (master)
self.but37.place(relx=0.45,rely=0.54)
self.but37.configure(activebackground="#f9f9f9")
self.but37.configure(command=lambda:display(4))
self.but37.configure(foreground="sienna")
self.but37.configure(text="4")
self.but38 = Button (master)
self.but38.place(relx=0.51,rely=0.54)
self.but38.configure(activebackground="#f9f9f9")
self.but38.configure(command=lambda:display(5))
self.but38.configure(foreground="purple")
self.but38.configure(text="5")
self.but39 = Button (master)
self.but39.place(relx=0.57,rely=0.54)
self.but39.configure(activebackground="#f9f9f9")
self.but39.configure(command=lambda:display(6))
self.but39.configure(foreground="firebrick")
self.but39.configure(text="6")
self.but40 = Button (master)
self.but40.place(relx=0.62,rely=0.54)
self.but40.configure(activebackground="#f9f9f9")
self.but40.configure(command=lambda:display(7))
self.but40.configure(foreground="deeppink")
self.but40.configure(text="7")
self.but41 = Button (master)
self.but41.place(relx=0.68,rely=0.54)
self.but41.configure(activebackground="#f9f9f9")
self.but41.configure(command=lambda:display(8))
self.but41.configure(foreground="green4")
self.but41.configure(text="8")
self.but42 = Button (master)
self.but42.place(relx=0.74,rely=0.54)
self.but42.configure(activebackground="#f9f9f9")
self.but42.configure(command=lambda:display(9))
self.but42.configure(foreground="deepskyblue1")
self.but42.configure(text="9")
self.but43 = Button (master)
self.but43.place(relx=0.03,rely=0.54)
self.but43.configure(command=go)
self.but43.configure(activebackground="#f9f9f9")
self.but43.configure(text="Go")
def __adjust_sash0(self, event):
paned = event.widget
pos = [75,]
i = 0
for sash in pos:
paned.sashpos(i, sash)
i += 1
paned.unbind('<map>', self.__funcid0)
del self.__funcid0
# The following code is added to facilitate the Scrolled widgets you specified.
class AutoScroll(object):
'''Configure the scrollbars for a widget.'''
def __init__(self, master):
vsb = ttk.Scrollbar(master, orient='vertical', command=self.yview)
hsb = ttk.Scrollbar(master, orient='horizontal', command=self.xview)
self.configure(yscrollcommand=self._autoscroll(vsb),
xscrollcommand=self._autoscroll(hsb))
self.grid(column=0, row=0, sticky='nsew')
vsb.grid(column=1, row=0, sticky='ns')
hsb.grid(column=0, row=1, sticky='ew')
master.grid_columnconfigure(0, weight=1)
master.grid_rowconfigure(0, weight=1)
# Copy geometry methods of master (took from ScrolledText.py)
methods = Pack.__dict__.keys() + Grid.__dict__.keys() \
+ Place.__dict__.keys()
for meth in methods:
if meth[0] != '_' and meth not in ('config', 'configure'):
setattr(self, meth, getattr(master, meth))
@staticmethod
def _autoscroll(sbar):
'''Hide and show scrollbar as needed.'''
def wrapped(first, last):
first, last = float(first), float(last)
if first <= 0 and last >= 1:
sbar.grid_remove()
else:
sbar.grid()
sbar.set(first, last)
return wrapped
def __str__(self):
return str(self.master)
def _create_container(func):
'''Creates a ttk Frame with a given master, and use this new frame to
place the scrollbars and the widget.'''
def wrapped(cls, master, **kw):
container = ttk.Frame(master)
return func(cls, container, **kw)
return wrapped
class ScrolledText(AutoScroll, Text):
'''A standard Tkinter Text widget with scrollbars that will
automatically show/hide as needed.'''
@_create_container
def __init__(self, master, **kw):
Text.__init__(self, master, **kw)
AutoScroll.__init__(self, master)
if __name__ == '__main__':
vp_start_gui()
|