1111
1212"""
1313
14- import sys , string , time , os , stat , regex , cgi , faqconf
14+ import sys , string , time , os , stat , re , cgi , faqconf
1515from faqconf import * # This imports all uppercase names
1616now = 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-
4135def escape (s ):
42- s = replace (s , '&' , '&' )
43- s = replace (s , '<' , '<' )
44- s = replace (s , '>' , '>' )
36+ s = string . replace (s , '&' , '&' )
37+ s = string . replace (s , '<' , '<' )
38+ s = string . replace (s , '>' , '>' )
4539 return s
4640
4741def escapeq (s ):
4842 s = escape (s )
49- s = replace (s , '"' , '"' )
43+ s = string . replace (s , '"' , '"' )
5044 return s
5145
5246def _interpolate (format , args , kw ):
@@ -73,20 +67,20 @@ def emit(format, *args, **kw):
7367def 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-
108100def 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
116103revparse_prog = None
117104
118105def 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
128115def 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