Consolidate some functions to strutils.bytes_to_escaped_str

This commit is contained in:
Aldo Cortesi 2016-06-02 12:53:12 +12:00
parent 3d9dbd6a49
commit 31012d782f
4 changed files with 6 additions and 29 deletions

View File

@ -3,8 +3,9 @@ import pprint
import io
import copy
from flask import Flask, jsonify, render_template, request, abort, make_response
from . import version, language, utils
from . import version, language
from netlib.http import user_agents
from netlib import strutils
logging.basicConfig(level="DEBUG")
EXAMPLE_HOST = "example.com"
@ -166,7 +167,7 @@ def make_app(noapi, debug):
settings.websocket_key = EXAMPLE_WEBSOCKET_KEY
language.serve(safe, s, settings)
args["output"] = utils.escape_unprintables(s.getvalue())
args["output"] = strutils.bytes_to_escaped_str(s.getvalue())
return render(template, False, **args)
@app.route('/response_preview')

View File

@ -18,10 +18,11 @@ from netlib.exceptions import HttpException, TcpDisconnect, TcpTimeout, TlsExcep
NetlibException
from netlib.http import http1, http2
from . import utils, log, language
from . import log, language
import logging
from netlib.tutils import treq
from netlib import strutils
logging.getLogger("hpack").setLevel(logging.WARNING)
@ -427,7 +428,7 @@ class Pathoc(tcp.TCPClient):
finally:
if resp:
lg("<< %s %s: %s bytes" % (
resp.status_code, xrepr(resp.reason), len(resp.content)
resp.status_code, strutils.bytes_to_escaped_str(resp.reason), len(resp.content)
))
if resp.status_code in self.ignorecodes:
lg.suppress()

View File

@ -2,8 +2,6 @@ import os
import sys
import netlib.utils
from netlib import strutils
class MemBool(object):
@ -28,18 +26,6 @@ def parse_anchor_spec(s):
return tuple(s.split("=", 1))
def escape_unprintables(s):
"""
Like inner_repr, but preserves line breaks.
"""
s = s.replace(b"\r\n", b"PATHOD_MARKER_RN")
s = s.replace(b"\n", b"PATHOD_MARKER_N")
s = strutils.bytes_to_escaped_str(s)
s = s.replace("PATHOD_MARKER_RN", "\n")
s = s.replace("PATHOD_MARKER_N", "\n")
return s
data = netlib.utils.Data(__name__)

View File

@ -1,8 +1,6 @@
from pathod import utils
import tutils
import six
def test_membool():
m = utils.MemBool()
@ -20,12 +18,3 @@ def test_parse_anchor_spec():
def test_data_path():
tutils.raises(ValueError, utils.data.path, "nonexistent")
def test_escape_unprintables():
s = bytes(range(256))
if six.PY2:
s = "".join([chr(i) for i in range(255)])
e = utils.escape_unprintables(s)
assert e.encode('ascii')
assert "PATHOD_MARKER" not in e