Skip to content

Commit 96a90e4

Browse files
Instantiate the transform/check classes. (#198)
* Instantiate the transform/check classes. Temporary fix. * checks: Do not include branch or client when instantiating to follow . * Remove unused transform/check loading functions. Change branch to empty string when instantiating transform class. * Fix linting/formatting after conflict fix in GitHub UI.
1 parent 322a0d1 commit 96a90e4

File tree

4 files changed

+8
-72
lines changed

4 files changed

+8
-72
lines changed

infrahub_sdk/checks.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@
1111
from git.repo import Repo
1212
from pydantic import BaseModel, Field
1313

14-
from .exceptions import InfrahubCheckNotFoundError, UninitializedError
14+
from .exceptions import UninitializedError
1515

1616
if TYPE_CHECKING:
17-
from pathlib import Path
18-
1917
from . import InfrahubClient
20-
from .schema.repository import InfrahubCheckDefinitionConfig
2118

2219
INFRAHUB_CHECK_VARIABLE_TO_IMPORT = "INFRAHUB_CHECKS"
2320

@@ -176,27 +173,3 @@ async def run(self, data: dict | None = None) -> bool:
176173
self.log_info("Check succesfully completed")
177174

178175
return self.passed
179-
180-
181-
def get_check_class_instance(
182-
check_config: InfrahubCheckDefinitionConfig, search_path: Path | None = None
183-
) -> InfrahubCheck:
184-
if check_config.file_path.is_absolute() or search_path is None:
185-
search_location = check_config.file_path
186-
else:
187-
search_location = search_path / check_config.file_path
188-
189-
try:
190-
spec = importlib.util.spec_from_file_location(check_config.class_name, search_location)
191-
module = importlib.util.module_from_spec(spec) # type: ignore[arg-type]
192-
spec.loader.exec_module(module) # type: ignore[union-attr]
193-
194-
# Get the specified class from the module
195-
check_class = getattr(module, check_config.class_name)
196-
197-
# Create an instance of the class
198-
check_instance = check_class()
199-
except (FileNotFoundError, AttributeError) as exc:
200-
raise InfrahubCheckNotFoundError(name=check_config.name) from exc
201-
202-
return check_instance

infrahub_sdk/pytest_plugin/items/check.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ def instantiate_check(self) -> None:
3737
str(self.resource_config.file_path.parent) if self.resource_config.file_path.parent != Path() else None # type: ignore[attr-defined]
3838
)
3939

40-
self.check_instance = self.resource_config.load_class( # type: ignore[attr-defined]
40+
check_class = self.resource_config.load_class( # type: ignore[attr-defined]
4141
import_root=self.repository_base, relative_path=relative_path
4242
)
43+
self.check_instance = check_class()
4344

4445
def run_check(self, variables: dict[str, Any]) -> Any:
4546
self.instantiate_check()

infrahub_sdk/pytest_plugin/items/python_transform.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ def instantiate_transform(self) -> None:
3636
relative_path = (
3737
str(self.resource_config.file_path.parent) if self.resource_config.file_path.parent != Path() else None # type: ignore[attr-defined]
3838
)
39-
self.transform_instance = self.resource_config.load_class( # type: ignore[attr-defined]
39+
transform_class = self.resource_config.load_class( # type: ignore[attr-defined]
4040
import_root=self.repository_base, relative_path=relative_path
4141
)
42+
client = self.session.infrahub_client # type: ignore[attr-defined]
43+
# TODO: Look into seeing how a transform class may use the branch, but set as a empty string for the time being to keep current behaviour
44+
self.transform_instance = transform_class(branch="", client=client)
4245

4346
def run_transform(self, variables: dict[str, Any]) -> Any:
4447
self.instantiate_transform()

infrahub_sdk/transforms.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
from __future__ import annotations
22

33
import asyncio
4-
import importlib
54
import os
65
from abc import abstractmethod
76
from typing import TYPE_CHECKING, Any
87

98
from git import Repo
109

11-
from .exceptions import InfrahubTransformNotFoundError, UninitializedError
10+
from .exceptions import UninitializedError
1211

1312
if TYPE_CHECKING:
14-
from pathlib import Path
15-
1613
from . import InfrahubClient
17-
from .schema.repository import InfrahubPythonTransformConfig
1814

1915
INFRAHUB_TRANSFORM_VARIABLE_TO_IMPORT = "INFRAHUB_TRANSFORMS"
2016

@@ -95,40 +91,3 @@ async def run(self, data: dict | None = None) -> Any:
9591
return await self.transform(data=unpacked)
9692

9793
return self.transform(data=unpacked)
98-
99-
100-
def get_transform_class_instance(
101-
transform_config: InfrahubPythonTransformConfig,
102-
search_path: Path | None = None,
103-
branch: str = "",
104-
client: InfrahubClient | None = None,
105-
) -> InfrahubTransform:
106-
"""Gets an instance of the InfrahubTransform class.
107-
108-
Args:
109-
transform_config: A config object with information required to find and load the transform.
110-
search_path: The path in which to search for a python file containing the transform. The current directory is
111-
assumed if not speicifed.
112-
branch: Infrahub branch which will be targeted in graphql query used to acquire data for transformation.
113-
client: InfrahubClient used to interact with infrahub API.
114-
"""
115-
if transform_config.file_path.is_absolute() or search_path is None:
116-
search_location = transform_config.file_path
117-
else:
118-
search_location = search_path / transform_config.file_path
119-
120-
try:
121-
spec = importlib.util.spec_from_file_location(transform_config.class_name, search_location)
122-
module = importlib.util.module_from_spec(spec) # type: ignore[arg-type]
123-
spec.loader.exec_module(module) # type: ignore[union-attr]
124-
125-
# Get the specified class from the module
126-
transform_class = getattr(module, transform_config.class_name)
127-
128-
# Create an instance of the class
129-
transform_instance = transform_class(branch=branch, client=client)
130-
131-
except (FileNotFoundError, AttributeError) as exc:
132-
raise InfrahubTransformNotFoundError(name=transform_config.name) from exc
133-
134-
return transform_instance

0 commit comments

Comments
 (0)