diff --git a/application.log b/application.log index f260a98..2f329db 100644 --- a/application.log +++ b/application.log @@ -44,3 +44,4 @@ 2023-07-24 21:44:05,766 - WorldStateRssFeedsResource - DEBUG - Instance of WorldStateRssFeedsResource resource created. 2023-07-24 21:44:05,957 - WorldStateRssFeedsResource - DEBUG - Query with [keyword: None, date: None] returned 51 results 2023-07-24 21:44:05,957 - WorldStateRssFeedsResource - DEBUG - Sample Result: Rhodes wildfires are 'like a biblical catastrophe' +2025-06-16 15:43:14,138 - TempRes - DEBUG - Instance of TempRes resource created. diff --git a/capability_manager/CapabilityManager.py b/capability_manager/CapabilityManager.py index 3902c39..e3ba425 100644 --- a/capability_manager/CapabilityManager.py +++ b/capability_manager/CapabilityManager.py @@ -1,5 +1,4 @@ import os -import inspect from .Capability import Capability @@ -75,14 +74,18 @@ def create_and_save_capability_class(self, name, description, required_imports=N new_class = self.create_capability_class(name, description) new_class.is_built_in = False # Add a property to indicate that this is not a built-in class - # Generate the Python code for the new class - class_code = inspect.getsource(new_class) + if required_imports is None: + required_imports = "" - # Define the necessary import statements - import_code = (required_imports + "\n" - "from capability_manager.Capability import Capability\n") + import_code = required_imports + "\nfrom ..Capability import Capability\n\n" + + class_code = ( + f"class {name}(Capability):\n" + f" \"\"\"{description}\"\"\"\n\n" + f" def __init__(self):\n" + f" super().__init__(name=\"{name}\", description=\"{description}\")\n" + ) - # Combine the import statements and the class code code = import_code + class_code # Define the module directory @@ -102,3 +105,5 @@ def create_and_save_capability_class(self, name, description, required_imports=N # Write the code to the file with open(filename, "w") as file: file.write(code) + + return filename diff --git a/capability_manager/test_capabilities.py b/capability_manager/test_capabilities.py index e69de29..9466486 100644 --- a/capability_manager/test_capabilities.py +++ b/capability_manager/test_capabilities.py @@ -0,0 +1,22 @@ +import importlib +import os + +from capability_manager import CapabilityManager, Capability + + +def test_dynamic_capability_creation_and_import(): + manager = CapabilityManager(creator="tester") + name = "TempCap" + description = "temporary capability" + + path = manager.create_and_save_capability_class(name, description, required_imports="") + + assert os.path.exists(path) + + module_name = "capability_manager.dynamically_created_capabilities." + os.path.splitext(os.path.basename(path))[0] + mod = importlib.import_module(module_name) + cls = getattr(mod, name) + instance = cls() + assert isinstance(instance, Capability) + + os.remove(path) diff --git a/resource_manager/ResourceManager.py b/resource_manager/ResourceManager.py index 03565b4..0fdb43d 100644 --- a/resource_manager/ResourceManager.py +++ b/resource_manager/ResourceManager.py @@ -1,5 +1,4 @@ import os -import inspect from .Resource import Resource @@ -81,14 +80,18 @@ def create_and_save_resource_class(self, name, description, capacity=0, used=0, new_class = self.create_resource_class(name, description, capacity, used, budget) new_class.is_built_in = False # Add a property to indicate that this is not a built-in class - # Generate the Python code for the new class - class_code = inspect.getsource(new_class) + if required_imports is None: + required_imports = "" - # Define the necessary import statements - import_code = (required_imports + "\n" - "from resource_manager.Resource import Resource\n") + import_code = required_imports + "\nfrom ..Resource import Resource\n\n" + + class_code = ( + f"class {name}(Resource):\n" + f" \"\"\"{description}\"\"\"\n\n" + f" def __init__(self, capacity={capacity}, used={used}, budget={budget}):\n" + f" super().__init__(name=\"{name}\", description=\"{description}\", capacity=capacity, used=used, budget=budget)\n" + ) - # Combine the import statements and the class code code = import_code + class_code # Define the module directory @@ -104,3 +107,9 @@ def create_and_save_resource_class(self, name, description, capacity=0, used=0, while os.path.exists(filename): filename = os.path.join(module, f"{name}V{i}Resource.py") i += 1 + + # Write the code to the file + with open(filename, "w") as file: + file.write(code) + + return filename diff --git a/resource_manager/test_resources.py b/resource_manager/test_resources.py new file mode 100644 index 0000000..d1af5d2 --- /dev/null +++ b/resource_manager/test_resources.py @@ -0,0 +1,22 @@ +import importlib +import os + +from resource_manager import ResourceManager, Resource + + +def test_dynamic_resource_creation_and_import(): + manager = ResourceManager(creator="tester") + name = "TempRes" + description = "temporary resource" + + path = manager.create_and_save_resource_class(name, description, capacity=1, used=0, budget=0, required_imports="") + + assert os.path.exists(path) + + module_name = "resource_manager.dynamically_created_resources." + os.path.splitext(os.path.basename(path))[0] + mod = importlib.import_module(module_name) + cls = getattr(mod, name) + instance = cls() + assert isinstance(instance, Resource) + + os.remove(path)