-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetching Distribution and Metadata for plugins (#1108)
* Adding plugin ETL base * Clean up * Handle pypi data source fetch * clean up * Adding tests for pypi_adapter * Adding tests for dynamo * Updating models * refactor funsies * Refactoring type hints * Adding metadata fetch and formatting * Adding github tokens for data-workflow * Making tests more environment agnostic * Clean up and optimizations * Apply suggestions from code review Co-authored-by: Ashley Anderson <aganders3@gmail.com> * Refactoring * Refactoring logging for info * Updating requirements * Adding more tests * bug fixes * Handling labels.ontology None * Making visibility value consistent * Clean up * Removing aws_request_id from logging --------- Co-authored-by: Ashley Anderson <aganders3@gmail.com>
- Loading branch information
Showing
51 changed files
with
1,973 additions
and
108 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
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
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
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,28 @@ | ||
import json | ||
import os | ||
|
||
import boto3 | ||
|
||
|
||
class LambdaAdapter: | ||
_client = None | ||
|
||
@classmethod | ||
def _init_client(cls): | ||
cls._client = boto3.client("lambda") | ||
|
||
def __init__(self): | ||
if not self._client: | ||
self._init_client() | ||
|
||
def invoke(self, plugin: str, version: str) -> None: | ||
""" | ||
Invoke plugins lambda to generate manifest & write to cache. | ||
:param plugin: name of the plugin to fetch manifest | ||
:param version: plugin version to fetch manifest | ||
""" | ||
self._client.invoke( | ||
FunctionName=os.environ.get("PLUGINS_LAMBDA_NAME"), | ||
InvocationType="Event", | ||
Payload=json.dumps({"plugin": plugin, "version": version}), | ||
) |
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 import defaultdict | ||
from typing import Optional | ||
|
||
from nhcommons.models.category import get_category | ||
from nhcommons.utils.custom_parser import render_description | ||
from nhcommons.utils.github_adapter import ( | ||
get_github_metadata, is_valid_repo_url | ||
) | ||
from nhcommons.utils.pypi_adapter import get_plugin_pypi_metadata | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_formatted_metadata(plugin: str, version: str) -> Optional[dict]: | ||
pypi_metadata = get_plugin_pypi_metadata(plugin, version) | ||
if not pypi_metadata: | ||
return None | ||
|
||
metadata = _generate_metadata(pypi_metadata) | ||
return _format_metadata(metadata) | ||
|
||
|
||
def _format_metadata(metadata: dict) -> dict: | ||
if "description" in metadata: | ||
description = metadata.get("description") | ||
metadata["description_text"] = render_description(description) | ||
if "labels" in metadata: | ||
labels = metadata["labels"] | ||
version = labels.get("ontology") | ||
|
||
if version: | ||
cat, cat_hierarchy = _process_for_categories(labels, version) | ||
metadata["category"] = cat | ||
metadata["category_hierarchy"] = cat_hierarchy | ||
else: | ||
logger.warning(f"Invalid version in label labels={labels}") | ||
|
||
del metadata["labels"] | ||
|
||
return metadata | ||
|
||
|
||
def _generate_metadata(pypi_metadata: dict) -> dict: | ||
github_repo_url = pypi_metadata.get("code_repository") | ||
if is_valid_repo_url(github_repo_url): | ||
github_metadata = get_github_metadata(github_repo_url) | ||
return {**pypi_metadata, **github_metadata} | ||
return pypi_metadata | ||
|
||
|
||
def _process_for_categories(labels: dict, version: str) -> (dict, dict): | ||
categories = defaultdict(list) | ||
category_hierarchy = defaultdict(list) | ||
for label_term in labels.get("terms", []): | ||
for category in get_category(label_term, version): | ||
dimension = category.get("dimension") | ||
label = category.get("label") | ||
if label not in categories[dimension]: | ||
categories[dimension].append(label) | ||
category.get("hierarchy")[0] = label | ||
category_hierarchy[dimension].append(category.get("hierarchy")) | ||
return dict(categories), dict(category_hierarchy) |
Oops, something went wrong.