-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
96 lines (69 loc) · 2.91 KB
/
utils.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
from collections import namedtuple
from flask import request, jsonify
import regex as re
import json
class FunctionNotFoundError(Exception):
def __init__(self, function_name, function_args):
self.function_name = function_name
self.function_args = function_args
super().__init__(
f"Error finding function {function_name} with arguments {function_args}")
class FunctionExecutionError(Exception):
def __init__(self, function_name, function_args):
self.function_name = function_name
self.function_args = function_args
super().__init__(
f"Error executing function {function_name} with arguments {function_args}")
def match_functions_with_metadata(functions, metadata_list):
FunctionMetadataPair = namedtuple(
'FunctionMetadataPair', ['function', 'meta_data'])
function_dict = {func.name: func for func in functions}
matched_metadata = {}
for metadata in metadata_list:
func_name = metadata.get('name')
if func_name in function_dict:
matched_metadata[func_name] = FunctionMetadataPair(
function=function_dict[func_name],
meta_data=metadata
)
else:
print(f"No function found for metadata with name: {func_name}")
return matched_metadata
def handle_function(cls, function, **kwargs) -> json:
"""Invoke function and return result"""
try:
return json.dumps(function(cls, **kwargs))
except Exception as e:
raise FunctionExecutionError(function, kwargs)
def extract_properties(metadata):
properties_info = metadata.get('parameters', {}).get('properties', {})
extracted_properties = {}
for prop_name, prop_details in properties_info.items():
prop_type = prop_details.get('type', 'unknown')
extracted_properties[prop_name] = prop_type
return extracted_properties
def convert_args(function_name, functions_and_metadata):
try:
metadata = functions_and_metadata.get(
function_name).meta_data
except KeyError as error:
raise FunctionNotFoundError
expected_properties = extract_properties(metadata)
request_args = request.args.to_dict()
converted_args = {}
for arg_name, arg_value in request_args.items():
expected_type = expected_properties.get(arg_name)
if not expected_type:
continue
try:
if expected_type == 'number':
match_float = re.match("\d+.\d+", arg_value)
if match_float:
converted_args[arg_name] = float(arg_value)
else:
converted_args[arg_name] = int(arg_value)
elif expected_type == 'string':
converted_args[arg_name] = str(arg_value)
except ValueError as e:
return jsonify({"error": f"Invalid value for parameter '{arg_name}': {e}"}), 400
return converted_args