Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80e57fb

Browse files
committed
Converted to use re instead of regex; version 0.9.0.
1 parent 9897f0f commit 80e57fb

3 files changed

Lines changed: 52 additions & 55 deletions

File tree

Tools/faqwiz/README

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ FAQ Wizard
22
----------
33

44
Author: Guido van Rossum <[email protected]>
5-
Version: 0.8.4
6-
Date: 16 December 1997
5+
Version: 0.9.0
6+
Date: 21 December 1997
77

88

99
This is a CGI program that maintains a user-editable FAQ. It uses RCS
@@ -22,6 +22,17 @@ faqwiz.py main module, lives in same directory as FAQ entry files
2222
faqconf.py main configuration module
2323
faqcust.py additional local customization module (optional)
2424

25+
26+
What's New?
27+
-----------
28+
29+
Version 0.9.0 uses the re module (Perl style regular expressions) for
30+
all its regular expression needs, instead of the regex and regsub
31+
modules (Emacs style). This affects the syntax for regular
32+
expressions entered by the user as search strings (with "regular
33+
expression" checked), hence the version number jump.
34+
35+
2536
Setup Information
2637
-----------------
2738

@@ -76,6 +87,7 @@ file faq01.001.htp,v in the RCS subdirectory. You can now exercise
7687
the other FAQ wizard features (search, index, whole FAQ, what's new,
7788
roulette, and so on).
7889

90+
7991
Maintaining Multiple FAQs
8092
-------------------------
8193

Tools/faqwiz/faqconf.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
# Version -- don't change unless you edit faqwiz.py
5151

52-
WIZVERSION = "0.8.4" # FAQ Wizard version
52+
WIZVERSION = "0.9.0" # FAQ Wizard version
5353

5454
# This parameter is normally overwritten with a dynamic value
5555

@@ -58,12 +58,12 @@
5858
FAQCGI = os.path.basename(sys.argv[0]) or FAQCGI
5959
del os, sys
6060

61-
# Regular expression to recognize FAQ entry files: group(1) should be
62-
# the section number, group(2) should be the question number. Both
63-
# should be fixed width so simple-minded sorting yields the right
64-
# order.
61+
# Perl (re module) style regular expression to recognize FAQ entry
62+
# files: group(1) should be the section number, group(2) should be the
63+
# question number. Both should be fixed width so simple-minded
64+
# sorting yields the right order.
6565

66-
OKFILENAME = "^faq\([0-9][0-9]\)\.\([0-9][0-9][0-9]\)\.htp$"
66+
OKFILENAME = r"^faq(\d\d)\.(\d\d\d)\.htp$"
6767

6868
# Format to construct a FAQ entry file name
6969

Tools/faqwiz/faqwiz.py

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
"""
1313

14-
import sys, string, time, os, stat, regex, cgi, faqconf
14+
import sys, string, time, os, stat, re, cgi, faqconf
1515
from faqconf import * # This imports all uppercase names
1616
now = time.time()
1717

@@ -32,21 +32,15 @@ def __init__(self, file, why=None):
3232
FileError.__init__(self, file)
3333
self.why = why
3434

35-
def replace(s, old, new):
36-
try:
37-
return string.replace(s, old, new)
38-
except AttributeError:
39-
return string.join(string.split(s, old), new)
40-
4135
def escape(s):
42-
s = replace(s, '&', '&amp;')
43-
s = replace(s, '<', '&lt;')
44-
s = replace(s, '>', '&gt;')
36+
s = string.replace(s, '&', '&amp;')
37+
s = string.replace(s, '<', '&lt;')
38+
s = string.replace(s, '>', '&gt;')
4539
return s
4640

4741
def escapeq(s):
4842
s = escape(s)
49-
s = replace(s, '"', '&quot;')
43+
s = string.replace(s, '"', '&quot;')
5044
return s
5145

5246
def _interpolate(format, args, kw):
@@ -73,20 +67,20 @@ def emit(format, *args, **kw):
7367
def translate(text, pre=0):
7468
global translate_prog
7569
if not translate_prog:
76-
url = '\(http\|ftp\|https\)://[^ \t\r\n]*'
77-
email = '\<[-a-zA-Z0-9._]+@[-a-zA-Z0-9._]+'
78-
translate_prog = prog = regex.compile(url + '\|' + email)
70+
translate_prog = prog = re.compile(
71+
r'\b(http|ftp|https)://\S+(\b|/)|\b[-.\w]+@[-.\w]+')
7972
else:
8073
prog = translate_prog
8174
i = 0
8275
list = []
8376
while 1:
84-
j = prog.search(text, i)
85-
if j < 0:
77+
m = prog.search(text, i)
78+
if not m:
8679
break
80+
j = m.start()
8781
list.append(escape(text[i:j]))
8882
i = j
89-
url = prog.group(0)
83+
url = m.group(0)
9084
while url[-1] in '();:,.?\'"<>':
9185
url = url[:-1]
9286
i = i + len(url)
@@ -103,26 +97,19 @@ def translate(text, pre=0):
10397
list.append(escape(text[i:j]))
10498
return string.join(list, '')
10599

106-
emphasize_prog = None
107-
108100
def emphasize(line):
109-
global emphasize_prog
110-
import regsub
111-
if not emphasize_prog:
112-
pat = '\*\([a-zA-Z]+\)\*'
113-
emphasize_prog = regex.compile(pat)
114-
return regsub.gsub(emphasize_prog, '<I>\\1</I>', line)
101+
return re.sub(r'\*([a-zA-Z]+)\*', r'<I>\1</I>', line)
115102

116103
revparse_prog = None
117104

118105
def revparse(rev):
119106
global revparse_prog
120107
if not revparse_prog:
121-
revparse_prog = regex.compile(
122-
'^\([1-9][0-9]?[0-9]?\)\.\([1-9][0-9]?[0-9]?[0-9]?\)$')
123-
if revparse_prog.match(rev) < 0:
108+
revparse_prog = re.compile(r'^(\d{1,3})\.(\d{1-4})$')
109+
m = revparse_prog.match(rev)
110+
if not m:
124111
return None
125-
[major, minor] = map(string.atoi, revparse_prog.group(1, 2))
112+
[major, minor] = map(string.atoi, m.group(1, 2))
126113
return major, minor
127114

128115
def load_cookies():
@@ -315,7 +302,7 @@ class FaqDir:
315302

316303
entryclass = FaqEntry
317304

318-
__okprog = regex.compile(OKFILENAME)
305+
__okprog = re.compile(OKFILENAME)
319306

320307
def __init__(self, dir=os.curdir):
321308
self.__dir = dir
@@ -327,17 +314,18 @@ def __fill(self):
327314
self.__files = files = []
328315
okprog = self.__okprog
329316
for file in os.listdir(self.__dir):
330-
if okprog.match(file) >= 0:
317+
if self.__okprog.match(file):
331318
files.append(file)
332319
files.sort()
333320

334321
def good(self, file):
335-
return self.__okprog.match(file) >= 0
322+
return self.__okprog.match(file)
336323

337324
def parse(self, file):
338-
if not self.good(file):
325+
m = self.good(file)
326+
if not m:
339327
return None
340-
sec, num = self.__okprog.group(1, 2)
328+
sec, num = m.group(1, 2)
341329
return string.atoi(sec), string.atoi(num)
342330

343331
def list(self):
@@ -426,31 +414,29 @@ def do_search(self):
426414
self.error("Empty query string!")
427415
return
428416
if self.ui.querytype == 'simple':
429-
for c in '\\.[]?+^$*':
430-
if c in query:
431-
query = replace(query, c, '\\'+c)
417+
query = re.escape(query)
432418
queries = [query]
433419
elif self.ui.querytype in ('anykeywords', 'allkeywords'):
434-
import regsub
435-
words = string.split(regsub.gsub('[^a-zA-Z0-9]+', ' ', query))
420+
words = filter(None, re.split('\W+', query))
436421
if not words:
437422
self.error("No keywords specified!")
438423
return
439-
words = map(lambda w: '\<%s\>' % w, words)
424+
words = map(lambda w: r'\b%s\b' % w, words)
440425
if self.ui.querytype[:3] == 'any':
441-
queries = [string.join(words, '\|')]
426+
queries = [string.join(words, '|')]
442427
else:
428+
# Each of the individual queries must match
443429
queries = words
444430
else:
445-
# Default to regex
431+
# Default to regular expression
446432
queries = [query]
447433
self.prologue(T_SEARCH)
448434
progs = []
449435
for query in queries:
450436
if self.ui.casefold == 'no':
451-
p = regex.compile(query)
437+
p = re.compile(query)
452438
else:
453-
p = regex.compile(query, regex.casefold)
439+
p = re.compile(query, re.IGNORECASE)
454440
progs.append(p)
455441
hits = []
456442
for file in self.dir.list():
@@ -459,7 +445,7 @@ def do_search(self):
459445
except FileError:
460446
constants
461447
for p in progs:
462-
if p.search(entry.title) < 0 and p.search(entry.body) < 0:
448+
if not p.search(entry.title) and not p.search(entry.body):
463449
break
464450
else:
465451
hits.append(file)
@@ -777,8 +763,7 @@ def commit(self, entry):
777763
file = entry.file
778764
# Normalize line endings in body
779765
if '\r' in self.ui.body:
780-
import regsub
781-
self.ui.body = regsub.gsub('\r\n?', '\n', self.ui.body)
766+
self.ui.body = re.sub('\r\n?', '\n', self.ui.body)
782767
# Normalize whitespace in title
783768
self.ui.title = string.join(string.split(self.ui.title))
784769
# Check that there were any changes

0 commit comments

Comments
 (0)