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