Skip to content

Commit

Permalink
Merge 1eea31c into 728891c
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalHaluza authored Sep 7, 2021
2 parents 728891c + 1eea31c commit 87e695b
Show file tree
Hide file tree
Showing 3 changed files with 46 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
38 changes: 34 additions & 4 deletions src/cdn_definitions/_impl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,46 @@
# annotation even though the actual import still works.
from requests.packages.urllib3.util.retry import Retry # pylint: disable=import-error

# It is preferred to return the data as immutable when possible
try:
from frozendict import frozendict
except ImportError: # pragma: no cover
# If frozendict is not available, fall back to builtin dict
frozendict = dict # pylint: disable=invalid-name
try:
from frozenlist2 import frozenlist
except ImportError: # pragma: no cover
# If frozenlist is not available, fall back to builtin list
frozenlist = list # pylint: disable=invalid-name


def freeze(node):
"""
Converts dict to frozendict and list to frozenlist.
"""
if isinstance(node, list):
# Iterating using index instead of enumeration
# so we can replace the list items in place
for index in range(len(node)): # pylint: disable=consider-using-enumerate
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 87e695b

Please sign in to comment.