Skip to content

Commit

Permalink
Create wrapper class so data.json is only loaded once [RHELDST-4863]
Browse files Browse the repository at this point in the history
This commit includes the creation of a CdnData class, which holds
methods related to the Python implementation of the data. The
CdnData class allows for the data set to be loaded once.
  • Loading branch information
crungehottman committed Feb 1, 2021
1 parent 2383bf4 commit b112aeb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
5 changes: 3 additions & 2 deletions src/cdn_definitions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ._impl import PathAlias, origin_aliases, rhui_aliases, load_data
from ._impl import PathAlias, load_data
from ._impl.cdn_data import CdnData

__all__ = ["PathAlias", "origin_aliases", "rhui_aliases", "load_data"]
__all__ = ["PathAlias", "load_data", "CdnData"]
19 changes: 0 additions & 19 deletions src/cdn_definitions/_impl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ def load_data(source=None):
raise RuntimeError("Could not load data")


DATA = load_data()


class PathAlias(object):
"""Represents an alias between one CDN path and another, generally
used to make two directory trees on CDN serve identical content."""
Expand All @@ -48,19 +45,3 @@ def __init__(self, **kwargs):
# Equal src/dest is nonsensical
if self.src == self.dest:
raise ValueError("%s cannot alias itself!" % self.src)


def rhui_aliases():
"""Returns:
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.
"""
return [PathAlias(**elem) for elem in DATA["origin_alias"]]
26 changes: 26 additions & 0 deletions src/cdn_definitions/_impl/cdn_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from . import load_data, PathAlias


class CdnData(object):
def __init__(self, source=None):
self._source = source
self._data = None

def data(self):
if not self._data:
self._data = load_data(self._source)
return self._data

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

def origin_aliases(self):
"""Returns:
list[:class:`~PathAlias`]
A list of aliases relating to origin paths.
"""
return [PathAlias(**elem) for elem in self.data()["origin_alias"]]
8 changes: 5 additions & 3 deletions tests/test_alias.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import pytest

from cdn_definitions import PathAlias, origin_aliases, rhui_aliases
from cdn_definitions import PathAlias, CdnData


def test_rhui_sanity():
"""rhui_aliases must exist with expected properties"""
paths = rhui_aliases()
data = CdnData()
paths = data.rhui_aliases()

# There should be some items
assert paths
Expand All @@ -18,7 +19,8 @@ def test_rhui_sanity():

def test_origin_sanity():
"""origin_aliases must exist with expected properties"""
paths = origin_aliases()
data = CdnData()
paths = data.origin_aliases()

# There should be some items
assert paths
Expand Down
1 change: 0 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ def test_load_data_api(tmpdir):
json_file.write('{"hello": "world"}')
data = api.data(source=str(json_file))
assert data == {"hello": "world"}

0 comments on commit b112aeb

Please sign in to comment.