1
0
Fork 0

Add bundle and operator caching

This commit is contained in:
Maurizio Porrato 2023-08-04 08:06:39 +01:00
parent a2e9e0a796
commit 5f08cff13b
4 changed files with 37 additions and 17 deletions

View File

@ -96,11 +96,11 @@ class Bundle:
)
@property
def root(self) -> str:
def root(self) -> Path:
"""
:return: The path to the root of the bundle
"""
return str(self._bundle_path)
return self._bundle_path
def operator(self) -> "Operator":
"""
@ -232,6 +232,7 @@ class Operator:
f"Not a valid operator: {self._operator_path}"
)
self.operator_name = self._operator_path.name
self._bundle_cache = {}
@cached_property
def config(self) -> Any:
@ -252,19 +253,22 @@ class Operator:
return path.is_dir() and any(Bundle.probe(x) for x in path.iterdir())
@property
def root(self) -> str:
def root(self) -> Path:
"""
:return: The path to the root of the operator
"""
return str(self._operator_path)
return self._operator_path
def all_bundles(self) -> Iterator[Bundle]:
"""
:return: All the bundles for the operator
"""
for version_path in self._operator_path.iterdir():
if Bundle.probe(version_path):
yield self.bundle(version_path.name)
try:
yield self._bundle_cache[version_path.name]
except KeyError:
if Bundle.probe(version_path):
yield self.bundle(version_path.name)
def bundle_path(self, operator_version: str) -> Path:
"""
@ -281,7 +285,12 @@ class Operator:
:param operator_version: Version of the bundle
:return: The loaded bundle
"""
return Bundle(self.bundle_path(operator_version))
try:
return self._bundle_cache[operator_version]
except KeyError:
bundle = Bundle(self.bundle_path(operator_version))
self._bundle_cache[operator_version] = bundle
return bundle
def has(self, operator_version: str) -> bool:
"""
@ -439,6 +448,7 @@ class Repo:
f"Not a valid operator repository: {self._repo_path}"
)
self._operators_path = self._repo_path / self.OPERATORS_DIR
self._operator_cache = {}
@cached_property
def config(self) -> Any:
@ -459,19 +469,22 @@ class Repo:
return path.is_dir() and (path / cls.OPERATORS_DIR).is_dir()
@property
def root(self) -> str:
def root(self) -> Path:
"""
:return: The path to the root of the repository
"""
return str(self._repo_path)
return self._repo_path
def all_operators(self) -> Iterator[Operator]:
"""
:return: All the operators in the repo
"""
for operator_path in self._operators_path.iterdir():
if Operator.probe(operator_path):
yield self.operator(operator_path.name)
try:
yield self._operator_cache[operator_path.name]
except KeyError:
if Operator.probe(operator_path):
yield self.operator(operator_path.name)
def operator_path(self, operator_name: str) -> Path:
"""
@ -488,7 +501,12 @@ class Repo:
:param operator_name: Name of the operator
:return: The loaded operator
"""
return Operator(self.operator_path(operator_name))
try:
return self._operator_cache[operator_name]
except KeyError:
operator = Operator(self.operator_path(operator_name))
self._operator_cache[operator_name] = operator
return operator
def has(self, operator_name: str) -> bool:
"""
@ -496,7 +514,9 @@ class Repo:
:param operator_name: Name of the operator to look for
:return: True if the repo contains an operator with the given name
"""
return Operator.probe(self.operator_path(operator_name))
return operator_name in self._operator_cache or Operator.probe(
self.operator_path(operator_name)
)
def __iter__(self) -> Iterator[Operator]:
yield from self.all_operators()

View File

@ -14,7 +14,7 @@ def test_bundle(tmp_path: Path) -> None:
repo = Repo(tmp_path)
operator = repo.operator("hello")
bundle = operator.bundle("0.0.1")
assert bundle.root == operator.root + "/0.0.1"
assert bundle.root == operator.root / "0.0.1"
assert bundle.csv_operator_name == "hello"
assert bundle.csv_operator_version == "0.0.1"
assert (
@ -94,7 +94,7 @@ def test_bundle_invalid(tmp_path: Path) -> None:
_ = repo.operator("invalid_csv_name").bundle("0.0.1").csv_operator_name
assert not repo.has("missing_manifests")
with pytest.raises(InvalidBundleException, match="Not a valid bundle"):
_ = Bundle(repo.root + "/operators/missing_manifests/0.0.1")
_ = Bundle(repo.root / "operators" / "missing_manifests" / "0.0.1")
with pytest.raises(InvalidOperatorException, match="Not a valid operator"):
_ = repo.operator("missing_manifests")
assert repo.has("invalid_csv_contents")

View File

@ -21,7 +21,7 @@ def test_operator_one_bundle(tmp_path: Path) -> None:
assert bundle.operator() == operator
assert operator.config == {}
assert bundle.dependencies == []
assert operator.root == repo.root + "/operators/hello"
assert operator.root == repo.root / "operators" / "hello"
assert "hello" in repr(operator)

View File

@ -27,7 +27,7 @@ def test_repo_no_operators(tmp_path: Path) -> None:
repo = Repo(tmp_path)
assert len(list(repo)) == 0
assert repo.config == {"hello": "world"}
assert repo.root == str(tmp_path.resolve())
assert repo.root == tmp_path.resolve()
assert str(tmp_path) in repr(repo)