clean up flow reading

This commit is contained in:
Maximilian Hils 2015-02-05 14:44:45 +01:00
parent 19555bb39a
commit 0ac3227b7b
4 changed files with 48 additions and 35 deletions

View File

@ -499,38 +499,33 @@ class ConsoleMaster(flow.FlowMaster):
self.view_flowlist()
def _readflow(self, paths):
"""
Utitility function that reads a list of flows
or prints an error to the UI if that fails.
Returns
- None, if there was an error.
- a list of flows, otherwise.
"""
try:
flows = []
for path in paths:
path = os.path.expanduser(path)
with file(path, "rb") as f:
flows.extend(list(flow.FlowReader(f).stream()))
except (IOError, flow.FlowReadError), v:
return True, v.strerror
return False, flows
return flow.read_flows_from_paths(paths)
except flow.FlowReadError as e:
if not self.statusbar:
print >> sys.stderr, e.strerror
sys.exit(1)
else:
self.statusbar.message(e.strerror)
return None
def client_playback_path(self, path):
err, ret = self._readflow(path)
if err:
if not self.statusbar:
print >> sys.stderr, ret
sys.exit(1)
else:
self.statusbar.message(ret)
else:
self.start_client_playback(ret, False)
flows = self._readflow(path)
if flows:
self.start_client_playback(flows, False)
def server_playback_path(self, path):
err, ret = self._readflow(path)
if err:
if not self.statusbar:
print >> sys.stderr, ret
sys.exit(1)
else:
self.statusbar.message(ret)
else:
flows = self._readflow(path)
if flows:
self.start_server_playback(
ret,
flows,
self.killextra, self.rheaders,
False, self.nopop,
self.options.replay_ignore_params, self.options.replay_ignore_content, self.options.replay_ignore_payload_params

View File

@ -144,15 +144,14 @@ class DumpMaster(flow.FlowMaster):
self.start_app(self.o.app_host, self.o.app_port)
def _readflow(self, paths):
"""
Utitility function that reads a list of flows
or raises a DumpError if that fails.
"""
try:
flows = []
for path in paths:
path = os.path.expanduser(path)
with file(path, "rb") as f:
flows.extend(list(flow.FlowReader(f).stream()))
except (IOError, flow.FlowReadError), v:
raise DumpError(v.strerror)
return flows
return flow.read_flows_from_paths(paths)
except flow.FlowReadError as e:
raise DumpError(e.strerror)
def add_event(self, e, level="info"):
needed = dict(error=0, info=1, debug=2).get(level, 1)

View File

@ -945,6 +945,25 @@ class FlowMaster(controller.Master):
self.stream = None
def read_flows_from_paths(paths):
"""
Given a list of filepaths, read all flows and return a list of them.
From a performance perspective, streaming would be advisable -
however, if there's an error with one of the files, we want it to be raised immediately.
If an error occurs, a FlowReadError will be raised.
"""
try:
flows = []
for path in paths:
path = os.path.expanduser(path)
with file(path, "rb") as f:
flows.extend(FlowReader(f).stream())
except IOError as e:
raise FlowReadError(e.strerror)
return flows
class FlowWriter:
def __init__(self, fo):
self.fo = fo

View File

@ -75,7 +75,7 @@ class TestDumpMaster:
def test_replay(self):
cs = StringIO()
o = dump.Options(server_replay="nonexistent", kill=True)
o = dump.Options(server_replay=["nonexistent"], kill=True)
tutils.raises(dump.DumpError, dump.DumpMaster, None, o, outfile=cs)
with tutils.tmpdir() as t: