diff options
author | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-09-04 14:09:18 -0400 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-09-04 14:16:37 -0400 |
commit | 32ea8e4f305d5968f50df59798e6eb0342b05bc2 (patch) | |
tree | cc965cc9f6437e5435a085e7526bbae2b065a6e6 | |
parent | 654dedb2c159c1820818a4428cccabdcc1a11bf5 (diff) | |
download | buildstream-32ea8e4f305d5968f50df59798e6eb0342b05bc2.tar.gz |
_yaml.py: Make _yaml.dump() optionally dump to stdout
-rw-r--r-- | buildstream/_yaml.py | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py index f9d49343d..2a1073fa9 100644 --- a/buildstream/_yaml.py +++ b/buildstream/_yaml.py @@ -18,9 +18,11 @@ # Authors: # Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> +import sys import collections import copy from enum import Enum +from contextlib import ExitStack from ruamel import yaml from ruamel.yaml.representer import SafeRepresenter, RoundTripRepresenter @@ -227,10 +229,12 @@ def load(filename, shortname=None, copy_tree=False): # node (dict): A node previously loaded with _yaml.load() above # filename (str): The YAML file to load # -# -def dump(node, filename): - - with open(filename, 'w') as f: +def dump(node, filename=None): + with ExitStack() as stack: + if filename: + f = stack.enter_context(open(filename, 'w')) + else: + f = sys.stdout yaml.round_trip_dump(node, f) |