Skip to content

Commit

Permalink
Added adapter for handling of search results
Browse files Browse the repository at this point in the history
  • Loading branch information
gabryelreyes committed Jun 26, 2024
1 parent 6ec8d44 commit 6ebb6c8
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 1 deletion.
11 changes: 10 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# Examples
# Examples

## Configuration File

The configuration file must have the same structure as `examples\config.json`.

## Adapter

Each project must provide an `Adapter` class with the methods `handle_jira` and `handle_polarion` to pack the data from the search results into the output dictionary.
The declaration, arguments and name of the methods must remain the same as in `examples\adapter\adapter.py`, otherwise the program will not work correctly.
107 changes: 107 additions & 0 deletions examples/adapter/adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

"""Project-Specific Adapter Module"""

# BSD 3-Clause License
#
# Copyright (c) 2024, NewTec GmbH
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU5LAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

################################################################################
# Imports
################################################################################

from typing import Callable
import logging

################################################################################
# Variables
################################################################################

LOG = logging.getLogger(__name__)

################################################################################
# Classes
################################################################################


class Adapter:
"""
Adapter class for handling different search results.
"""

def handle_jira(self,
search_results: dict,
set_key_value_pair: Callable[[str, str], bool]) -> None:
"""
Handles the JIRA search results.
Args:
search_results: The search results from the JIRA API.
set_key_value_pair: The callback function to set
a key-value pair in the output dictionary.
set_key_value_pair:
Args:
key: The key to set in the output dictionary.
value: The value to set in the output dictionary.
Returns:
True if the key-value pair was set successfully, False otherwise.
"""
LOG.info("Handling JIRA search results...")
LOG.info(type(search_results))
LOG.info(type(set_key_value_pair))

def handle_polarion(self,
search_results: dict,
set_key_value_pair: Callable[[str, str], bool]) -> None:
"""
Handles the Polarion search results.
Args:
search_results: The search results from the Polarion API.
set_key_value_pair: The callback function to set
a key-value pair in the output dictionary.
set_key_value_pair:
Args:
key: The key to set in the output dictionary.
value: The value to set in the output dictionary.
Returns:
True if the key-value pair was set successfully, False otherwise.
"""
LOG.info("Handling Polarion search results...")
LOG.info(type(search_results))
LOG.info(type(set_key_value_pair))

################################################################################
# Functions
################################################################################

################################################################################
# Main
################################################################################
6 changes: 6 additions & 0 deletions examples/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"adapter_path": "examples/adapter/adapter.py",
"jira":{},
"polarion":{},
"superset":{}
}
71 changes: 71 additions & 0 deletions src/pyMetricCli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
import sys
import logging
import argparse
import os.path
import importlib.util
import json

from pyMetricCli.version import __version__, __author__, __email__, __repository__, __license__
from pyMetricCli.ret import Ret
Expand Down Expand Up @@ -98,6 +101,45 @@ def add_parser() -> argparse.ArgumentParser:
return parser


def import_handler(adapter_path: str) -> type:
"""
Import the adapter module from the given path.
Args:
adapter_path (str): The path to the adapter module.
Returns:
type: The Adapter Class.
"""
adapter_name = "adapter"

if not os.path.isfile(adapter_path):
raise ValueError(f"File not found: {adapter_path}")

module_spec = importlib.util.spec_from_file_location(adapter_name,
adapter_path)
adapter = importlib.util.module_from_spec(module_spec)
sys.modules[adapter_name] = adapter
module_spec.loader.exec_module(adapter)

return adapter.Adapter()


def set_key_value_pair(key: str, value: str) -> bool:
"""
Set the value of a key in a dictionary.
Args:
key (str): The key.
value (str): The value.
Returns:
bool: True if the key-value pair was set successfully, False otherwise.
"""
print(f"Setting key '{key}' to value '{value}'")
return True


def main() -> Ret:
""" The program entry point function.
Expand Down Expand Up @@ -125,9 +167,38 @@ def main() -> Ret:
LOG.info("* %s = %s", arg, vars(args)[arg])

try:
# Check if the config file is a JSON file.
if args.config_file.endswith(".json") is False:
raise ValueError(
"Invalid config_file format. Please provide a JSON file.")

# Load the config file.
with open(args.config_file, "r", encoding="UTF-8") as file:
config_data = json.load(file)

# Check if the adapter_path is present in the config file.
if "adapter_path" not in config_data:
raise ValueError("adapter_path not found in config file.")

# Import the adapter module.
adapter = import_handler(config_data["adapter_path"])

# Get data from JIRA and Polarion.
LOG.info("Getting data from JIRA and Polarion...")

# Call the handler functions to extract the data.
adapter.handle_jira({}, set_key_value_pair)
adapter.handle_polarion({}, set_key_value_pair)

# Save the output dictionary to a temporary file.
LOG.info("Saving output to a temporary file...")

# Send the temporary file to the metric server using Superset.
LOG.info("Sending the temporary file to the metric server...")

# Remove the temporary file.
LOG.info("Removing the temporary file...")

except Exception as e: # pylint: disable=broad-except
LOG.error("An error occurred: %s", e)
ret_status = Ret.ERROR
Expand Down

0 comments on commit 6ebb6c8

Please sign in to comment.