stores 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stores/indexes/index.py +7 -2
- stores/indexes/remote_index.py +22 -2
- stores/indexes/venv_utils.py +34 -15
- {stores-0.1.1.dist-info → stores-0.1.3.dist-info}/METADATA +1 -1
- {stores-0.1.1.dist-info → stores-0.1.3.dist-info}/RECORD +7 -7
- {stores-0.1.1.dist-info → stores-0.1.3.dist-info}/WHEEL +0 -0
- {stores-0.1.1.dist-info → stores-0.1.3.dist-info}/licenses/LICENSE +0 -0
stores/indexes/index.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import logging
|
2
2
|
import os
|
3
3
|
from pathlib import Path
|
4
|
-
from typing import Callable
|
4
|
+
from typing import Callable, Optional
|
5
5
|
|
6
6
|
from stores.indexes.base_index import BaseIndex
|
7
7
|
from stores.indexes.local_index import LocalIndex
|
@@ -17,6 +17,8 @@ class Index(BaseIndex):
|
|
17
17
|
self,
|
18
18
|
tools: list[Callable, os.PathLike] | None = None,
|
19
19
|
env_var: dict[str, dict] | None = None,
|
20
|
+
cache_dir: Optional[os.PathLike] = None,
|
21
|
+
reset_cache=False,
|
20
22
|
):
|
21
23
|
self.env_var = env_var or {}
|
22
24
|
tools = tools or []
|
@@ -38,7 +40,10 @@ class Index(BaseIndex):
|
|
38
40
|
# Load RemoteIndex
|
39
41
|
try:
|
40
42
|
loaded_index = RemoteIndex(
|
41
|
-
index_name,
|
43
|
+
index_name,
|
44
|
+
env_var=self.env_var.get(index_name),
|
45
|
+
cache_dir=cache_dir,
|
46
|
+
reset_cache=reset_cache,
|
42
47
|
)
|
43
48
|
except Exception:
|
44
49
|
logger.warning(
|
stores/indexes/remote_index.py
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
import json
|
2
2
|
import logging
|
3
|
+
import shutil
|
3
4
|
import venv
|
5
|
+
from os import PathLike
|
4
6
|
from pathlib import Path
|
7
|
+
from typing import Optional
|
5
8
|
|
6
9
|
import requests
|
7
10
|
from git import Repo
|
@@ -21,6 +24,10 @@ INDEX_LOOKUP_URL = (
|
|
21
24
|
)
|
22
25
|
|
23
26
|
|
27
|
+
def clear_default_cache():
|
28
|
+
shutil.rmtree(CACHE_DIR)
|
29
|
+
|
30
|
+
|
24
31
|
def lookup_index(index_id: str, index_version: str | None = None):
|
25
32
|
response = requests.post(
|
26
33
|
INDEX_LOOKUP_URL,
|
@@ -39,11 +46,24 @@ def lookup_index(index_id: str, index_version: str | None = None):
|
|
39
46
|
|
40
47
|
|
41
48
|
class RemoteIndex(BaseIndex):
|
42
|
-
def __init__(
|
49
|
+
def __init__(
|
50
|
+
self,
|
51
|
+
index_id: str,
|
52
|
+
env_var: dict | None = None,
|
53
|
+
cache_dir: Optional[PathLike] = None,
|
54
|
+
reset_cache=False,
|
55
|
+
):
|
43
56
|
self.index_id = index_id
|
44
|
-
|
57
|
+
if cache_dir is None:
|
58
|
+
cache_dir = CACHE_DIR
|
59
|
+
else:
|
60
|
+
cache_dir = Path(cache_dir)
|
61
|
+
if reset_cache:
|
62
|
+
shutil.rmtree(cache_dir)
|
63
|
+
self.index_folder = cache_dir / self.index_id
|
45
64
|
self.env_var = env_var or {}
|
46
65
|
if not self.index_folder.exists():
|
66
|
+
logger.info(f"Installing {index_id}...")
|
47
67
|
commit_like = None
|
48
68
|
if ":" in index_id:
|
49
69
|
index_id, commit_like = index_id.split(":")
|
stores/indexes/venv_utils.py
CHANGED
@@ -93,6 +93,7 @@ def init_venv_tools(index_folder: os.PathLike, env_var: dict | None = None):
|
|
93
93
|
tool_id=tool_id,
|
94
94
|
index_folder=index_folder,
|
95
95
|
venv=VENV_NAME,
|
96
|
+
env_var=env_var,
|
96
97
|
)
|
97
98
|
tool = parse_tool_signature(
|
98
99
|
signature_dict=tool_sig,
|
@@ -105,9 +106,15 @@ def init_venv_tools(index_folder: os.PathLike, env_var: dict | None = None):
|
|
105
106
|
|
106
107
|
|
107
108
|
# TODO: Sanitize tool_id, args, and kwargs
|
108
|
-
def get_tool_signature(
|
109
|
+
def get_tool_signature(
|
110
|
+
tool_id: str,
|
111
|
+
index_folder: os.PathLike,
|
112
|
+
venv: str = VENV_NAME,
|
113
|
+
env_var: dict | None = None,
|
114
|
+
):
|
109
115
|
module_name = ".".join(tool_id.split(".")[:-1])
|
110
116
|
tool_name = tool_id.split(".")[-1]
|
117
|
+
env_var = env_var or {}
|
111
118
|
|
112
119
|
runner = f"""
|
113
120
|
import pickle, sys, traceback, inspect, enum
|
@@ -115,44 +122,49 @@ from typing import Any, Dict, List, Literal, Tuple, Union, get_args, get_origin,
|
|
115
122
|
import types as T
|
116
123
|
|
117
124
|
|
118
|
-
def extract_type_info(typ):
|
125
|
+
def extract_type_info(typ, custom_types: list[str] | None = None):
|
126
|
+
custom_types = custom_types or []
|
127
|
+
if hasattr(typ, "__name__") and typ.__name__ in custom_types:
|
128
|
+
return typ.__name__
|
119
129
|
origin = get_origin(typ)
|
120
130
|
args = list(get_args(typ))
|
121
131
|
if origin is Literal:
|
122
132
|
return {{"type": "Literal", "values": args}}
|
123
133
|
elif inspect.isclass(typ) and issubclass(typ, enum.Enum):
|
134
|
+
custom_types.append(typ.__name__)
|
124
135
|
return {{
|
125
136
|
"type": "Enum",
|
126
137
|
"type_name": typ.__name__,
|
127
138
|
"values": {{v.name: v.value for v in typ}},
|
128
139
|
}}
|
129
140
|
elif isinstance(typ, type) and typ.__class__.__name__ == "_TypedDictMeta":
|
141
|
+
custom_types.append(typ.__name__)
|
130
142
|
hints = get_type_hints(typ)
|
131
143
|
return {{
|
132
144
|
"type": "TypedDict",
|
133
145
|
"type_name": typ.__name__,
|
134
|
-
"fields": {{k: extract_type_info(v) for k, v in hints.items()}}
|
146
|
+
"fields": {{k: extract_type_info(v, custom_types) for k, v in hints.items()}}
|
135
147
|
}}
|
136
148
|
elif origin in (list, List) or typ is list:
|
137
149
|
return {{
|
138
150
|
"type": "List",
|
139
|
-
"item_type": extract_type_info(args[0]) if args else {{"type": Any}}
|
151
|
+
"item_type": extract_type_info(args[0], custom_types) if args else {{"type": Any}}
|
140
152
|
}}
|
141
153
|
elif origin in (dict, Dict) or typ is dict:
|
142
154
|
return {{
|
143
155
|
"type": "Dict",
|
144
|
-
"key_type": extract_type_info(args[0]) if args else {{"type": Any}},
|
145
|
-
"value_type": extract_type_info(args[1]) if len(args) > 1 else {{"type": Any}}
|
156
|
+
"key_type": extract_type_info(args[0], custom_types) if args else {{"type": Any}},
|
157
|
+
"value_type": extract_type_info(args[1], custom_types) if len(args) > 1 else {{"type": Any}}
|
146
158
|
}}
|
147
159
|
elif origin in (tuple, Tuple) or typ is tuple:
|
148
160
|
return {{
|
149
161
|
"type": "Tuple",
|
150
|
-
"item_types": [extract_type_info(arg) for arg in args] if args else [{{"type": Any}}]
|
162
|
+
"item_types": [extract_type_info(arg, custom_types) for arg in args] if args else [{{"type": Any}}]
|
151
163
|
}}
|
152
164
|
elif origin is Union or origin is T.UnionType:
|
153
165
|
return {{
|
154
166
|
"type": "Union",
|
155
|
-
"options": [extract_type_info(arg) for arg in args]
|
167
|
+
"options": [extract_type_info(arg, custom_types) for arg in args]
|
156
168
|
}}
|
157
169
|
else:
|
158
170
|
return {{"type": typ}}
|
@@ -192,6 +204,7 @@ except Exception as e:
|
|
192
204
|
[f"{venv}/bin/python", "-c", runner],
|
193
205
|
capture_output=True,
|
194
206
|
cwd=index_folder,
|
207
|
+
env=env_var,
|
195
208
|
)
|
196
209
|
try:
|
197
210
|
response = pickle.loads(result.stdout)
|
@@ -205,33 +218,39 @@ except Exception as e:
|
|
205
218
|
raise RuntimeError(f"Error loading tool {tool_id}:\n{response['error']}")
|
206
219
|
|
207
220
|
|
208
|
-
def parse_param_type(param_info: dict):
|
221
|
+
def parse_param_type(param_info: dict, custom_types: list[str] | None = None):
|
222
|
+
custom_types = custom_types or []
|
223
|
+
# Support ForwardRef
|
209
224
|
param_type = param_info["type"]
|
225
|
+
if param_type in custom_types:
|
226
|
+
return param_type
|
210
227
|
if not isinstance(param_type, str):
|
211
228
|
return param_type
|
212
229
|
if param_type == "Literal":
|
213
230
|
return Literal.__getitem__(tuple(param_info["values"]))
|
214
231
|
elif param_type == "Enum":
|
232
|
+
custom_types.append(param_info["type_name"])
|
215
233
|
return Enum(param_info["type_name"], param_info["values"])
|
216
234
|
elif param_type == "TypedDict":
|
235
|
+
custom_types.append(param_info["type_name"])
|
217
236
|
properties = {}
|
218
237
|
for k, v in param_info["fields"].items():
|
219
|
-
properties[k] = parse_param_type(v)
|
238
|
+
properties[k] = parse_param_type(v, custom_types)
|
220
239
|
return TypedDict(param_info["type_name"], properties)
|
221
240
|
elif param_type == "List":
|
222
|
-
return list[parse_param_type(param_info["item_type"])]
|
241
|
+
return list[parse_param_type(param_info["item_type"], custom_types)]
|
223
242
|
elif param_type == "Dict":
|
224
243
|
return Dict[
|
225
|
-
parse_param_type(param_info["key_type"]),
|
226
|
-
parse_param_type(param_info["value_type"]),
|
244
|
+
parse_param_type(param_info["key_type"], custom_types),
|
245
|
+
parse_param_type(param_info["value_type"], custom_types),
|
227
246
|
]
|
228
247
|
elif param_type == "Tuple":
|
229
248
|
return Tuple.__getitem__(
|
230
|
-
tuple([parse_param_type(i) for i in param_info["item_types"]])
|
249
|
+
tuple([parse_param_type(i, custom_types) for i in param_info["item_types"]])
|
231
250
|
)
|
232
251
|
elif param_type == "Union":
|
233
252
|
return Union.__getitem__(
|
234
|
-
tuple([parse_param_type(i) for i in param_info["options"]])
|
253
|
+
tuple([parse_param_type(i, custom_types) for i in param_info["options"]])
|
235
254
|
)
|
236
255
|
else:
|
237
256
|
raise TypeError(f"Invalid param type {param_type} in param info {param_info}")
|
@@ -5,11 +5,11 @@ stores/parse.py,sha256=HYPNPzQod2vpu1Cln7yQ8aVkZT1Mw2IN0sZ2A1DIaqE,4967
|
|
5
5
|
stores/utils.py,sha256=GPWT6lCoGobwP3PlEOHyJfKyd0dobamjyErcR7lgm7M,242
|
6
6
|
stores/indexes/__init__.py,sha256=s-RNqml8uGREQhxwSdDoxcbcxeD8soB9BcL5dBKsQfI,215
|
7
7
|
stores/indexes/base_index.py,sha256=K4--_OAFyPNESaisD0JPNRIEffCnjw9nziahDgjdZHU,10708
|
8
|
-
stores/indexes/index.py,sha256=
|
8
|
+
stores/indexes/index.py,sha256=X8q2Pe6PCmzQ9j0KVvJmqZid1A1RB0jUe-JugcOrl7o,2330
|
9
9
|
stores/indexes/local_index.py,sha256=NS4AWzeMVZ51YPo0eJ_udbF5aASuksG-88YXLDn41fQ,2880
|
10
|
-
stores/indexes/remote_index.py,sha256
|
11
|
-
stores/indexes/venv_utils.py,sha256=
|
12
|
-
stores-0.1.
|
13
|
-
stores-0.1.
|
14
|
-
stores-0.1.
|
15
|
-
stores-0.1.
|
10
|
+
stores/indexes/remote_index.py,sha256=dp84keyEDyL-cQrmshwHd4QqDGMw0CTSJcQxqZQiULY,2983
|
11
|
+
stores/indexes/venv_utils.py,sha256=U69UsHn9APTc615TchW2aZmrBORgbZhuZunv4zQv_hU,12514
|
12
|
+
stores-0.1.3.dist-info/METADATA,sha256=4ta-OcEHDgCulY7lk_yxkBb_JDOxbY0keiLisgPS8_U,3076
|
13
|
+
stores-0.1.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
14
|
+
stores-0.1.3.dist-info/licenses/LICENSE,sha256=VTidYE7_Dam0Dwyq095EhhDIqi47g03oVpLAHQgKws0,1066
|
15
|
+
stores-0.1.3.dist-info/RECORD,,
|
File without changes
|
File without changes
|