-
Notifications
You must be signed in to change notification settings - Fork 1
/
ygg-mule.py
123 lines (95 loc) · 3.91 KB
/
ygg-mule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
import argparse
import asyncio
from lib.core_utils.common import YggdrasilUtilities as Ygg
from lib.core_utils.config_loader import ConfigLoader
from lib.core_utils.logging_utils import configure_logging, custom_logger
from lib.couchdb.manager import ProjectDBManager, YggdrasilDBManager
# Configure logging
configure_logging(debug=True)
logging = custom_logger("Ygg-Mule")
async def launch_realm(realm):
try:
await realm.launch()
except Exception as e:
logging.error(f"Error in realm.launch(): {e}", exc_info=True)
def process_document(doc_id):
"""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 database managers
pdm = ProjectDBManager()
ydm = YggdrasilDBManager()
# Fetch the document from the project database
document = pdm.fetch_document_by_id(doc_id)
if not document:
logging.error(f"Document with ID {doc_id} not found.")
return
project_id = document.get("project_id")
# 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:
RealmClass = Ygg.load_realm_class(module_loc)
if RealmClass:
realm = RealmClass(document, ydm)
if realm.proceed:
asyncio.run(launch_realm(realm))
logging.info("Processing complete.")
else:
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:
logging.error(f"Error while processing document: {e}", exc_info=True)
# TODO: If the module registry doesn’t change often, consider caching it to avoid reloading it every time
def get_module_location(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 or None: The module location if found; otherwise, None.
"""
try:
# Load the module registry configuration
module_registry = ConfigLoader().load_config("module_registry.json")
# Extract the library construction method from the document
method = document["details"]["library_construction_method"]
# Retrieve module configuration for the specified method
module_config = module_registry.get(method)
if module_config:
return module_config["module"]
# If no exact match, check for prefix matches
for registered_method, config in module_registry.items():
if config.get("prefix") and method.startswith(registered_method):
return config["module"]
logging.warning(f"No module configuration found for method '{method}'.")
return None
except KeyError as e:
logging.error(f"Error accessing module location: {e}")
return None
except Exception as 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")
# Parse arguments
args = parser.parse_args()
# Process the document
process_document(args.doc_id)
if __name__ == "__main__":
main()