From 6c4dbcc7a7774f7acbad0f1362a94525316d51ff Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Mon, 24 Jul 2017 20:30:14 +0800 Subject: [PATCH] [web] Update OptManager.save() to suit the need of mitmweb. --- mitmproxy/optmanager.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mitmproxy/optmanager.py b/mitmproxy/optmanager.py index e1d74b8e0..830821532 100644 --- a/mitmproxy/optmanager.py +++ b/mitmproxy/optmanager.py @@ -512,13 +512,15 @@ def serialize(opts, text, defaults=False): return ruamel.yaml.round_trip_dump(data) -def save(opts, path, defaults=False): +def save(opts, path=None, defaults=False): """ - Save to path. If the destination file exists, modify it in-place. + If the path is given, save to path, otherwise return the serialized data. + + If the destination file exists, modify it in-place. Raises OptionsError if the existing data is corrupt. """ - if os.path.exists(path) and os.path.isfile(path): + if path and os.path.exists(path) and os.path.isfile(path): with open(path, "rt", encoding="utf8") as f: try: data = f.read() @@ -529,5 +531,9 @@ def save(opts, path, defaults=False): else: data = "" data = serialize(opts, data, defaults) - with open(path, "wt", encoding="utf8") as f: - f.write(data) + + if path: + with open(path, "wt", encoding="utf8") as f: + f.write(data) + else: + return data