Exemple #1
0
    def replace(self, pattern, repl, *args, **kwargs):
        """
            Replaces a regular expression pattern with repl in both keys and
            values.

            Returns the number of replacements made.
        """
        new, count = [], 0
        for k, v in self.lst:
            k, c = strutils.safe_subn(pattern, repl, k, *args, **kwargs)
            count += c
            v, c = strutils.safe_subn(pattern, repl, v, *args, **kwargs)
            count += c
            new.append([k, v])
        self.lst = new
        return count
Exemple #2
0
    def replace(self, pattern, repl, flags=0):
        """
            Replaces a regular expression pattern with repl in the headers, the
            request path and the body of the request. Encoded content will be
            decoded before replacement, and re-encoded afterwards.

            Returns:
                The number of replacements made.
        """
        # TODO: Proper distinction between text and bytes.
        c = super(Request, self).replace(pattern, repl, flags)
        self.path, pc = strutils.safe_subn(
            pattern, repl, self.path, flags=flags
        )
        c += pc
        return c
Exemple #3
0
    def replace(self, pattern, repl, flags=0):
        """
            Replaces a regular expression pattern with repl in the headers, the
            request path and the body of the request. Encoded content will be
            decoded before replacement, and re-encoded afterwards.

            Returns:
                The number of replacements made.
        """
        # TODO: Proper distinction between text and bytes.
        c = super(Request, self).replace(pattern, repl, flags)
        self.path, pc = strutils.safe_subn(pattern,
                                           repl,
                                           self.path,
                                           flags=flags)
        c += pc
        return c
Exemple #4
0
    def replace(self, pattern, repl, flags=0):
        """
        Replaces a regular expression pattern with repl in both the headers
        and the body of the message. Encoded body will be decoded
        before replacement, and re-encoded afterwards.

        Returns:
            The number of replacements made.
        """
        # TODO: Proper distinction between text and bytes.
        replacements = 0
        if self.content:
            with decoded(self):
                self.content, replacements = strutils.safe_subn(
                    pattern, repl, self.content, flags=flags
                )
        replacements += self.headers.replace(pattern, repl, flags)
        return replacements
Exemple #5
0
    def replace(self, pattern, repl, flags=0):
        """
        Replaces a regular expression pattern with repl in both the headers
        and the body of the message. Encoded body will be decoded
        before replacement, and re-encoded afterwards.

        Returns:
            The number of replacements made.
        """
        # TODO: Proper distinction between text and bytes.
        replacements = 0
        if self.content:
            with decoded(self):
                self.content, replacements = strutils.safe_subn(pattern,
                                                                repl,
                                                                self.content,
                                                                flags=flags)
        replacements += self.headers.replace(pattern, repl, flags)
        return replacements
Exemple #6
0
def test_safe_subn():
    assert strutils.safe_subn("foo", u"bar", "\xc2foo")