[web] Update OptManager.save() to suit the need of mitmweb.

This commit is contained in:
Matthew Shao 2017-07-24 20:30:14 +08:00
parent 93cd1562de
commit 6c4dbcc7a7

View File

@ -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