Skip to content

Commit

Permalink
Merge pull request #67 from NNPDF/update_yaml
Browse files Browse the repository at this point in the history
address deprecation of ruamel.yaml functions
  • Loading branch information
RoyStegeman authored Dec 4, 2024
2 parents 0c97834 + 64d7791 commit 46d7dc1
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 41 deletions.
2 changes: 1 addition & 1 deletion conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ requirements:
run:
- python
- jinja2
- ruamel_yaml =0.15
- ruamel.yaml >=0.15
- matplotlib
- pandas >=1
- pygments
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ classifiers = [
description-file="README.md"
requires = [
"jinja2",
"ruamel_yaml<0.18", # the code is not compatible with ruamel 0.18
"ruamel.yaml>=0.15",
"matplotlib",
"pandas",
"pygments",
Expand Down
13 changes: 0 additions & 13 deletions src/reportengine/compat.py

This file was deleted.

10 changes: 5 additions & 5 deletions src/reportengine/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import json
from copy import copy

from ruamel.yaml import YAMLError

from reportengine.compat import yaml
from reportengine import namespaces
from reportengine.utils import ChainMap, get_classmembers
from reportengine.utils import ChainMap, get_classmembers, yaml_rt
from reportengine import templateparser
from reportengine.baseexceptions import ErrorWithAlternatives, AsInputError

Expand Down Expand Up @@ -800,10 +800,10 @@ def __contains__(self, item):
@classmethod
def from_yaml(cls, o, *args, **kwargs):
try:
return cls(yaml.round_trip_load(o), *args, **kwargs)
except yaml.error.YAMLError as e:
return cls(yaml_rt.load(o), *args, **kwargs)
except YAMLError as e:
raise ConfigError(f"Failed to parse yaml file: {e}")

def dump_lockfile(self):
with open(self.environment.input_folder/"lockfile.yaml", "w+") as f:
yaml.dump(self.lockfile, stream=f, Dumper=yaml.RoundTripDumper)
yaml_rt.dump(self.lockfile, stream=f)
25 changes: 12 additions & 13 deletions src/reportengine/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@
from __future__ import generator_stop

import os
import os.path as osp
import logging
import subprocess
import shutil
from collections import UserList
import pathlib

import dask.distributed
import jinja2
from reportengine.compat import yaml


from . import configparser
from . resourcebuilder import target_map
Expand All @@ -52,18 +50,18 @@
from . import styles
from . import filefinder
from . import floatformatting

import dask.distributed
from . utils import yaml_rt

log = logging.getLogger(__name__)


__all__ = ('report', 'Config')


def _process_template_text(source, *, filename=None):
if filename:
#PY36
log.debug("Processing template %s" % osp.abspath(str(filename)))
log.debug("Processing template %s" % os.path.abspath(str(filename)))

root = {}
d = root
Expand Down Expand Up @@ -111,7 +109,7 @@ class JinjaEnv(jinja2.Environment):

def preprocess(self, source, name=None, filename=None):
if filename:
log.debug("Processing template %s" % osp.abspath(filename))
log.debug("Processing template %s" % os.path.abspath(filename))

root = {}
d = root
Expand Down Expand Up @@ -213,12 +211,13 @@ def meta_file(output_path, meta:(dict, type(None))=None):
path = output_path/fname
with open(path, 'w') as f:
f.write('\n')
#Using round_trip_dump is important here because the input dict may
#be a recursive commented map, which yaml.dump (or safe_dumo) doesn't
#know how to
#process correctly.
yaml.round_trip_dump(meta, f, explicit_start=True, explicit_end=True,
default_flow_style=False)
#Using round_trip_dump is important here because the input dict may be a
#recursive commented map, which yaml.dump (or safe_dumo) doesn't know
#how to process correctly.
yaml_rt.explicit_start=True
yaml_rt.explicit_end=True
yaml_rt.default_flow_style=False
yaml_rt.dump(meta, f)
return fname

def pandoc_template(*, templatename='report.template', output_path):
Expand Down
8 changes: 5 additions & 3 deletions src/reportengine/templateparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from collections import namedtuple
import logging

from reportengine.compat import yaml
from ruamel.yaml import YAMLError

from reportengine.utils import yaml_safe
from reportengine.targets import FuzzyTarget

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -43,8 +45,8 @@ def parse_assignments(args):
k = m.group(1)
vstring = m.group(2)
try:
v = yaml.safe_load(vstring)
except yaml.YamlError:
v = yaml_safe.load(vstring)
except YAMLError:
raise ValueError(f"Couldn't process assignment value '{vstring}'")
res.append((k, v))
else:
Expand Down
4 changes: 2 additions & 2 deletions src/reportengine/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import pytest

from reportengine import app
from reportengine.utils import yaml_safe
from reportengine.tests.utils import tmp
from reportengine.compat import yaml

runcard =\
"""
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_app_runs(tmp):

#Test meta round trip
with open(output_path/'meta.yaml') as f:
meta = yaml.safe_load(f)
meta = yaml_safe.load(f)
assert meta['author'] == "Zahari Kassabov"
assert meta['keywords'] == ["test", "debug"]

Expand Down
6 changes: 3 additions & 3 deletions src/reportengine/tests/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from collections import OrderedDict
import unittest

from reportengine.compat import yaml
from reportengine.utils import ChainMap
from reportengine.utils import ChainMap, yaml_safe
from reportengine import namespaces
from reportengine.configparser import (Config, BadInputType, element_of,
named_element_of, ConfigError)


class BaseConfig(Config):

@element_of('ys')
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_rewrite_actions():
c = BaseConfig(inp)
r = c.parse_actions_(inp['actions_'])
suggested_yaml = c._rewrite_old_actions(r)
newacts = yaml.safe_load(suggested_yaml)
newacts = yaml_safe.load(suggested_yaml)
newr = c.parse_actions_(newacts['actions_'])
assert newr == r

Expand Down
4 changes: 4 additions & 0 deletions src/reportengine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import re
import importlib.util
import pathlib
from ruamel.yaml import YAML

yaml_rt = YAML(typ="rt")
yaml_safe = YAML(typ="safe")

#TODO: Support metaclass attributes?
def get_classmembers(cls, *, predicate=None):
Expand Down

0 comments on commit 46d7dc1

Please sign in to comment.