Skip to content

Commit 46d7dc1

Browse files
authored
Merge pull request #67 from NNPDF/update_yaml
address deprecation of ruamel.yaml functions
2 parents 0c97834 + 64d7791 commit 46d7dc1

File tree

9 files changed

+33
-41
lines changed

9 files changed

+33
-41
lines changed

conda-recipe/meta.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ requirements:
1717
run:
1818
- python
1919
- jinja2
20-
- ruamel_yaml =0.15
20+
- ruamel.yaml >=0.15
2121
- matplotlib
2222
- pandas >=1
2323
- pygments

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ classifiers = [
1919
description-file="README.md"
2020
requires = [
2121
"jinja2",
22-
"ruamel_yaml<0.18", # the code is not compatible with ruamel 0.18
22+
"ruamel.yaml>=0.15",
2323
"matplotlib",
2424
"pandas",
2525
"pygments",

src/reportengine/compat.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/reportengine/configparser.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import json
1414
from copy import copy
1515

16+
from ruamel.yaml import YAMLError
1617

17-
from reportengine.compat import yaml
1818
from reportengine import namespaces
19-
from reportengine.utils import ChainMap, get_classmembers
19+
from reportengine.utils import ChainMap, get_classmembers, yaml_rt
2020
from reportengine import templateparser
2121
from reportengine.baseexceptions import ErrorWithAlternatives, AsInputError
2222

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

807807
def dump_lockfile(self):
808808
with open(self.environment.input_folder/"lockfile.yaml", "w+") as f:
809-
yaml.dump(self.lockfile, stream=f, Dumper=yaml.RoundTripDumper)
809+
yaml_rt.dump(self.lockfile, stream=f)

src/reportengine/report.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,14 @@
3232
from __future__ import generator_stop
3333

3434
import os
35-
import os.path as osp
3635
import logging
3736
import subprocess
3837
import shutil
3938
from collections import UserList
4039
import pathlib
4140

41+
import dask.distributed
4242
import jinja2
43-
from reportengine.compat import yaml
44-
4543

4644
from . import configparser
4745
from . resourcebuilder import target_map
@@ -52,18 +50,18 @@
5250
from . import styles
5351
from . import filefinder
5452
from . import floatformatting
55-
56-
import dask.distributed
53+
from . utils import yaml_rt
5754

5855
log = logging.getLogger(__name__)
5956

57+
6058
__all__ = ('report', 'Config')
6159

6260

6361
def _process_template_text(source, *, filename=None):
6462
if filename:
6563
#PY36
66-
log.debug("Processing template %s" % osp.abspath(str(filename)))
64+
log.debug("Processing template %s" % os.path.abspath(str(filename)))
6765

6866
root = {}
6967
d = root
@@ -111,7 +109,7 @@ class JinjaEnv(jinja2.Environment):
111109

112110
def preprocess(self, source, name=None, filename=None):
113111
if filename:
114-
log.debug("Processing template %s" % osp.abspath(filename))
112+
log.debug("Processing template %s" % os.path.abspath(filename))
115113

116114
root = {}
117115
d = root
@@ -213,12 +211,13 @@ def meta_file(output_path, meta:(dict, type(None))=None):
213211
path = output_path/fname
214212
with open(path, 'w') as f:
215213
f.write('\n')
216-
#Using round_trip_dump is important here because the input dict may
217-
#be a recursive commented map, which yaml.dump (or safe_dumo) doesn't
218-
#know how to
219-
#process correctly.
220-
yaml.round_trip_dump(meta, f, explicit_start=True, explicit_end=True,
221-
default_flow_style=False)
214+
#Using round_trip_dump is important here because the input dict may be a
215+
#recursive commented map, which yaml.dump (or safe_dumo) doesn't know
216+
#how to process correctly.
217+
yaml_rt.explicit_start=True
218+
yaml_rt.explicit_end=True
219+
yaml_rt.default_flow_style=False
220+
yaml_rt.dump(meta, f)
222221
return fname
223222

224223
def pandoc_template(*, templatename='report.template', output_path):

src/reportengine/templateparser.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
from collections import namedtuple
1010
import logging
1111

12-
from reportengine.compat import yaml
12+
from ruamel.yaml import YAMLError
13+
14+
from reportengine.utils import yaml_safe
1315
from reportengine.targets import FuzzyTarget
1416

1517
log = logging.getLogger(__name__)
@@ -43,8 +45,8 @@ def parse_assignments(args):
4345
k = m.group(1)
4446
vstring = m.group(2)
4547
try:
46-
v = yaml.safe_load(vstring)
47-
except yaml.YamlError:
48+
v = yaml_safe.load(vstring)
49+
except YAMLError:
4850
raise ValueError(f"Couldn't process assignment value '{vstring}'")
4951
res.append((k, v))
5052
else:

src/reportengine/tests/test_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import pytest
88

99
from reportengine import app
10+
from reportengine.utils import yaml_safe
1011
from reportengine.tests.utils import tmp
11-
from reportengine.compat import yaml
1212

1313
runcard =\
1414
"""
@@ -58,7 +58,7 @@ def test_app_runs(tmp):
5858

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

src/reportengine/tests/test_configparser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
from collections import OrderedDict
99
import unittest
1010

11-
from reportengine.compat import yaml
12-
from reportengine.utils import ChainMap
11+
from reportengine.utils import ChainMap, yaml_safe
1312
from reportengine import namespaces
1413
from reportengine.configparser import (Config, BadInputType, element_of,
1514
named_element_of, ConfigError)
1615

16+
1717
class BaseConfig(Config):
1818

1919
@element_of('ys')
@@ -193,7 +193,7 @@ def test_rewrite_actions():
193193
c = BaseConfig(inp)
194194
r = c.parse_actions_(inp['actions_'])
195195
suggested_yaml = c._rewrite_old_actions(r)
196-
newacts = yaml.safe_load(suggested_yaml)
196+
newacts = yaml_safe.load(suggested_yaml)
197197
newr = c.parse_actions_(newacts['actions_'])
198198
assert newr == r
199199

src/reportengine/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import re
1010
import importlib.util
1111
import pathlib
12+
from ruamel.yaml import YAML
13+
14+
yaml_rt = YAML(typ="rt")
15+
yaml_safe = YAML(typ="safe")
1216

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

0 commit comments

Comments
 (0)