Skip to content

Commit

Permalink
Merge dd86a74 into ee15610
Browse files Browse the repository at this point in the history
  • Loading branch information
crungehottman committed Feb 1, 2021
2 parents ee15610 + dd86a74 commit 75f799e
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 22 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
4 changes: 2 additions & 2 deletions src/cdn_definitions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from ._impl import PathAlias, origin_aliases, rhui_aliases
from ._impl import PathAlias, origin_aliases, rhui_aliases, load_data

__all__ = ["PathAlias", "origin_aliases", "rhui_aliases"]
__all__ = ["PathAlias", "origin_aliases", "rhui_aliases", "load_data"]
47 changes: 28 additions & 19 deletions src/cdn_definitions/_impl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import json
import os


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",
]

# 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]))
import requests


def load_data(source=None):
if source is None:
# 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",
]

# If env var is set, it takes highest precedence
if "CDN_DEFINITIONS_PATH" in os.environ:
cdn_definitions_path = os.environ["CDN_DEFINITIONS_PATH"]
candidate_paths.insert(0, cdn_definitions_path)
if cdn_definitions_path.startswith("http"):
return requests.get(cdn_definitions_path)
existing_paths = [p for p in candidate_paths if os.path.exists(p)]
return json.load(open(existing_paths[0]))
if os.path.exists(source):
return json.load(open(source))
if source.startswith("http"):
return requests.get(source)
raise RuntimeError("Could not load data")


DATA = load_data()
Expand Down Expand Up @@ -43,15 +52,15 @@ def __init__(self, **kwargs):

def rhui_aliases():
"""Returns:
list[:class:`~PathAlias`]
A list of aliases relating to RHUI paths.
list[:class:`~PathAlias`]
A list of aliases relating to RHUI paths.
"""
return [PathAlias(**elem) for elem in DATA["rhui_alias"]]


def origin_aliases():
"""Returns:
list[:class:`~PathAlias`]
A list of aliases relating to origin paths.
list[:class:`~PathAlias`]
A list of aliases relating to origin paths.
"""
return [PathAlias(**elem) for elem in DATA["origin_alias"]]
14 changes: 14 additions & 0 deletions src/cdn_definitions/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import json
from .. import load_data


def schema():
with open(
os.path.join(os.path.dirname(os.path.dirname(__file__)), "schema.json")
) as f:
return json.load(f)


def data(source=None):
return load_data(source)
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest
jsonschema
PyYAML
mock
25 changes: 25 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json
import os

from cdn_definitions import api


def test_load_schema_api():
schema = api.schema()
with open(
os.path.join(
os.path.dirname(os.path.dirname(__file__)),
"src",
"cdn_definitions",
"schema.json",
)
) as f:
local_schema = json.load(f)
assert schema == local_schema


def test_load_data_api(tmpdir):
json_file = tmpdir.join("myfile.json")
json_file.write('{"hello": "world"}')
data = api.data(source=str(json_file))
assert data == {"hello": "world"}
35 changes: 35 additions & 0 deletions tests/test_load.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from cdn_definitions._impl import load_data
from mock import patch
import pytest
import requests


def test_can_load_custom_data(monkeypatch, tmpdir):
Expand All @@ -11,3 +14,35 @@ def test_can_load_custom_data(monkeypatch, tmpdir):
data = load_data()

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


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

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

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


def test_can_load_url_from_env_var(monkeypatch):
with patch("cdn_definitions._impl.requests.get") as r:
r.return_value = {"hello": "world"}
monkeypatch.setenv("CDN_DEFINITIONS_PATH", "https://example")
data = load_data()
assert data == {"hello": "world"}


def test_can_load_url_from_source_arg(monkeypatch):
with patch("cdn_definitions._impl.requests.get") as r:
r.return_value = {"hello": "world"}
data = load_data("https://example.com")

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


def test_invalid_source(monkeypatch, tmpdir):
with pytest.raises(RuntimeError, match="Could not load data"):
data = load_data(source="test")
7 changes: 6 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 Down

0 comments on commit 75f799e

Please sign in to comment.