Changes replace logic to function in both Python 2.6.x and 2.7.x

Tests now only assume Python 2.6.x rather than requiring 2.7.x. This does not preclude the use of flags as a kwarg in replace
This commit is contained in:
Stephen Altamirano 2011-07-26 22:47:08 -07:00
parent e6288e2d07
commit 78049abac1
4 changed files with 27 additions and 27 deletions

View File

@ -323,16 +323,16 @@ class Flow:
self.response.ack() self.response.ack()
self.intercepting = False self.intercepting = False
def replace(self, pattern, repl, count=0, flags=0): def replace(self, pattern, repl, *args, **kwargs):
""" """
Replaces a regular expression pattern with repl in all parts of the Replaces a regular expression pattern with repl in all parts of the
flow . Returns the number of replacements made. flow . Returns the number of replacements made.
""" """
c = self.request.replace(pattern, repl, count, flags) c = self.request.replace(pattern, repl, *args, **kwargs)
if self.response: if self.response:
c += self.response.replace(pattern, repl, count, flags) c += self.response.replace(pattern, repl, *args, **kwargs)
if self.error: if self.error:
c += self.error.replace(pattern, repl, count, flags) c += self.error.replace(pattern, repl, *args, **kwargs)
return c return c

View File

@ -280,16 +280,16 @@ class Request(controller.Msg):
else: else:
return self.FMT_PROXY % (self.method, self.scheme, self.host, self.port, self.path, str(headers), content) return self.FMT_PROXY % (self.method, self.scheme, self.host, self.port, self.path, str(headers), content)
def replace(self, pattern, repl, count=0, flags=0): def replace(self, pattern, repl, *args, **kwargs):
""" """
Replaces a regular expression pattern with repl in both the headers Replaces a regular expression pattern with repl in both the headers
and the body of the request. Returns the number of replacements and the body of the request. Returns the number of replacements
made. made.
""" """
self.content, c = re.subn(pattern, repl, self.content, count, flags) self.content, c = re.subn(pattern, repl, self.content, *args, **kwargs)
self.path, pc = re.subn(pattern, repl, self.path, count, flags) self.path, pc = re.subn(pattern, repl, self.path, *args, **kwargs)
c += pc c += pc
c += self.headers.replace(pattern, repl, count, flags) c += self.headers.replace(pattern, repl, *args, **kwargs)
return c return c
@ -418,14 +418,14 @@ class Response(controller.Msg):
data = (proto, str(headers), content) data = (proto, str(headers), content)
return self.FMT%data return self.FMT%data
def replace(self, pattern, repl, count=0, flags=0): def replace(self, pattern, repl, *args, **kwargs):
""" """
Replaces a regular expression pattern with repl in both the headers Replaces a regular expression pattern with repl in both the headers
and the body of the response. Returns the number of replacements and the body of the response. Returns the number of replacements
made. made.
""" """
self.content, c = re.subn(pattern, repl, self.content, count, flags) self.content, c = re.subn(pattern, repl, self.content, *args, **kwargs)
c += self.headers.replace(pattern, repl, count, flags) c += self.headers.replace(pattern, repl, *args, **kwargs)
return c return c
@ -497,13 +497,13 @@ class Error(controller.Msg):
def __eq__(self, other): def __eq__(self, other):
return self.get_state() == other.get_state() return self.get_state() == other.get_state()
def replace(self, pattern, repl, count=0, flags=0): def replace(self, pattern, repl, *args, **kwargs):
""" """
Replaces a regular expression pattern with repl in both the headers Replaces a regular expression pattern with repl in both the headers
and the body of the request. Returns the number of replacements and the body of the request. Returns the number of replacements
made. made.
""" """
self.msg, c = re.subn(pattern, repl, self.msg, count, flags) self.msg, c = re.subn(pattern, repl, self.msg, *args, **kwargs)
return c return c

View File

@ -1,15 +1,15 @@
# Copyright (C) 2010 Aldo Cortesi # Copyright (C) 2010 Aldo Cortesi
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or # the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import re, os, subprocess, datetime, textwrap, errno import re, os, subprocess, datetime, textwrap, errno
@ -67,7 +67,7 @@ def cleanBin(s):
if i not in "\n\r\t": if i not in "\n\r\t":
parts.append(".") parts.append(".")
return "".join(parts) return "".join(parts)
TAG = r""" TAG = r"""
<\s* <\s*
@ -279,16 +279,16 @@ class Headers:
ret.append([name, value]) ret.append([name, value])
self.lst = ret self.lst = ret
def replace(self, pattern, repl, count=0, flags=0): def replace(self, pattern, repl, *args, **kwargs):
""" """
Replaces a regular expression pattern with repl in both header keys Replaces a regular expression pattern with repl in both header keys
and values. Returns the number of replacements made. and values. Returns the number of replacements made.
""" """
nlst, count = [], 0 nlst, count = [], 0
for i in self.lst: for i in self.lst:
k, c = re.subn(pattern, repl, i[0], count, flags) k, c = re.subn(pattern, repl, i[0], *args, **kwargs)
count += c count += c
v, c = re.subn(pattern, repl, i[1], count, flags) v, c = re.subn(pattern, repl, i[1], *args, **kwargs)
count += c count += c
nlst.append([k, v]) nlst.append([k, v])
self.lst = nlst self.lst = nlst

View File

@ -133,7 +133,7 @@ class uRequest(libpry.AutoTree):
r.path = "path/foo" r.path = "path/foo"
r.headers["Foo"] = ["fOo"] r.headers["Foo"] = ["fOo"]
r.content = "afoob" r.content = "afoob"
assert r.replace("foo", "boo", flags=re.I) == 4 assert r.replace("foo(?i)", "boo") == 4
assert r.path == "path/boo" assert r.path == "path/boo"
assert not "foo" in r.content assert not "foo" in r.content
assert r.headers["boo"] == ["boo"] assert r.headers["boo"] == ["boo"]
@ -199,7 +199,7 @@ class uResponse(libpry.AutoTree):
r = tutils.tresp() r = tutils.tresp()
r.headers["Foo"] = ["fOo"] r.headers["Foo"] = ["fOo"]
r.content = "afoob" r.content = "afoob"
assert r.replace("foo", "boo", flags=re.I) == 3 assert r.replace("foo(?i)", "boo") == 3
assert not "foo" in r.content assert not "foo" in r.content
assert r.headers["boo"] == ["boo"] assert r.headers["boo"] == ["boo"]