Merge lp:~max-rabkin/ibid/factoid-ci-replace-709175-0.1 into lp:~ibid-core/ibid/old-release-0.1-1.6

Proposed by Max Rabkin on 2011-02-24
Status: Merged
Approved by: Stefano Rivera on 2011-03-16
Approved revision: 989
Merged at revision: 991
Proposed branch: lp:~max-rabkin/ibid/factoid-ci-replace-709175-0.1
Merge into: lp:~ibid-core/ibid/old-release-0.1-1.6
Diff against target: 48 lines (+21/-17)
1 file modified
ibid/plugins/factoid.py (+21/-17)
To merge this branch: bzr merge lp:~max-rabkin/ibid/factoid-ci-replace-709175-0.1
Reviewer Review Type Date Requested Status
Jonathan Hitchcock Approve on 2011-03-06
Stefano Rivera 2011-02-24 Approve on 2011-02-27
Review via email: mp+51196@code.launchpad.net

Commit message

Use regexen to do string replacement, allowing case-insensitive replacement

To post a comment you must log in.
Stefano Rivera (stefanor) :
review: Approve
Jonathan Hitchcock (vhata) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ibid/plugins/factoid.py'
2--- ibid/plugins/factoid.py 2011-02-21 15:27:41 +0000
3+++ ibid/plugins/factoid.py 2011-02-24 19:25:16 +0000
4@@ -722,23 +722,27 @@
5 op, search, replace, flags = parts
6 flags = flags.lower()
7 if op == "s":
8- if "r" in flags:
9- if "i" in flags:
10- search += "(?i)"
11- try:
12- factoid[2].value = re.sub(search, replace, oldvalue, int("g" not in flags))
13- except:
14- event.addresponse(u"That operation makes no sense. Try something like s/foo/bar/")
15- return
16- else:
17- newvalue = oldvalue.replace(search, replace, "g" in flags and -1 or 1)
18- if newvalue == oldvalue:
19- event.addresponse(u"I couldn't find '%(terms)s' in '%(oldvalue)s'. If that was a proper regular expression, append the 'r' flag", {
20- 'terms': search,
21- 'oldvalue': oldvalue,
22- })
23- return
24- factoid[2].value = newvalue
25+ if "r" not in flags:
26+ plain_search = search
27+ search = re.escape(search)
28+ replace = replace.replace('\\', '\\\\')
29+ if "i" in flags:
30+ search += "(?i)"
31+ try:
32+ if "r" not in flags and not re.search(search, oldvalue):
33+ event.addresponse(u"I couldn't find '%(terms)s' in "
34+ u"'%(oldvalue)s'. If that was a "
35+ u"proper regular expression, append "
36+ u"the 'r' flag", {
37+ 'terms': plain_search,
38+ 'oldvalue': oldvalue,
39+ })
40+ return
41+
42+ factoid[2].value = re.sub(search, replace, oldvalue, int("g" not in flags))
43+ except:
44+ event.addresponse(u"That operation makes no sense. Try something like s/foo/bar/")
45+ return
46
47 elif op == "y":
48 if len(search) != len(replace):

Subscribers

People subscribed via source and target branches