Minor tweaks

This commit is contained in:
Aldo Cortesi 2016-12-08 10:18:50 +13:00
parent a617e3b5f7
commit b2695dbc6a
4 changed files with 21 additions and 19 deletions

View File

@ -180,12 +180,13 @@ class OptManager(metaclass=_DefaultsMeta):
Save to path. If the destination file exists, modify it in-place.
"""
if os.path.exists(path) and os.path.isfile(path):
data = open(path, "r").read()
with open(path, "r") as f:
data = f.read()
else:
data = ""
data = self.serialize(data, defaults)
fp = open(path, "w")
fp.write(data)
with open(path, "w") as f:
f.write(data)
def serialize(self, text, defaults=False):
"""
@ -228,8 +229,7 @@ class OptManager(metaclass=_DefaultsMeta):
this object. May raise OptionsError if the config file is invalid.
"""
data = self._load(text)
for k, v in data.items():
setattr(self, k, v)
self.update(**data)
def load_paths(self, *paths):
"""
@ -240,7 +240,8 @@ class OptManager(metaclass=_DefaultsMeta):
for p in paths:
p = os.path.expanduser(p)
if os.path.exists(p) and os.path.isfile(p):
txt = open(p, "r").read()
with open(p, "r") as f:
txt = f.read()
self.load(txt)
def __repr__(self):

View File

@ -513,6 +513,7 @@ def proxy_ssl_options(parser):
'as the first entry. Can be passed multiple times.')
group.add_argument(
"--ciphers-client", action="store",
type=str, dest="ciphers_client",
help="Set supported ciphers for client connections. (OpenSSL Syntax)"
)
group.add_argument(

View File

@ -31,37 +31,37 @@ def test_simple():
sio = io.StringIO()
d = dumper.Dumper(sio)
with taddons.context(options=dump.Options()) as ctx:
ctx.configure(d, flow_detail = 0)
ctx.configure(d, flow_detail=0)
d.response(tflow.tflow(resp=True))
assert not sio.getvalue()
sio.truncate(0)
ctx.configure(d, flow_detail = 1)
ctx.configure(d, flow_detail=1)
d.response(tflow.tflow(resp=True))
assert sio.getvalue()
sio.truncate(0)
ctx.configure(d, flow_detail = 1)
ctx.configure(d, flow_detail=1)
d.error(tflow.tflow(err=True))
assert sio.getvalue()
sio.truncate(0)
ctx.configure(d, flow_detail = 4)
ctx.configure(d, flow_detail=4)
d.response(tflow.tflow(resp=True))
assert sio.getvalue()
sio.truncate(0)
ctx.configure(d, flow_detail = 4)
ctx.configure(d, flow_detail=4)
d.response(tflow.tflow(resp=True))
assert "<<" in sio.getvalue()
sio.truncate(0)
ctx.configure(d, flow_detail = 4)
ctx.configure(d, flow_detail=4)
d.response(tflow.tflow(err=True))
assert "<<" in sio.getvalue()
sio.truncate(0)
ctx.configure(d, flow_detail = 4)
ctx.configure(d, flow_detail=4)
flow = tflow.tflow()
flow.request = tutils.treq()
flow.request.stickycookie = True
@ -74,7 +74,7 @@ def test_simple():
assert sio.getvalue()
sio.truncate(0)
ctx.configure(d, flow_detail = 4)
ctx.configure(d, flow_detail=4)
flow = tflow.tflow(resp=tutils.tresp(content=b"{"))
flow.response.headers["content-type"] = "application/json"
flow.response.status_code = 400
@ -82,7 +82,7 @@ def test_simple():
assert sio.getvalue()
sio.truncate(0)
ctx.configure(d, flow_detail = 4)
ctx.configure(d, flow_detail=4)
flow = tflow.tflow()
flow.request.content = None
flow.response = http.HTTPResponse.wrap(tutils.tresp())
@ -100,7 +100,7 @@ def test_echo_body():
sio = io.StringIO()
d = dumper.Dumper(sio)
with taddons.context(options=dump.Options()) as ctx:
ctx.configure(d, flow_detail = 3)
ctx.configure(d, flow_detail=3)
d._echo_message(f.response)
t = sio.getvalue()
assert "cut off" in t
@ -110,7 +110,7 @@ def test_echo_request_line():
sio = io.StringIO()
d = dumper.Dumper(sio)
with taddons.context(options=dump.Options()) as ctx:
ctx.configure(d, flow_detail = 3, showhost = True)
ctx.configure(d, flow_detail=3, showhost=True)
f = tflow.tflow(client_conn=None, server_conn=True, resp=True)
f.request.is_replay = True
d._echo_request_line(f)
@ -146,7 +146,7 @@ def test_tcp():
sio = io.StringIO()
d = dumper.Dumper(sio)
with taddons.context(options=dump.Options()) as ctx:
ctx.configure(d, flow_detail = 3, showhost = True)
ctx.configure(d, flow_detail=3, showhost=True)
f = tflow.ttcpflow(client_conn=True, server_conn=True)
d.tcp_message(f)
assert "it's me" in sio.getvalue()

View File

@ -15,7 +15,7 @@ class TO(optmanager.OptManager):
class TD(optmanager.OptManager):
def __init__(self, one="done", two="dtwo", three="error"):
def __init__(self, *, one="done", two="dtwo", three="error"):
self.one = one
self.two = two
self.three = three