Skip to content

Commit

Permalink
Update docstring, add type hints, improve code readability, adapt for…
Browse files Browse the repository at this point in the history
… file relocations
  • Loading branch information
glrs committed Oct 3, 2024
1 parent eff75cb commit ffd8250
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions ygg-mule.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
#!/usr/bin/env python

import asyncio
import argparse

from lib.utils.config_loader import ConfigLoader
from lib.utils.common import YggdrasilUtilities as Ygg
from lib.core_utils.config_loader import ConfigLoader
from lib.core_utils.common import YggdrasilUtilities as Ygg
from lib.couchdb.manager import ProjectDBManager, YggdrasilDBManager
from lib.utils.logging_utils import configure_logging, custom_logger
from lib.core_utils.logging_utils import configure_logging, custom_logger

# Configure logging
configure_logging(debug=True)
logging = custom_logger("Ygg-Mule")


def process_document(doc_id):
"""
Processes a document by its ID. Fetches the document from the database,
determines the appropriate module to load, and executes the module.
"""Process a document by its ID.
Fetches the document from the database, determines the appropriate module
to load, and executes the module.
Args:
doc_id (str): The ID of the document to process.
"""
# Initialize the ProjectDBManager and YggdrasilDBManager
# Initialize the database managers
pdm = ProjectDBManager()
ydm = YggdrasilDBManager()

# Fetch the document using the ProjectDBManager
# Fetch the document from the project database
document = pdm.fetch_document_by_id(doc_id)
# document = fetch_document_from_db(doc_id)

if not document:
logging.error(f"Document with ID {doc_id} not found.")
return

project_id = document.get('project_id')
project_id = document.get("project_id")

# Check if the project exists in the yggdrasil database
existing_document = ydm.check_project_exists(project_id)

if existing_document is None:
projects_reference = document.get('_id')
method = document.get('details', {}).get('library_construction_method')
projects_reference = document.get("_id")
method = document.get("details", {}).get("library_construction_method")

# Create a new project if it doesn't exist
# Create a new project in the yggdrasil database
ydm.create_project(project_id, projects_reference, method)
process_project = True
else:
# If the project exists, check if it is completed
if existing_document.get('status') == 'completed':
logging.info(f"Project with ID {project_id} is already completed. Skipping further processing.")
# Check the status of the existing project
status = existing_document.get("status")
if status == "completed":
logging.info(f"Project with ID {project_id} is already completed. Skipping processing.")
process_project = False
else:
logging.info(f"Project with ID {project_id} is ongoing and will be processed.")
Expand All @@ -55,6 +57,9 @@ def process_document(doc_id):
if process_project:
# Determine the appropriate module to load
module_loc = get_module_location(document)
if not module_loc:
logging.warning(f"No module found for document with ID {doc_id}.")
return

# Load and execute the module
try:
Expand All @@ -65,7 +70,7 @@ def process_document(doc_id):
asyncio.run(realm.process())
logging.info("Processing complete.")
else:
logging.info(f"Skipping processing due to missing required information. {document.get('project_id')}")
logging.info(f"Skipping processing due to missing required information for project: {project_id}")
else:
logging.warning(f"Failed to load module '{module_loc}'.")
except Exception as e:
Expand All @@ -74,14 +79,13 @@ def process_document(doc_id):

# TODO: If the module registry doesn’t change often, consider caching it to avoid reloading it every time
def get_module_location(document):
"""
Retrieves the module location based on the library construction method from the document.
"""Retrieve the module location based on the library construction method.
Args:
document (dict): The document containing details about the library construction method.
Returns:
str: The module location, or None if not found.
str or None: The module location if found; otherwise, None.
"""
try:
# Load the module registry configuration
Expand All @@ -92,9 +96,7 @@ def get_module_location(document):

# Retrieve module configuration for the specified method
module_config = module_registry.get(method)

if module_config:
# Return the module location
return module_config["module"]

# If no exact match, check for prefix matches
Expand All @@ -105,18 +107,20 @@ def get_module_location(document):
logging.warning(f"No module configuration found for method '{method}'.")
return None
except KeyError as e:
logging.error(f'Error accessing module location: {e}')
logging.error(f"Error accessing module location: {e}")
return None
except Exception as e:
logging.error(f'Unexpected error: {e}')
logging.error(f"Unexpected error: {e}", exc_info=True)
return None


def main():
"""Main function to parse arguments and start Yggdrasil."""
logging.info("Ygg-Mule: Standalone Module Executor for Yggdrasil")

# Set up argument parser
parser = argparse.ArgumentParser(description='Ygg-Mule: Standalone Module Executor for Yggdrasil')
parser.add_argument('doc_id', type=str, help='Document ID to process')
parser = argparse.ArgumentParser(description="Ygg-Mule: Standalone Module Executor for Yggdrasil")
parser.add_argument("doc_id", type=str, help="Document ID to process")

# Parse arguments
args = parser.parse_args()
Expand Down

0 comments on commit ffd8250

Please sign in to comment.