Update MemoryReadingAtomicFlow.py
Browse files- MemoryReadingAtomicFlow.py +71 -11
MemoryReadingAtomicFlow.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
from typing import Dict, Any
|
| 2 |
import os
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
from flows.base_flows import AtomicFlow
|
| 5 |
-
from flows.utils.general_helpers import get_pyfile_functions_metadata_from_file
|
| 6 |
|
| 7 |
|
| 8 |
class MemoryReadingAtomicFlow(AtomicFlow):
|
|
@@ -34,18 +35,77 @@ class MemoryReadingAtomicFlow(AtomicFlow):
|
|
| 34 |
content = file.read()
|
| 35 |
return content
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def _read_py_code_library(self, file_location):
|
| 38 |
-
metadata =
|
| 39 |
if len(metadata) == 0:
|
| 40 |
return "No functions yet."
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
description = f"Function Name: {function['name']}\n"
|
| 44 |
-
description += f"Documentation: {function.get('docstring', 'No documentation')}\n"
|
| 45 |
-
description += f"Parameters: {', '.join(function.get('parameters', []))}\n"
|
| 46 |
-
description += f"Default Values: {function.get('defaults', 'None')}"
|
| 47 |
-
descriptions.append(description)
|
| 48 |
-
return "\n\n".join(descriptions)
|
| 49 |
|
| 50 |
def run(
|
| 51 |
self,
|
|
|
|
| 1 |
from typing import Dict, Any
|
| 2 |
import os
|
| 3 |
+
import ast
|
| 4 |
+
import importlib
|
| 5 |
+
import inspect
|
| 6 |
from flows.base_flows import AtomicFlow
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
class MemoryReadingAtomicFlow(AtomicFlow):
|
|
|
|
| 35 |
content = file.read()
|
| 36 |
return content
|
| 37 |
|
| 38 |
+
def _get_pyfile_functions_metadata_from_file(self, file_location):
|
| 39 |
+
def python_file_path_to_module_name(file_path):
|
| 40 |
+
return os.path.basename(file_path).replace('.py', '')
|
| 41 |
+
|
| 42 |
+
def extract_top_level_function_names(python_file_path):
|
| 43 |
+
with open(python_file_path, 'r') as file:
|
| 44 |
+
file_content = file.read()
|
| 45 |
+
tree = ast.parse(file_content)
|
| 46 |
+
functions = filter(lambda node: isinstance(node, ast.FunctionDef), ast.iter_child_nodes(tree))
|
| 47 |
+
return [node.name for node in functions]
|
| 48 |
+
|
| 49 |
+
def load_module_from_file(file_path):
|
| 50 |
+
module_name = python_file_path_to_module_name(file_path)
|
| 51 |
+
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
| 52 |
+
module = importlib.util.module_from_spec(spec)
|
| 53 |
+
spec.loader.exec_module(module)
|
| 54 |
+
return module
|
| 55 |
+
|
| 56 |
+
def get_function_from_name(function_name, module):
|
| 57 |
+
return getattr(module, function_name)
|
| 58 |
+
|
| 59 |
+
def function_to_dict(function):
|
| 60 |
+
if not callable(function):
|
| 61 |
+
raise ValueError("Provided object is not a function.")
|
| 62 |
+
|
| 63 |
+
function_dict = {
|
| 64 |
+
"name": function.__name__,
|
| 65 |
+
"doc": function.__doc__,
|
| 66 |
+
"args": []
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
signature = inspect.signature(function)
|
| 70 |
+
for name, param in signature.parameters.items():
|
| 71 |
+
arg_info = {
|
| 72 |
+
"name": name,
|
| 73 |
+
"default": param.default if param.default is not inspect.Parameter.empty else None,
|
| 74 |
+
"type": str(param.annotation) if param.annotation is not inspect.Parameter.empty else "unknown"
|
| 75 |
+
}
|
| 76 |
+
function_dict["args"].append(arg_info)
|
| 77 |
+
|
| 78 |
+
return function_dict
|
| 79 |
+
|
| 80 |
+
function_names = extract_top_level_function_names(file_location)
|
| 81 |
+
module = load_module_from_file(file_location)
|
| 82 |
+
functions = [get_function_from_name(name, module) for name in function_names]
|
| 83 |
+
return [function_to_dict(function) for function in functions]
|
| 84 |
+
|
| 85 |
+
def _format_metadata(self, metadata):
|
| 86 |
+
lines = []
|
| 87 |
+
for function_data in metadata:
|
| 88 |
+
lines.append(f"Function: {function_data['name']}")
|
| 89 |
+
lines.append(f"Documentation: {function_data['doc']}")
|
| 90 |
+
|
| 91 |
+
args = function_data.get('args', [])
|
| 92 |
+
if args:
|
| 93 |
+
lines.append("Arguments:")
|
| 94 |
+
for arg in args:
|
| 95 |
+
default = f" (default: {arg['default']})" if arg['default'] is not None else ""
|
| 96 |
+
lines.append(f" - {arg['name']} (type: {arg['type']}){default}")
|
| 97 |
+
else:
|
| 98 |
+
lines.append("Arguments: None")
|
| 99 |
+
|
| 100 |
+
lines.append("#########")
|
| 101 |
+
|
| 102 |
+
return '\n'.join(lines)
|
| 103 |
def _read_py_code_library(self, file_location):
|
| 104 |
+
metadata = self._get_pyfile_functions_metadata_from_file(file_location)
|
| 105 |
if len(metadata) == 0:
|
| 106 |
return "No functions yet."
|
| 107 |
+
formatted_metadata = self._format_metadata(metadata)
|
| 108 |
+
return formatted_metadata
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
def run(
|
| 111 |
self,
|