Skip to content

Commit

Permalink
allow ruamel.yaml 0.18 (#739)
Browse files Browse the repository at this point in the history
* allow ruamel.yaml 0.18

* pre-commit

* use string plugin to enable dump_to_str-

* create utility function instead

* add news
  • Loading branch information
jaimergp authored Jan 8, 2024
1 parent 764ba8a commit e402255
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
9 changes: 5 additions & 4 deletions constructor/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
from functools import partial
from os.path import dirname

from ruamel import yaml
from ruamel.yaml import YAMLError

from constructor.exceptions import UnableToParse, UnableToParseMissingJinja2, YamlParsingError
from constructor.utils import yaml

# list of tuples (key name, required, type, description)
KEYS = [
Expand Down Expand Up @@ -694,16 +695,16 @@ def select_lines(data, namespace):
def yamlize(data, directory, content_filter):
data = content_filter(data)
try:
return yaml.safe_load(data)
except yaml.error.YAMLError as e:
return yaml.load(data)
except YAMLError as e:
if ('{{' not in data) and ('{%' not in data):
raise UnableToParse(original=e)
try:
from constructor.jinja import render_jinja
except ImportError as ex:
raise UnableToParseMissingJinja2(original=ex)
data = render_jinja(data, directory, content_filter)
return yaml.load(data, Loader=yaml.SafeLoader)
return yaml.load(data)


def parse(path, platform):
Expand Down
14 changes: 12 additions & 2 deletions constructor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
import math
import re
import sys
from io import StringIO
from os import sep, unlink
from os.path import basename, isdir, isfile, islink, join, normpath
from shutil import rmtree
from subprocess import check_call, check_output

from ruamel import yaml
from ruamel.yaml import YAML

logger = logging.getLogger(__name__)
yaml = YAML(typ="rt")
yaml.default_flow_style = False
yaml.indent(mapping=2, sequence=4, offset=2)


def explained_check_call(args):
Expand All @@ -35,6 +39,12 @@ def filename_dist(dist):
return dist


def yaml_to_string(data):
blob = StringIO()
yaml.dump(data, blob)
return blob.getvalue()


def fill_template(data, d, exceptions=[]):
pat = re.compile(r'__(\w+)__')

Expand Down Expand Up @@ -117,7 +127,7 @@ def add_condarc(info):
if channel_alias:
condarc['channel_alias'] = channel_alias
if isinstance(condarc, dict):
condarc = yaml.dump(condarc, default_flow_style=False)
condarc = yaml_to_string(condarc)
yield '# ----- add condarc'
if info['_platform'].startswith('win'):
yield 'Var /Global CONDARC'
Expand Down
2 changes: 1 addition & 1 deletion dev/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ channels:
dependencies:
- python
- conda >=4.6
- ruamel.yaml >=0.11.14,<0.18
- ruamel.yaml >=0.11.14,<0.19
- conda-standalone # >=23.11.0
- pillow >=3.1 # [osx or win]
19 changes: 19 additions & 0 deletions news/739-ruamel-yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* Add support for `ruamel.yaml` 0.18 API. (#729 via #739)

### Bug fixes

* <news item>

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
2 changes: 1 addition & 1 deletion recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ requirements:
run:
- conda >=4.6
- python
- ruamel.yaml >=0.11.14,<0.18
- ruamel.yaml >=0.11.14,<0.19
- conda-standalone
- pillow >=3.1 # [win or osx]
- nsis >=3.08 # [win]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
install_requires=[
"conda >=4.6",
"ruamel.yaml >=0.11.14,<0.18",
"ruamel.yaml >=0.11.14,<0.19",
"pillow >=3.1 ; platform_system=='Windows' or platform_system=='Darwin'",
# non-python dependency: "nsis >=3.08 ; platform_system=='Windows'",
],
Expand Down

0 comments on commit e402255

Please sign in to comment.