import os import importlib.util import re def execute_functions_in_folder(folder_path, tenureKey): # List all files in the folder file_list = os.listdir(folder_path) # Initialize an empty dictionary to store function names and return values function_results = {} # Iterate through each file for file_name in file_list: if file_name.endswith('.py'): # Consider only Python files module_name = file_name[:-3] # Remove the '.py' extension # Import the module dynamically file_path = os.path.join(folder_path, file_name) spec = importlib.util.spec_from_file_location(module_name, file_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) # Execute the function with the same name as the file function_name = module_name if hasattr(module, function_name) and callable(getattr(module, function_name)): function = getattr(module, function_name) return_value = function(tenureKey) # Execute the function and get the return value function_results[function_name] = return_value # Add the function name and return value to the dictionary else: print(f"No function named '{function_name}' found in '{module_name}'") return function_results def open_markdown_file(file_path): try: with open(file_path, 'r') as file: markdown_content = file.read() return markdown_content except FileNotFoundError: print(f"Markdown file '{file_path}' not found.") return None except Exception as e: print(f"An error occurred while opening the markdown file: {e}") return None def substitute_variables_in_markdown(markdown_content, variable_dict): for variable, content in variable_dict.items(): # markdown_content = re.sub(r'\{\{' + variable + r'\}\}', str(content), markdown_content) markdown_content.format(**variable_dict) return markdown_content if __name__ == '__main__': variable_dict = execute_functions_in_folder('./modules', '1234') print(variable_dict) markdown_content = open_markdown_file('report.md') if markdown_content: # substituted_content = substitute_variables_in_markdown(markdown_content, variable_dict) # print(substituted_content) print(markdown_content.format(**variable_dict))