Skip to content

Commit

Permalink
Merge 9f3a5a7 into d8f9219
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalHaluza committed Aug 27, 2021
2 parents d8f9219 + 9f3a5a7 commit e375faa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
frozendict
frozenlist2
PyYAML
requests
36 changes: 32 additions & 4 deletions src/cdn_definitions/_impl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,44 @@
import yaml
from urllib3.util.retry import Retry

# It is preferred to return the data as immutable when possible
try:
from frozendict import frozendict
except ImportError:
# If frozendict is not available, fall back to builtin dict
frozendict = dict
try:
from frozenlist2 import frozenlist
except ImportError:
# If frozenlist is not available, fall back to builtin list
frozenlist = list


def freeze(node):
"""
Converts dict to frozendict and list to frozenlist.
"""
if isinstance(node, list):
for index in range(len(node)):
node[index] = freeze(node[index])
return frozenlist(node)
if isinstance(node, dict):
for key, value in node.items():
node[key] = freeze(value)
return frozendict(node)
return node


def parsed(data, ext):
"""
Parses a JSON or YAML string or a requests.Response object.
"""
is_response = isinstance(data, requests.Response)
if ext.lower() == ".json":
return data.json() if isinstance(data, requests.Response) else json.loads(data)
return yaml.load(
data.text if isinstance(data, requests.Response) else data, yaml.SafeLoader
)
parsed_data = data.json() if is_response else json.loads(data)
else:
parsed_data = yaml.safe_load(data.text if is_response else data)
return freeze(parsed_data)


def get_remote_data(url, session=None):
Expand Down
15 changes: 10 additions & 5 deletions tests/test_load.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
import os
import json

from frozendict import frozendict
from frozenlist2 import frozenlist
import pytest
import requests
import requests_mock

from cdn_definitions import load_data, load_schema


def test_can_load_custom_json_data(monkeypatch, tmpdir):
json_file = tmpdir.join("myfile.json")
json_file.write('{"hello": "json"}')
json_file.write('{"hello": ["from", "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": "json"}
assert data == {"hello": ["from", "json"]}
assert isinstance(data, frozendict)
assert isinstance(data["hello"], frozenlist)


def test_can_load_custom_yaml_data(monkeypatch, tmpdir):
yaml_file = tmpdir.join("myfile.yaml")
yaml_file.write("hello: yaml")
yaml_file.write("hello:\n - from\n - 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"}
assert data == {"hello": ["from", "yaml"]}
assert isinstance(data, frozendict)
assert isinstance(data["hello"], frozenlist)


@pytest.mark.parametrize(
Expand Down

0 comments on commit e375faa

Please sign in to comment.