Skip to content

Commit

Permalink
Add support for YAML data files in load_data [RHELDST-4904]
Browse files Browse the repository at this point in the history
The load_data method will accept both YAML and JSON files, but will
load the data.yaml file by default.

This change enables cdn-definitions-private to cease maintaining
two sets of data, in both YAML and JSON forms -- only data.yaml
files will be provided and maintained. Those who wish to use
the cdn-definitions-private data will be able to do so without
a manual conversion of the YAML data file into a JSON file.
  • Loading branch information
crungehottman committed Feb 2, 2021
1 parent 614467e commit 34e906f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PyYAML
pathlib
12 changes: 9 additions & 3 deletions src/cdn_definitions/_impl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import json
import pathlib
import os
import yaml


def load_data():
# Load data from first existing of these
candidate_paths = [
os.path.join(os.path.dirname(os.path.dirname(__file__)), "data.json"),
"/usr/share/cdn-definitions/data.json",
os.path.join(os.path.dirname(os.path.dirname(__file__)), "data.yaml"),
"/usr/share/cdn-definitions/data.yaml",
]

# If env var is set, it takes highest precedence
if "CDN_DEFINITIONS_PATH" in os.environ:
candidate_paths.insert(0, os.environ["CDN_DEFINITIONS_PATH"])

existing_paths = [p for p in candidate_paths if os.path.exists(p)]
return json.load(open(existing_paths[0]))

if (pathlib.Path(existing_paths[0]).suffix).lower() == ".json":
return json.load(open(existing_paths[0]))

return yaml.load(open(existing_paths[0]), yaml.SafeLoader)


DATA = load_data()
Expand Down
1 change: 0 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pytest
jsonschema
PyYAML
18 changes: 15 additions & 3 deletions tests/test_load.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
from cdn_definitions._impl import load_data


def test_can_load_custom_data(monkeypatch, tmpdir):
def test_can_load_custom_json_data(monkeypatch, tmpdir):
json_file = tmpdir.join("myfile.json")
json_file.write('{"hello": "world"}')
json_file.write('{"hello": "json"}')

# If we set an env var pointing at the above JSON file, the library
# should load it instead of the bundled data
monkeypatch.setenv("CDN_DEFINITIONS_PATH", str(json_file))
data = load_data()

assert data == {"hello": "world"}
assert data == {"hello": "json"}


def test_can_load_custom_yaml_data(monkeypatch, tmpdir):
yaml_file = tmpdir.join("myfile.yaml")
yaml_file.write("hello: yaml")

# If we set an env var pointing at the above YAML file, the library
# should load it instead of the bundled data
monkeypatch.setenv("CDN_DEFINITIONS_PATH", str(yaml_file))
data = load_data()

assert data == {"hello": "yaml"}
8 changes: 7 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
envlist = py27,py38,static,docs

[testenv]
deps=-rtest-requirements.txt
deps=
-rrequirements.txt
-rtest-requirements.txt
commands=pytest -v {posargs}
whitelist_externals=sh

[testenv:static]
deps=
-rrequirements.txt
-rtest-requirements.txt
black
pylint
Expand All @@ -17,6 +20,7 @@ commands=

[testenv:cov]
deps=
-rrequirements.txt
-rtest-requirements.txt
pytest-cov
usedevelop=true
Expand All @@ -26,6 +30,7 @@ commands=
[testenv:cov-travis]
passenv = TRAVIS TRAVIS_*
deps=
-rrequirements.txt
-rtest-requirements.txt
pytest-cov
coveralls
Expand All @@ -36,6 +41,7 @@ commands=

[testenv:docs]
deps=
-rrequirements.txt
sphinx
alabaster
use_develop=true
Expand Down

0 comments on commit 34e906f

Please sign in to comment.