Skip to content

Commit

Permalink
update ruamel parsers to be same as with old version of ruamel
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyStegeman committed Dec 4, 2024
1 parent 3c9cd0a commit 5660c29
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 24 deletions.
11 changes: 5 additions & 6 deletions src/reportengine/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
import json
from copy import copy

import ruamel.yaml
from ruamel.yaml import YAMLError

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

log = logging.getLogger(__name__)
yaml=ruamel.yaml.YAML(typ='safe')

_config_token = 'parse_'
_produce_token = 'produce_'
Expand Down Expand Up @@ -801,10 +800,10 @@ def __contains__(self, item):
@classmethod
def from_yaml(cls, o, *args, **kwargs):
try:
return cls(yaml.load(o), *args, **kwargs)
except ruamel.yaml.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)
yaml_rt.dump(self.lockfile, stream=f)
13 changes: 8 additions & 5 deletions src/reportengine/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"""
from __future__ import generator_stop

import os
import os.path as osp
import logging
import subprocess
Expand All @@ -41,7 +40,6 @@

import dask.distributed
import jinja2
import ruamel.yaml

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

from . utils import yaml_rt

log = logging.getLogger(__name__)
yaml=ruamel.yaml.YAML(typ='safe')


__all__ = ('report', 'Config')
Expand Down Expand Up @@ -214,7 +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')
yaml.dump(meta, f)
#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: 4 additions & 4 deletions src/reportengine/templateparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from collections import namedtuple
import logging

import ruamel.yaml
from ruamel.yaml import YAMLError

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

log = logging.getLogger(__name__)
yaml=ruamel.yaml.YAML(typ='safe')


#TODO: Do a real tokenizer/lexer/parser? Would avoid having r'\s*?'
Expand Down Expand Up @@ -45,8 +45,8 @@ def parse_assignments(args):
k = m.group(1)
vstring = m.group(2)
try:
v = yaml.load(vstring)
except ruamel.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
6 changes: 2 additions & 4 deletions src/reportengine/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import ruamel.yaml

from reportengine import app
from reportengine.tests.utils import tmp

yaml=ruamel.yaml.YAML(typ='safe')
from reportengine.utils import yaml_safe


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

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

Expand Down
7 changes: 2 additions & 5 deletions src/reportengine/tests/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
from collections import OrderedDict
import unittest

import ruamel.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)

yaml=ruamel.yaml.YAML(typ='safe')

class BaseConfig(Config):

Expand Down Expand Up @@ -196,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.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 5660c29

Please sign in to comment.