diff --git a/anaconda_project/env_spec.py b/anaconda_project/env_spec.py index aaff7b8b..f78fdacb 100644 --- a/anaconda_project/env_spec.py +++ b/anaconda_project/env_spec.py @@ -19,7 +19,7 @@ from anaconda_project.internal.py2_compat import is_string from anaconda_project import conda_manager -from anaconda_project.yaml_file import _load_string, _save_file, _YAMLError, ryaml +from anaconda_project.yaml_file import _load_string, _save_file, _YAMLError class EnvSpec(object): @@ -402,12 +402,11 @@ def to_json(self): channels = list(self._channels) platforms = list(self._platforms) - # this is a gross, roundabout hack to get ryaml dicts that + # this is a gross, roundabout hack to get ruamel.yaml dicts that # have ordering... OrderedDict doesn't work because the # yaml saver saves them as some "!!omap" nonsense. Other # than ordering, the formatting isn't even preserved here. - template_json = ryaml.load("something:\n description: null\n" + " packages: []\n" + " channels: []\n", - Loader=ryaml.RoundTripLoader) + template_json = _load_string("something:\n description: null\n" + " packages: []\n" + " channels: []\n") if self._description is not None: template_json['something']['description'] = self._description @@ -474,7 +473,7 @@ def save_environment_yml(self, filename): packages.append(dict(pip=pip_packages)) channels = list(self.channels) - yaml = ryaml.load("name: " "\ndependencies: []\nchannels: []\n", Loader=ryaml.RoundTripLoader) + yaml = _load_string("name: " "\ndependencies: []\nchannels: []\n") assert self.name is not None # the global anonymous spec can't be saved yaml['name'] = self.name diff --git a/anaconda_project/yaml_file.py b/anaconda_project/yaml_file.py index 4736f34a..cb01046d 100644 --- a/anaconda_project/yaml_file.py +++ b/anaconda_project/yaml_file.py @@ -12,22 +12,15 @@ # comments, etc., which is why we use it instead of the usual yaml # module. Remember the project file is intended to go into source # control. -try: - # this is the conda-packaged version of ruamel.yaml which has the - # module renamed - import ruamel_yaml as ryaml - from ruamel_yaml.error import YAMLError - from ruamel_yaml.comments import CommentedMap - from ruamel_yaml.comments import CommentedSeq -except ImportError: # pragma: no cover - # this is the upstream version - import ruamel.yaml as ryaml # pragma: no cover - from ruamel.yaml.error import YAMLError # pragma: no cover - from ruamel.yaml.comments import CommentedMap # pragma: no cover - from ruamel.yaml.comments import CommentedSeq # pragma: no cover +import ruamel.yaml +import ruamel.yaml.constructor +from ruamel.yaml.error import YAMLError # pragma: no cover +from ruamel.yaml.comments import CommentedMap # pragma: no cover +from ruamel.yaml.comments import CommentedSeq # pragma: no cover import codecs import errno +import io import os import sys import uuid @@ -57,20 +50,26 @@ def _atomic_replace(path, contents, encoding='utf-8'): pass +def _get_yaml(): + return ruamel.yaml.YAML(typ="rt") + + def _load_string(contents): if contents.strip() == '': - # ryaml.load below returns None for an empty file, we want + # YAML.load below returns None for an empty file, we want # to return an empty dict instead. return {} else: # using RoundTripLoader incorporates safe_load # (we don't load code) - assert issubclass(ryaml.RoundTripLoader, ryaml.constructor.SafeConstructor) - return ryaml.load(contents, Loader=ryaml.RoundTripLoader) + assert issubclass(ruamel.yaml.RoundTripLoader, ruamel.yaml.constructor.SafeConstructor) + return _get_yaml().load(contents) def _dump_string(yaml): - return ryaml.dump(yaml, Dumper=ryaml.RoundTripDumper) + stream = io.StringIO() + _get_yaml().dump(yaml, stream=stream) + return stream.getvalue() def _save_file(yaml, filename, contents=None): @@ -79,7 +78,7 @@ def _save_file(yaml, filename, contents=None): try: # This is to ensure we don't corrupt the file, even if ruamel.yaml is broken - ryaml.load(contents, Loader=ryaml.RoundTripLoader) + _get_yaml().load(contents) except YAMLError as e: # pragma: no cover (should not happen) print("ruamel.yaml bug; it failed to parse a file that it generated.", file=sys.stderr) print(" the parse error was: " + str(e), file=sys.stderr) diff --git a/conda.recipe/meta.yaml b/conda.recipe/meta.yaml index ad837bc1..9c7751de 100644 --- a/conda.recipe/meta.yaml +++ b/conda.recipe/meta.yaml @@ -26,7 +26,7 @@ requirements: - conda-pack - python - requests - - ruamel_yaml + - ruamel.yaml - tornado >=4.2 - jinja2 - tqdm diff --git a/environment.yml b/environment.yml index 05feb49d..c3447d2d 100644 --- a/environment.yml +++ b/environment.yml @@ -7,7 +7,7 @@ channels: dependencies: - redis - notebook - - ruamel_yaml + - ruamel.yaml - anaconda-client - conda-pack - requests diff --git a/setup.py b/setup.py index a5c7a360..b71c935b 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ "anaconda-client", "conda-pack", "requests", - "ruamel_yaml", + "ruamel.yaml", "tornado>=4.2", "jinja2", "tqdm",