@@ -10,14 +10,15 @@ def return_attribute(value: str, attr: str) -> ast.Attribute:
10
10
return ast .Attribute (
11
11
value = ast .Name (id = value , ctx = ast .Load ()),
12
12
attr = attr ,
13
- ctx = ast .Load ())
13
+ ctx = ast .Load (),
14
+ )
14
15
15
16
16
17
def update_annotation (
17
18
resource_name : str ,
18
19
annotation : Union [ast .Name , ast .Subscript ],
19
20
nodes : Set [str ],
20
- parent : str
21
+ parent : str ,
21
22
) -> Union [ast .Attribute , ast .Subscript ]:
22
23
if isinstance (annotation , ast .Name ) and annotation .id in nodes :
23
24
value = parent if parent else resource_name
@@ -27,48 +28,43 @@ def update_annotation(
27
28
resource_name ,
28
29
annotation .slice ,
29
30
nodes ,
30
- parent )
31
+ parent ,
32
+ )
31
33
return annotation
32
34
33
35
34
36
def replace_async_func (
35
- resource_name : str ,
36
- func : ast .AsyncFunctionDef ,
37
- nodes : Set [str ],
38
- parent : str = ""
37
+ resource_name : str , func : ast .AsyncFunctionDef , nodes : Set [str ], parent : str = ""
39
38
) -> None :
40
39
for arg in func .args .args :
41
- arg .annotation = update_annotation (
42
- resource_name ,
43
- arg .annotation ,
44
- nodes ,
45
- parent )
40
+ arg .annotation = update_annotation (resource_name , arg .annotation , nodes , parent )
46
41
func .body = [
47
- ast .Expr (ast .Call (
48
- func = ast .Attribute (
49
- value = ast .Attribute (
50
- ast .Name (id = "self" ),
51
- attr = "logger" ),
52
- attr = "error" ,
53
- ),
54
- args = [ast .Constant (value = f"`{ func .name } ` is not implemented" )],
55
- keywords = [])),
42
+ ast .Expr (
43
+ ast .Call (
44
+ func = ast .Attribute (
45
+ value = ast .Attribute (ast .Name (id = "self" ), attr = "logger" ),
46
+ attr = "error" ,
47
+ ),
48
+ args = [ast .Constant (value = f"`{ func .name } ` is not implemented" )],
49
+ keywords = [],
50
+ )
51
+ ),
56
52
ast .Raise (
57
- exc = ast .Call (func = ast .Name (id = 'NotImplementedError' ,
58
- ctx = ast .Load ()),
59
- args = [],
60
- keywords = []),
61
- cause = None )
53
+ exc = ast .Call (
54
+ func = ast .Name (id = "NotImplementedError" , ctx = ast .Load ()),
55
+ args = [],
56
+ keywords = [],
57
+ ),
58
+ cause = None ,
59
+ ),
62
60
]
63
61
func .decorator_list = []
64
62
if isinstance (func .returns , (ast .Name , ast .Subscript )):
65
- func .returns = update_annotation (
66
- resource_name , func .returns , nodes , parent
67
- )
63
+ func .returns = update_annotation (resource_name , func .returns , nodes , parent )
68
64
69
65
70
66
def return_subclass (
71
- resource_name : str , stmt : ast .ClassDef , parent : str = ""
67
+ resource_name : str , stmt : ast .ClassDef , parent : str = ""
72
68
) -> List [str ]:
73
69
def parse_subclass (resource_name : str , stmt : ast .ClassDef , parent : str ):
74
70
nodes = set ()
@@ -93,9 +89,7 @@ def parse_subclass(resource_name: str, stmt: ast.ClassDef, parent: str):
93
89
stmt .body = [ast .Pass ()]
94
90
95
91
parse_subclass (resource_name , stmt , parent )
96
- return '\n ' .join (
97
- [' ' + line for line in ast .unparse (stmt ).splitlines ()]
98
- )
92
+ return "\n " .join ([" " + line for line in ast .unparse (stmt ).splitlines ()])
99
93
100
94
101
95
def main (
@@ -108,14 +102,12 @@ def main(
108
102
import isort
109
103
from slugify import slugify
110
104
111
- module_name = (
112
- f"viam.{ resource_type } s.{ resource_subtype } .{ resource_subtype } "
113
- )
105
+ module_name = f"viam.{ resource_type } s.{ resource_subtype } .{ resource_subtype } "
114
106
module = import_module (module_name )
115
- resource_name = {
116
- "input" : "Controller" , "slam" : "SLAM" , "mlmodel" : "MLModel"
117
- }. get ( resource_subtype , "" .join (word .capitalize ()
118
- for word in resource_subtype . split ( "_" )) )
107
+ resource_name = {"input" : "Controller" , "slam" : "SLAM" , "mlmodel" : "MLModel" }. get (
108
+ resource_subtype ,
109
+ "" .join (word .capitalize () for word in resource_subtype . split ( "_" )),
110
+ )
119
111
120
112
imports , subclasses , abstract_methods = [], [], []
121
113
nodes = set ()
@@ -132,8 +124,11 @@ def main(
132
124
for imp in stmt .names :
133
125
if imp .name in modules_to_ignore :
134
126
continue
135
- imports .append (f"import { imp .name } as { imp .asname } "
136
- if imp .asname else f"import { imp .name } " )
127
+ imports .append (
128
+ f"import { imp .name } as { imp .asname } "
129
+ if imp .asname
130
+ else f"import { imp .name } "
131
+ )
137
132
elif (
138
133
isinstance (stmt , ast .ImportFrom )
139
134
and stmt .module
@@ -159,8 +154,8 @@ def main(
159
154
nodes .add (cstmt .target .id )
160
155
elif isinstance (cstmt , ast .AsyncFunctionDef ):
161
156
replace_async_func (resource_name , cstmt , nodes )
162
- indented_code = ' \n ' .join (
163
- [' ' + line for line in ast .unparse (cstmt ).splitlines ()]
157
+ indented_code = " \n " .join (
158
+ [" " + line for line in ast .unparse (cstmt ).splitlines ()]
164
159
)
165
160
abstract_methods .append (indented_code )
166
161
@@ -172,22 +167,24 @@ def main(
172
167
for cstmt in stmt .body :
173
168
if isinstance (cstmt , ast .AsyncFunctionDef ):
174
169
replace_async_func ("" , cstmt , [])
175
- indented_code = ' \n ' .join (
176
- [' ' + line for line in ast .unparse (cstmt ).splitlines ()]
170
+ indented_code = " \n " .join (
171
+ [" " + line for line in ast .unparse (cstmt ).splitlines ()]
177
172
)
178
173
abstract_methods .append (indented_code )
179
174
if cstmt .name == "do_command" :
180
175
imports .append ("from typing import Optional" )
181
176
imports .append ("from viam.utils import ValueTypes" )
182
177
elif cstmt .name == "get_geometries" :
183
- imports .append ("from typing import Any, Dict, List, Optional" )
178
+ imports .append (
179
+ "from typing import Any, Dict, List, Optional"
180
+ )
184
181
imports .append ("from viam.proto.common import Geometry" )
185
182
186
183
model_name_pascal = "" .join (
187
184
[word .capitalize () for word in slugify (model_name ).split ("-" )]
188
185
)
189
186
resource_file = '''
190
- from typing import ClassVar, Mapping, Sequence
187
+ from typing import ClassVar, Mapping, Sequence, Tuple
191
188
from typing_extensions import Self
192
189
from viam.proto.app.robot import ComponentConfig
193
190
from viam.proto.common import ResourceName
@@ -218,17 +215,19 @@ def new(cls, config: ComponentConfig, dependencies: Mapping[ResourceName, Resour
218
215
return super().new(config, dependencies)
219
216
220
217
@classmethod
221
- def validate_config(cls, config: ComponentConfig) -> Sequence[str]:
218
+ def validate_config(cls, config: ComponentConfig) -> Tuple[ Sequence[str], Sequence[str] ]:
222
219
"""This method allows you to validate the configuration object received from the machine,
223
- as well as to return any implicit dependencies based on that `config`.
220
+ as well as to return any required dependencies or optional dependencies based on that `config`.
224
221
225
222
Args:
226
223
config (ComponentConfig): The configuration for this resource
227
224
228
225
Returns:
229
- Sequence[str]: A list of implicit dependencies
226
+ Tuple[Sequence[str], Sequence[str]]: A tuple where the
227
+ first element is a list of required dependencies and the
228
+ second element is a list of optional dependencies
230
229
"""
231
- return []
230
+ return [], []
232
231
233
232
def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]):
234
233
"""This method allows you to dynamically update your service when it receives a new `config` object.
@@ -250,8 +249,8 @@ def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceNam
250
249
namespace ,
251
250
mod_name ,
252
251
model_name ,
253
- ' \n \n ' .join ([subclass for subclass in subclasses ]),
254
- ' \n \n ' .join ([f' { method } ' for method in abstract_methods ]),
252
+ " \n \n " .join ([subclass for subclass in subclasses ]),
253
+ " \n \n " .join ([f" { method } " for method in abstract_methods ]),
255
254
)
256
255
f_name = os .path .join (mod_name , "src" , "models" , "resource.py" )
257
256
with open (f_name , "w+" ) as f :
@@ -274,12 +273,7 @@ def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceNam
274
273
if sys .argv [2 ] == "mlmodel" :
275
274
packages .append ("numpy" )
276
275
install_res = subprocess .run (
277
- [
278
- sys .executable ,
279
- "-m" ,
280
- "pip" ,
281
- "install"
282
- ] + packages ,
276
+ [sys .executable , "-m" , "pip" , "install" ] + packages ,
283
277
capture_output = True ,
284
278
)
285
279
if install_res .returncode != 0 :
0 commit comments