PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Windows/Python3.8/WPy64-3830/WPy64-3830/python-3.8.3.amd64/Tools/demo/redemo.py

https://gitlab.com/abhi1tb/build
Python | 171 lines | 161 code | 8 blank | 2 comment | 0 complexity | 45da98041faa46dda18425e3f4b028aa MD5 | raw file
  1. #!/usr/bin/env python3
  2. """Basic regular expression demonstration facility (Perl style syntax)."""
  3. from tkinter import *
  4. import re
  5. class ReDemo:
  6. def __init__(self, master):
  7. self.master = master
  8. self.promptdisplay = Label(self.master, anchor=W,
  9. text="Enter a Perl-style regular expression:")
  10. self.promptdisplay.pack(side=TOP, fill=X)
  11. self.regexdisplay = Entry(self.master)
  12. self.regexdisplay.pack(fill=X)
  13. self.regexdisplay.focus_set()
  14. self.addoptions()
  15. self.statusdisplay = Label(self.master, text="", anchor=W)
  16. self.statusdisplay.pack(side=TOP, fill=X)
  17. self.labeldisplay = Label(self.master, anchor=W,
  18. text="Enter a string to search:")
  19. self.labeldisplay.pack(fill=X)
  20. self.labeldisplay.pack(fill=X)
  21. self.showframe = Frame(master)
  22. self.showframe.pack(fill=X, anchor=W)
  23. self.showvar = StringVar(master)
  24. self.showvar.set("first")
  25. self.showfirstradio = Radiobutton(self.showframe,
  26. text="Highlight first match",
  27. variable=self.showvar,
  28. value="first",
  29. command=self.recompile)
  30. self.showfirstradio.pack(side=LEFT)
  31. self.showallradio = Radiobutton(self.showframe,
  32. text="Highlight all matches",
  33. variable=self.showvar,
  34. value="all",
  35. command=self.recompile)
  36. self.showallradio.pack(side=LEFT)
  37. self.stringdisplay = Text(self.master, width=60, height=4)
  38. self.stringdisplay.pack(fill=BOTH, expand=1)
  39. self.stringdisplay.tag_configure("hit", background="yellow")
  40. self.grouplabel = Label(self.master, text="Groups:", anchor=W)
  41. self.grouplabel.pack(fill=X)
  42. self.grouplist = Listbox(self.master)
  43. self.grouplist.pack(expand=1, fill=BOTH)
  44. self.regexdisplay.bind('<Key>', self.recompile)
  45. self.stringdisplay.bind('<Key>', self.reevaluate)
  46. self.compiled = None
  47. self.recompile()
  48. btags = self.regexdisplay.bindtags()
  49. self.regexdisplay.bindtags(btags[1:] + btags[:1])
  50. btags = self.stringdisplay.bindtags()
  51. self.stringdisplay.bindtags(btags[1:] + btags[:1])
  52. def addoptions(self):
  53. self.frames = []
  54. self.boxes = []
  55. self.vars = []
  56. for name in ('IGNORECASE',
  57. 'MULTILINE',
  58. 'DOTALL',
  59. 'VERBOSE'):
  60. if len(self.boxes) % 3 == 0:
  61. frame = Frame(self.master)
  62. frame.pack(fill=X)
  63. self.frames.append(frame)
  64. val = getattr(re, name).value
  65. var = IntVar()
  66. box = Checkbutton(frame,
  67. variable=var, text=name,
  68. offvalue=0, onvalue=val,
  69. command=self.recompile)
  70. box.pack(side=LEFT)
  71. self.boxes.append(box)
  72. self.vars.append(var)
  73. def getflags(self):
  74. flags = 0
  75. for var in self.vars:
  76. flags = flags | var.get()
  77. return flags
  78. def recompile(self, event=None):
  79. try:
  80. self.compiled = re.compile(self.regexdisplay.get(),
  81. self.getflags())
  82. bg = self.promptdisplay['background']
  83. self.statusdisplay.config(text="", background=bg)
  84. except re.error as msg:
  85. self.compiled = None
  86. self.statusdisplay.config(
  87. text="re.error: %s" % str(msg),
  88. background="red")
  89. self.reevaluate()
  90. def reevaluate(self, event=None):
  91. try:
  92. self.stringdisplay.tag_remove("hit", "1.0", END)
  93. except TclError:
  94. pass
  95. try:
  96. self.stringdisplay.tag_remove("hit0", "1.0", END)
  97. except TclError:
  98. pass
  99. self.grouplist.delete(0, END)
  100. if not self.compiled:
  101. return
  102. self.stringdisplay.tag_configure("hit", background="yellow")
  103. self.stringdisplay.tag_configure("hit0", background="orange")
  104. text = self.stringdisplay.get("1.0", END)
  105. last = 0
  106. nmatches = 0
  107. while last <= len(text):
  108. m = self.compiled.search(text, last)
  109. if m is None:
  110. break
  111. first, last = m.span()
  112. if last == first:
  113. last = first+1
  114. tag = "hit0"
  115. else:
  116. tag = "hit"
  117. pfirst = "1.0 + %d chars" % first
  118. plast = "1.0 + %d chars" % last
  119. self.stringdisplay.tag_add(tag, pfirst, plast)
  120. if nmatches == 0:
  121. self.stringdisplay.yview_pickplace(pfirst)
  122. groups = list(m.groups())
  123. groups.insert(0, m.group())
  124. for i in range(len(groups)):
  125. g = "%2d: %r" % (i, groups[i])
  126. self.grouplist.insert(END, g)
  127. nmatches = nmatches + 1
  128. if self.showvar.get() == "first":
  129. break
  130. if nmatches == 0:
  131. self.statusdisplay.config(text="(no match)",
  132. background="yellow")
  133. else:
  134. self.statusdisplay.config(text="")
  135. # Main function, run when invoked as a stand-alone Python program.
  136. def main():
  137. root = Tk()
  138. demo = ReDemo(root)
  139. root.protocol('WM_DELETE_WINDOW', root.quit)
  140. root.mainloop()
  141. if __name__ == '__main__':
  142. main()