Skip to content

Commit

Permalink
Merge pull request #12 from NewTec-GmbH/feature/ABC
Browse files Browse the repository at this point in the history
AdapterInterface uses ABC to enforce implementation
  • Loading branch information
jkerpe authored Jul 19, 2024
2 parents 909936a + bfa5505 commit 51521dd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
19 changes: 0 additions & 19 deletions src/pyMetricCli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,9 @@ def _import_adapter(adapter_path: str) -> AdapterInterface:
adapter_instance = adapter.Adapter()

# Check all required attributes and methods of the adapter class.
# Must be done as Python does not enforce interfaces.
if not isinstance(adapter_instance, AdapterInterface):
LOG.error("The adapter class must inherit from AdapterInterface.")
adapter_instance = None
elif not hasattr(adapter_instance, "output"):
LOG.error("The adapter class must have an 'output' attribute.")
adapter_instance = None
elif not hasattr(adapter_instance, "jira_config"):
LOG.error("The adapter class must have a 'jira_config' attribute.")
adapter_instance = None
elif not hasattr(adapter_instance, "polarion_config"):
LOG.error("The adapter class must have a 'polarion_config' attribute.")
adapter_instance = None
elif not hasattr(adapter_instance, "superset_config"):
LOG.error("The adapter class must have a 'superset_config' attribute.")
adapter_instance = None
elif not hasattr(adapter_instance, "handle_jira"):
LOG.error("The adapter class must have a 'handle_jira' method.")
adapter_instance = None
elif not hasattr(adapter_instance, "handle_polarion"):
LOG.error("The adapter class must have a 'handle_polarion' method.")
adapter_instance = None
else:
LOG.info("Adapter class successfully imported.")

Expand Down
50 changes: 43 additions & 7 deletions src/pyMetricCli/adapter_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
# Imports
################################################################################

from abc import ABC, abstractmethod

################################################################################
# Variables
################################################################################
Expand All @@ -42,16 +44,51 @@
################################################################################


class AdapterInterface():
class AdapterInterface(ABC):
"""
Adapter interface class for handling different search results.
"""

output: dict
jira_config: dict
polarion_config: dict
superset_config: dict
@property
@abstractmethod
def output(self) -> dict:
"""
Output dictionary.
"""

@property
@abstractmethod
def jira_config(self) -> dict:
"""
JIRA configuration.
"""
@property
@abstractmethod
def polarion_config(self) -> dict:
"""
Polarion configuration.
"""
@property
@abstractmethod
def superset_config(self) -> dict:
"""
Superset configuration.
"""

def __init__(self) -> None:
"""
Initializes the adapter.
Must be called after the definition of the properties.
"""
assert isinstance(self.output, dict), "output must be a dictionary"
assert isinstance(self.jira_config,
dict), "jira_config must be a dictionary"
assert isinstance(self.polarion_config,
dict), "polarion_config config must be a dictionary"
assert isinstance(self.superset_config,
dict), "superset_config config must be a dictionary"

@abstractmethod
def handle_jira(self, _search_results: dict) -> bool:
"""
Handles the JIRA search results.
Expand All @@ -62,8 +99,8 @@ def handle_jira(self, _search_results: dict) -> bool:
Returns:
bool: True if the search results were handled successfully, False otherwise.
"""
return False

@abstractmethod
def handle_polarion(self, _search_results: dict) -> bool:
"""
Handles the Polarion search results.
Expand All @@ -74,7 +111,6 @@ def handle_polarion(self, _search_results: dict) -> bool:
Returns:
bool: True if the search results were handled successfully, False otherwise.
"""
return False

################################################################################
# Functions
Expand Down

0 comments on commit 51521dd

Please sign in to comment.