def replace(self, pattern, repl, flags=0): """ Replaces a regular expression pattern with repl in each "name: value" header line. Returns: The number of replacements made. """ if isinstance(pattern, six.text_type): pattern = strutils.escaped_str_to_bytes(pattern) if isinstance(repl, six.text_type): repl = strutils.escaped_str_to_bytes(repl) pattern = re.compile(pattern, flags) replacements = 0 fields = [] for name, value in self.fields: line, n = pattern.subn(repl, name + b": " + value) try: name, value = line.split(b": ", 1) except ValueError: # We get a ValueError if the replacement removed the ": " # There's not much we can do about this, so we just keep the header as-is. pass else: replacements += n fields.append([name, value]) self.fields = fields return replacements
def replace(self, pattern, repl, flags=0, count=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. """ if isinstance(pattern, six.text_type): pattern = strutils.escaped_str_to_bytes(pattern) if isinstance(repl, six.text_type): repl = strutils.escaped_str_to_bytes(repl) replacements = 0 if self.content: self.content, replacements = re.subn(pattern, repl, self.content, flags=flags, count=count) replacements += self.headers.replace(pattern, repl, flags=flags, count=count) return replacements
def replace(self, pattern, repl, flags=0, count=0): """ Replaces a regular expression pattern with repl in each "name: value" header line. Returns: The number of replacements made. """ if isinstance(pattern, six.text_type): pattern = strutils.escaped_str_to_bytes(pattern) if isinstance(repl, six.text_type): repl = strutils.escaped_str_to_bytes(repl) pattern = re.compile(pattern, flags) replacements = 0 flag_count = count > 0 fields = [] for name, value in self.fields: line, n = pattern.subn(repl, name + b": " + value, count=count) try: name, value = line.split(b": ", 1) except ValueError: # We get a ValueError if the replacement removed the ": " # There's not much we can do about this, so we just keep the header as-is. pass else: replacements += n if flag_count: count -= n if count == 0: break fields.append((name, value)) self.fields = tuple(fields) return replacements
def test_escaped_str_to_bytes(): assert strutils.escaped_str_to_bytes("foo") == b"foo" assert strutils.escaped_str_to_bytes("\x08") == b"\b" assert strutils.escaped_str_to_bytes("&!?=\\\\)") == br"&!?=\)" assert strutils.escaped_str_to_bytes(u"\\x08") == b"\b" assert strutils.escaped_str_to_bytes(u"&!?=\\\\)") == br"&!?=\)" assert strutils.escaped_str_to_bytes(u"\u00fc") == b'\xc3\xbc' if six.PY2: with tutils.raises(ValueError): strutils.escaped_str_to_bytes(42) else: with tutils.raises(ValueError): strutils.escaped_str_to_bytes(b"very byte")
def __init__(self, expr): self.expr = expr if self.is_binary: expr = strutils.escaped_str_to_bytes(expr) try: self.re = re.compile(expr, self.flags) except: raise ValueError("Cannot compile expression.")
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. """ if isinstance(pattern, six.text_type): pattern = strutils.escaped_str_to_bytes(pattern) if isinstance(repl, six.text_type): repl = strutils.escaped_str_to_bytes(repl) c = super(Request, self).replace(pattern, repl, flags) self.path, pc = re.subn(pattern, repl, self.data.path, flags=flags) c += pc return c
def test_escaped_str_to_bytes(): assert strutils.escaped_str_to_bytes("foo") == b"foo" assert strutils.escaped_str_to_bytes("\x08") == b"\b" assert strutils.escaped_str_to_bytes("&!?=\\\\)") == br"&!?=\)" assert strutils.escaped_str_to_bytes("ü") == b'\xc3\xbc' assert strutils.escaped_str_to_bytes(u"\\x08") == b"\b" assert strutils.escaped_str_to_bytes(u"&!?=\\\\)") == br"&!?=\)" assert strutils.escaped_str_to_bytes(u"ü") == b'\xc3\xbc'
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. """ if isinstance(pattern, six.text_type): pattern = strutils.escaped_str_to_bytes(pattern) if isinstance(repl, six.text_type): repl = strutils.escaped_str_to_bytes(repl) replacements = 0 if self.content: self.content, replacements = re.subn( pattern, repl, self.content, flags=flags ) replacements += self.headers.replace(pattern, repl, flags) return replacements
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. """ if isinstance(pattern, six.text_type): pattern = strutils.escaped_str_to_bytes(pattern) if isinstance(repl, six.text_type): repl = strutils.escaped_str_to_bytes(repl) c = super(Request, self).replace(pattern, repl, flags) self.path, pc = re.subn( pattern, repl, self.data.path, flags=flags ) c += pc return c
def get_data(self) -> bytes: txt = self._w.get_text()[0].strip() try: return strutils.escaped_str_to_bytes(txt) except ValueError: signals.status_message.send( self, message="Invalid Python-style string encoding.", expire=1000 ) raise
def get_data(self): # type: () -> bytes txt = self._w.get_text()[0].strip() try: return strutils.escaped_str_to_bytes(txt) except ValueError: signals.status_message.send( self, message="Invalid Python-style string encoding.", expire=1000 ) raise
def read_file(filename: str, callback: Callable[..., None], escaped: bool) -> Optional[str]: if not filename: return filename = os.path.expanduser(filename) try: with open(filename, "r" if escaped else "rb") as f: d = f.read() except IOError as v: return str(v) if escaped: try: d = strutils.escaped_str_to_bytes(d) except ValueError: return "Invalid Python-style string encoding." # TODO: Refactor the status_prompt_path signal so that we # can raise exceptions here and return the content instead. callback(d)
def read_file(filename, callback, escaped): # type: (str, Callable[...,None], bool) -> Optional[str] if not filename: return filename = os.path.expanduser(filename) try: with open(filename, "r" if escaped else "rb") as f: d = f.read() except IOError as v: return str(v) if escaped: try: d = strutils.escaped_str_to_bytes(d) except ValueError: return "Invalid Python-style string encoding." # TODO: Refactor the status_prompt_path signal so that we # can raise exceptions here and return the content instead. callback(d)
def __init__(self, val): self.val = strutils.escaped_str_to_bytes(val)