Skip to content

Commit

Permalink
Switch to upstream ruamel.yaml package
Browse files Browse the repository at this point in the history
Avoid usage of deprecated top-level ruamel.yaml load/dump functions.
Those have been removed in ruamel.yaml>=0.18 .

Signed-off-by: Marcel Bargull <marcel.bargull@udo.edu>
  • Loading branch information
mbargull committed Nov 1, 2023
1 parent 6bc61a5 commit ad7bd02
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
9 changes: 4 additions & 5 deletions anaconda_project/env_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
35 changes: 17 additions & 18 deletions anaconda_project/yaml_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ requirements:
- conda-pack
- python
- requests
- ruamel_yaml
- ruamel.yaml
- tornado >=4.2
- jinja2
- tqdm
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ channels:
dependencies:
- redis
- notebook
- ruamel_yaml
- ruamel.yaml
- anaconda-client
- conda-pack
- requests
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"anaconda-client",
"conda-pack",
"requests",
"ruamel_yaml",
"ruamel.yaml",
"tornado>=4.2",
"jinja2",
"tqdm",
Expand Down

0 comments on commit ad7bd02

Please sign in to comment.