generated from MinBZK/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
376c8f7
commit fd5d501
Showing
11 changed files
with
180 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import logging | ||
from collections.abc import Sequence | ||
|
||
from amt.clients.clients import TaskRegistryAPIClient, TaskType | ||
from amt.schema.requirement import Requirement | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RequirementsService: | ||
def __init__(self, repository: TaskRegistryRepository) -> None: | ||
self.repository = repository | ||
|
||
def fetch_measures(self, urns: str | Sequence[str] | None = None) -> list[Requirement]: | ||
""" | ||
Fetches measures with the given URNs. | ||
If urns contains an URN that is not a valid URN of an measure, it is simply ignored. | ||
@param urns: URNs of instruments to fetch. If None, function returns all measures. | ||
@return: List of measures with the given URNs in 'urns'. | ||
""" | ||
task_data = self.repository.fetch_tasks(TaskType.REQUIREMENTS, urns) | ||
return [Requirement(**data) for data in task_data] | ||
|
||
|
||
def create_requirements_service() -> RequirementsService: | ||
client = TaskRegistryAPIClient() | ||
repository = TaskRegistryRepository(client) | ||
return RequirementsService(repository) | ||
|
||
|
||
class RequirementsService: | ||
def __init__(self) -> None: | ||
self.client = TaskRegistryAPIClient() | ||
|
||
def fetch_requirements(self, urns: str | Sequence[str] | None = None) -> list[Requirement]: | ||
""" | ||
This functions returns requirement with given URN's. If urns contains an URN that is not a | ||
valid URN of an requirement it is simply ignored. | ||
|
||
@param: URN's of requirements to fetch. If empty, function returns all requirements. | ||
@return: List of requirements with given URN's in 'urns'. | ||
""" | ||
|
||
if isinstance(urns, str): | ||
urns = [urns] | ||
|
||
all_valid_urns = self.fetch_urns() | ||
|
||
if urns is not None: | ||
return [ | ||
Requirement(**self.client.get_task_by_urn(TaskType.REQUIREMENTS, urn)) | ||
for urn in urns | ||
if urn in all_valid_urns | ||
] | ||
|
||
return [Requirement(**self.client.get_task_by_urn(TaskType.REQUIREMENTS, urn)) for urn in all_valid_urns] | ||
|
||
def fetch_urns(self) -> list[str]: | ||
""" | ||
Fetches all valid requirement URN's. | ||
""" | ||
content_list = self.client.get_list_of_task(TaskType.REQUIREMENTS) | ||
return [content.urn for content in content_list.root] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import logging | ||
from collections.abc import Sequence | ||
from typing import Any | ||
|
||
from amt.clients.clients import TaskRegistryAPIClient, TaskType | ||
from amt.core.exceptions import AMTNotFound | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TaskRegistryRepository: | ||
""" | ||
Responsible for fetching tasks (instruments, measures, etc.) from the Task Registry API. | ||
""" | ||
|
||
def __init__(self, client: TaskRegistryAPIClient) -> None: | ||
self.client = client | ||
|
||
def fetch_tasks(self, task_type: TaskType, urns: str | Sequence[str] | None = None) -> list[dict[str, Any]]: | ||
""" | ||
Fetches tasks (instruments, measures, etc.) with the given URNs. | ||
If urns contains an URN that is not a valid URN of a task, it is simply ignored. | ||
@param task_type: The type of task to fetch (e.g. TaskType.INSTRUMENTS, TaskType.MEASURES). | ||
@param urns: URNs of tasks to fetch. If None, function returns all tasks of the given type. | ||
@return: List of task data dictionaries with the given URNs in 'urns'. | ||
""" | ||
if isinstance(urns, str): | ||
urns = [urns] | ||
|
||
all_valid_urns: list[str] = self.fetch_urns(task_type) | ||
|
||
if urns is None: | ||
return [self.client.get_task_by_urn(task_type, urn) for urn in all_valid_urns] | ||
|
||
tasks: list[dict[str, Any]] = [] | ||
for urn in urns: | ||
# For backward compatibilty of this method we now simply ignore invalid URN's. | ||
# We might want to refactor this later to throw exceptions when task with URN is not | ||
# found. | ||
try: | ||
tasks.append(self.client.get_task_by_urn(task_type, urn)) | ||
except AMTNotFound: | ||
logger.warning(f"Cannot find {task_type.value} with URN {urn}") | ||
|
||
return tasks | ||
|
||
def fetch_urns(self, task_type: TaskType) -> list[str]: | ||
""" | ||
Fetches all valid URNs for the given task type. | ||
""" | ||
content_list = self.client.get_list_of_task(task_type) | ||
return [content.urn for content in content_list.root] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.