kcl-lib 0.9.0b1__cp39-none-win_amd64.whl → 0.9.2__cp39-none-win_amd64.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.
Potentially problematic release.
This version of kcl-lib might be problematic. Click here for more details.
- kcl_lib/_kcl_lib.cp39-win_amd64.pyd +0 -0
- kcl_lib/api/__init__.py +135 -2
- kcl_lib/api/service.py +657 -48
- kcl_lib/api/spec_pb2.py +208 -187
- kcl_lib/api/spec_pb2.pyi +1164 -0
- kcl_lib/plugin/__init__.py +3 -0
- kcl_lib/plugin/plugin.py +82 -0
- kcl_lib-0.9.2.dist-info/METADATA +722 -0
- kcl_lib-0.9.2.dist-info/RECORD +11 -0
- {kcl_lib-0.9.0b1.dist-info → kcl_lib-0.9.2.dist-info}/WHEEL +1 -1
- kcl_lib-0.9.0b1.dist-info/METADATA +0 -72
- kcl_lib-0.9.0b1.dist-info/RECORD +0 -8
kcl_lib/api/service.py
CHANGED
|
@@ -1,5 +1,49 @@
|
|
|
1
1
|
import kcl_lib
|
|
2
|
-
|
|
2
|
+
import kcl_lib.plugin as plugin
|
|
3
|
+
from .spec_pb2 import (
|
|
4
|
+
Ping_Args,
|
|
5
|
+
Ping_Result,
|
|
6
|
+
GetVersion_Args,
|
|
7
|
+
GetVersion_Result,
|
|
8
|
+
ParseFile_Args,
|
|
9
|
+
ParseFile_Result,
|
|
10
|
+
ParseProgram_Args,
|
|
11
|
+
ParseProgram_Result,
|
|
12
|
+
LoadPackage_Args,
|
|
13
|
+
LoadPackage_Result,
|
|
14
|
+
ListOptions_Result,
|
|
15
|
+
ExecProgram_Args,
|
|
16
|
+
ExecProgram_Result,
|
|
17
|
+
BuildProgram_Args,
|
|
18
|
+
BuildProgram_Result,
|
|
19
|
+
ExecArtifact_Args,
|
|
20
|
+
FormatCode_Args,
|
|
21
|
+
FormatCode_Result,
|
|
22
|
+
FormatPath_Args,
|
|
23
|
+
FormatPath_Result,
|
|
24
|
+
LintPath_Args,
|
|
25
|
+
LintPath_Result,
|
|
26
|
+
OverrideFile_Args,
|
|
27
|
+
OverrideFile_Result,
|
|
28
|
+
ListVariables_Args,
|
|
29
|
+
ListVariables_Result,
|
|
30
|
+
GetSchemaTypeMapping_Args,
|
|
31
|
+
GetSchemaTypeMapping_Result,
|
|
32
|
+
ValidateCode_Args,
|
|
33
|
+
ValidateCode_Result,
|
|
34
|
+
ListDepFiles_Args,
|
|
35
|
+
ListDepFiles_Result,
|
|
36
|
+
LoadSettingsFiles_Args,
|
|
37
|
+
LoadSettingsFiles_Result,
|
|
38
|
+
Rename_Args,
|
|
39
|
+
Rename_Result,
|
|
40
|
+
RenameCode_Args,
|
|
41
|
+
RenameCode_Result,
|
|
42
|
+
Test_Args,
|
|
43
|
+
Test_Result,
|
|
44
|
+
UpdateDependencies_Args,
|
|
45
|
+
UpdateDependencies_Result,
|
|
46
|
+
)
|
|
3
47
|
from google.protobuf import message as _message
|
|
4
48
|
|
|
5
49
|
|
|
@@ -19,16 +63,82 @@ class API:
|
|
|
19
63
|
```
|
|
20
64
|
"""
|
|
21
65
|
|
|
22
|
-
def __init__(self, plugin_agent: int =
|
|
66
|
+
def __init__(self, plugin_agent: int = plugin.plugin_agent_addr):
|
|
23
67
|
self.plugin_agent = plugin_agent
|
|
24
68
|
|
|
25
69
|
def ping(self, args: Ping_Args) -> Ping_Result:
|
|
26
70
|
return self.call("KclvmService.Ping", args)
|
|
27
71
|
|
|
28
72
|
def parse_program(self, args: ParseProgram_Args) -> ParseProgram_Result:
|
|
73
|
+
"""Parse KCL program with entry files and return the AST JSON string.
|
|
74
|
+
|
|
75
|
+
## Example
|
|
76
|
+
|
|
77
|
+
The content of `schema.k` is
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
schema AppConfig:
|
|
81
|
+
replicas: int
|
|
82
|
+
|
|
83
|
+
app: AppConfig {
|
|
84
|
+
replicas: 2
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Python Code
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
import kcl_lib.api as api
|
|
92
|
+
|
|
93
|
+
args = api.ParseProgram_Args(paths=["schema.k"])
|
|
94
|
+
api = api.API()
|
|
95
|
+
result = api.parse_program(args)
|
|
96
|
+
assert len(result.paths) == 1
|
|
97
|
+
assert len(result.errors) == 0
|
|
98
|
+
```
|
|
99
|
+
"""
|
|
29
100
|
return self.call("KclvmService.ParseProgram", args)
|
|
30
101
|
|
|
31
102
|
def exec_program(self, args: ExecProgram_Args) -> ExecProgram_Result:
|
|
103
|
+
"""Execute KCL file with arguments and return the JSON/YAML result.
|
|
104
|
+
|
|
105
|
+
## Examples
|
|
106
|
+
|
|
107
|
+
The content of `schema.k` is
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
schema AppConfig:
|
|
111
|
+
replicas: int
|
|
112
|
+
|
|
113
|
+
app: AppConfig {
|
|
114
|
+
replicas: 2
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Python Code
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
import kcl_lib.api as api
|
|
122
|
+
|
|
123
|
+
args = api.ExecProgram_Args(k_filename_list=["schema.k"])
|
|
124
|
+
api = api.API()
|
|
125
|
+
result = api.exec_program(args)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
A case with the file not found error
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
import kcl_lib.api as api
|
|
132
|
+
|
|
133
|
+
try:
|
|
134
|
+
args = api.ExecProgram_Args(k_filename_list=["file_not_found"])
|
|
135
|
+
api = api.API()
|
|
136
|
+
result = api.exec_program(args)
|
|
137
|
+
assert False
|
|
138
|
+
except Exception as err:
|
|
139
|
+
assert "Cannot find the kcl file" in str(err)
|
|
140
|
+
```
|
|
141
|
+
"""
|
|
32
142
|
return self.call("KclvmService.ExecProgram", args)
|
|
33
143
|
|
|
34
144
|
def build_program(self, args: BuildProgram_Args) -> BuildProgram_Result:
|
|
@@ -38,70 +148,565 @@ class API:
|
|
|
38
148
|
return self.call("KclvmService.ExecArtifact", args)
|
|
39
149
|
|
|
40
150
|
def parse_file(self, args: ParseFile_Args) -> ParseFile_Result:
|
|
151
|
+
"""Parse KCL single file to Module AST JSON string with import dependencies and parse errors.
|
|
152
|
+
|
|
153
|
+
## Example
|
|
154
|
+
|
|
155
|
+
The content of `schema.k` is
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
schema AppConfig:
|
|
159
|
+
replicas: int
|
|
160
|
+
|
|
161
|
+
app: AppConfig {
|
|
162
|
+
replicas: 2
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Python Code
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
import kcl_lib.api as api
|
|
170
|
+
|
|
171
|
+
args = api.ParseProgram_Args(paths=[TEST_FILE])
|
|
172
|
+
api = api.API()
|
|
173
|
+
result = api.parse_program(args)
|
|
174
|
+
assert len(result.paths) == 1
|
|
175
|
+
assert len(result.errors) == 0
|
|
176
|
+
```
|
|
177
|
+
"""
|
|
41
178
|
return self.call("KclvmService.ParseFile", args)
|
|
42
179
|
|
|
43
180
|
def parse_program(self, args: ParseProgram_Args) -> ParseProgram_Result:
|
|
181
|
+
"""Parse KCL program with entry files and return the AST JSON string.
|
|
182
|
+
|
|
183
|
+
## Example
|
|
184
|
+
|
|
185
|
+
The content of `schema.k` is
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
schema AppConfig:
|
|
189
|
+
replicas: int
|
|
190
|
+
|
|
191
|
+
app: AppConfig {
|
|
192
|
+
replicas: 2
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Python Code
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
import kcl_lib.api as api
|
|
200
|
+
|
|
201
|
+
args = api.ParseProgram_Args(paths=["schema.k"])
|
|
202
|
+
api = api.API()
|
|
203
|
+
result = api.parse_program(args)
|
|
204
|
+
assert len(result.paths) == 1
|
|
205
|
+
assert len(result.errors) == 0
|
|
206
|
+
```
|
|
207
|
+
"""
|
|
44
208
|
return self.call("KclvmService.ParseProgram", args)
|
|
45
209
|
|
|
46
210
|
def load_package(self, args: LoadPackage_Args) -> LoadPackage_Result:
|
|
211
|
+
"""load_package provides users with the ability to parse KCL program and semantic model information including symbols, types, definitions, etc.
|
|
212
|
+
|
|
213
|
+
## Example
|
|
214
|
+
|
|
215
|
+
The content of `schema.k` is
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
schema AppConfig:
|
|
219
|
+
replicas: int
|
|
220
|
+
|
|
221
|
+
app: AppConfig {
|
|
222
|
+
replicas: 2
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Python Code
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
import kcl_lib.api as api
|
|
230
|
+
|
|
231
|
+
args = api.LoadPackage_Args(
|
|
232
|
+
parse_args=api.ParseProgram_Args(paths=["schema.k"]), resolve_ast=True
|
|
233
|
+
)
|
|
234
|
+
api = api.API()
|
|
235
|
+
result = api.load_package(args)
|
|
236
|
+
assert list(result.symbols.values())[0].ty.schema_name == "AppConfig"
|
|
237
|
+
```
|
|
238
|
+
"""
|
|
47
239
|
return self.call("KclvmService.LoadPackage", args)
|
|
48
240
|
|
|
49
241
|
def list_options(self, args: ParseProgram_Args) -> ListOptions_Result:
|
|
242
|
+
"""list_options provides users with the ability to parse KCL program and get all option information.
|
|
243
|
+
|
|
244
|
+
## Example
|
|
245
|
+
|
|
246
|
+
The content of `options.k` is
|
|
247
|
+
|
|
248
|
+
```python
|
|
249
|
+
a = option("key1")
|
|
250
|
+
b = option("key2", required=True)
|
|
251
|
+
c = {
|
|
252
|
+
metadata.key = option("metadata-key")
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
Python Code
|
|
257
|
+
|
|
258
|
+
```python
|
|
259
|
+
import kcl_lib.api as api
|
|
260
|
+
|
|
261
|
+
args = api.ParseProgram_Args(paths=["options.k"])
|
|
262
|
+
api = api.API()
|
|
263
|
+
result = api.list_options(args)
|
|
264
|
+
assert len(result.options) == 3
|
|
265
|
+
assert result.options[0].name == "key1"
|
|
266
|
+
assert result.options[1].name == "key2"
|
|
267
|
+
assert result.options[2].name == "metadata-key"
|
|
268
|
+
```
|
|
269
|
+
"""
|
|
50
270
|
return self.call("KclvmService.ListOptions", args)
|
|
51
271
|
|
|
52
272
|
def list_variables(self, args: ListVariables_Args) -> ListVariables_Result:
|
|
273
|
+
"""list_variables provides users with the ability to parse KCL program and get all variables by specs.
|
|
274
|
+
|
|
275
|
+
## Example
|
|
276
|
+
|
|
277
|
+
The content of `schema.k` is
|
|
278
|
+
|
|
279
|
+
```python
|
|
280
|
+
schema AppConfig:
|
|
281
|
+
replicas: int
|
|
282
|
+
|
|
283
|
+
app: AppConfig {
|
|
284
|
+
replicas: 2
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
Python Code
|
|
289
|
+
|
|
290
|
+
```python
|
|
291
|
+
import kcl_lib.api as api
|
|
292
|
+
|
|
293
|
+
args = api.ListVariables_Args(files=[TEST_FILE])
|
|
294
|
+
api = api.API()
|
|
295
|
+
result = api.list_variables(args)
|
|
296
|
+
assert result.variables["app"].variables[0].value == "AppConfig {replicas: 2}"
|
|
297
|
+
```
|
|
298
|
+
"""
|
|
53
299
|
return self.call("KclvmService.ListVariables", args)
|
|
54
300
|
|
|
55
301
|
def format_code(self, args: FormatCode_Args) -> FormatCode_Result:
|
|
302
|
+
"""Format the code source.
|
|
303
|
+
|
|
304
|
+
<details><summary>Example</summary>
|
|
305
|
+
<p>
|
|
306
|
+
|
|
307
|
+
Python Code
|
|
308
|
+
|
|
309
|
+
```python
|
|
310
|
+
import kcl_lib.api as api
|
|
311
|
+
|
|
312
|
+
source_code = \"\"\"schema Person:
|
|
313
|
+
name: str
|
|
314
|
+
age: int
|
|
315
|
+
|
|
316
|
+
check:
|
|
317
|
+
0 < age < 120
|
|
318
|
+
\"\"\"
|
|
319
|
+
args = api.FormatCode_Args(source=source_code)
|
|
320
|
+
api_instance = api.API()
|
|
321
|
+
result = api_instance.format_code(args)
|
|
322
|
+
assert (
|
|
323
|
+
result.formatted.decode()
|
|
324
|
+
== \"\"\"schema Person:
|
|
325
|
+
name: str
|
|
326
|
+
age: int
|
|
327
|
+
|
|
328
|
+
check:
|
|
329
|
+
0 < age < 120
|
|
330
|
+
|
|
331
|
+
\"\"\"
|
|
332
|
+
)
|
|
333
|
+
```
|
|
334
|
+
"""
|
|
56
335
|
return self.call("KclvmService.FormatCode", args)
|
|
57
336
|
|
|
58
337
|
def format_path(self, args: FormatPath_Args) -> FormatPath_Result:
|
|
338
|
+
"""Format KCL file or directory path contains KCL files and returns the changed file paths.
|
|
339
|
+
|
|
340
|
+
## Example
|
|
341
|
+
|
|
342
|
+
The content of `format_path.k` is
|
|
343
|
+
|
|
344
|
+
```python
|
|
345
|
+
schema Person:
|
|
346
|
+
name: str
|
|
347
|
+
age: int
|
|
348
|
+
|
|
349
|
+
check:
|
|
350
|
+
0 < age < 120
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
Python Code
|
|
354
|
+
|
|
355
|
+
```python
|
|
356
|
+
import kcl_lib.api as api
|
|
357
|
+
|
|
358
|
+
args = api.FormatPath_Args(path="format_path.k")
|
|
359
|
+
api_instance = api.API()
|
|
360
|
+
result = api_instance.format_path(args)
|
|
361
|
+
print(result)
|
|
362
|
+
```
|
|
363
|
+
"""
|
|
59
364
|
return self.call("KclvmService.FormatPath", args)
|
|
60
365
|
|
|
61
366
|
def lint_path(self, args: LintPath_Args) -> LintPath_Result:
|
|
367
|
+
"""Lint files and return error messages including errors and warnings.
|
|
368
|
+
|
|
369
|
+
## Example
|
|
370
|
+
|
|
371
|
+
The content of `lint_path.k` is
|
|
372
|
+
|
|
373
|
+
```python
|
|
374
|
+
import math
|
|
375
|
+
|
|
376
|
+
a = 1
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
Python Code
|
|
380
|
+
|
|
381
|
+
```python
|
|
382
|
+
import kcl_lib.api as api
|
|
383
|
+
|
|
384
|
+
args = api.LintPath_Args(paths=["lint_path.k"])
|
|
385
|
+
api_instance = api.API()
|
|
386
|
+
result = api_instance.lint_path(args)
|
|
387
|
+
```
|
|
388
|
+
"""
|
|
62
389
|
return self.call("KclvmService.LintPath", args)
|
|
63
390
|
|
|
64
391
|
def override_file(self, args: OverrideFile_Args) -> OverrideFile_Result:
|
|
392
|
+
"""Override KCL file with arguments. See [https://www.kcl-lang.io/docs/user_docs/guides/automation](https://www.kcl-lang.io/docs/user_docs/guides/automation) for more override spec guide.
|
|
393
|
+
|
|
394
|
+
## Example
|
|
395
|
+
|
|
396
|
+
The content of `main.k` is
|
|
397
|
+
|
|
398
|
+
```python
|
|
399
|
+
a = 1
|
|
400
|
+
|
|
401
|
+
b = {
|
|
402
|
+
"a": 1
|
|
403
|
+
"b": 2
|
|
404
|
+
}
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
Python Code
|
|
408
|
+
|
|
409
|
+
```python
|
|
410
|
+
import kcl_lib.api as api
|
|
411
|
+
import pathlib
|
|
412
|
+
|
|
413
|
+
test_file = "main.k"
|
|
414
|
+
args = api.OverrideFile_Args(
|
|
415
|
+
file=test_file,
|
|
416
|
+
specs=["b.a=2"],
|
|
417
|
+
)
|
|
418
|
+
api = api.API()
|
|
419
|
+
result = api.override_file(args)
|
|
420
|
+
assert len(result.parse_errors) == 0
|
|
421
|
+
assert result.result == True
|
|
422
|
+
assert pathlib.Path(test_file).read_text() == \"\"\"a = 1
|
|
423
|
+
b = {
|
|
424
|
+
"a": 2
|
|
425
|
+
"b": 2
|
|
426
|
+
}
|
|
427
|
+
\"\"\"
|
|
428
|
+
```
|
|
429
|
+
"""
|
|
65
430
|
return self.call("KclvmService.OverrideFile", args)
|
|
66
431
|
|
|
67
432
|
def get_schema_type_mapping(
|
|
68
433
|
self,
|
|
69
434
|
args: GetSchemaTypeMapping_Args,
|
|
70
435
|
) -> GetSchemaTypeMapping_Result:
|
|
436
|
+
"""Get schema type mapping defined in the program.
|
|
437
|
+
|
|
438
|
+
## Example
|
|
439
|
+
|
|
440
|
+
The content of `schema.k` is
|
|
441
|
+
|
|
442
|
+
```python
|
|
443
|
+
schema AppConfig:
|
|
444
|
+
replicas: int
|
|
445
|
+
|
|
446
|
+
app: AppConfig {
|
|
447
|
+
replicas: 2
|
|
448
|
+
}
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
Python Code
|
|
452
|
+
|
|
453
|
+
```python
|
|
454
|
+
import kcl_lib.api as api
|
|
455
|
+
|
|
456
|
+
exec_args = api.ExecProgram_Args(k_filename_list=["schema.k"])
|
|
457
|
+
args = api.GetSchemaTypeMapping_Args(exec_args=exec_args)
|
|
458
|
+
api = api.API()
|
|
459
|
+
result = api.get_schema_type_mapping(args)
|
|
460
|
+
assert result.schema_type_mapping["app"].properties["replicas"].type == "int"
|
|
461
|
+
```
|
|
462
|
+
"""
|
|
71
463
|
return self.call("KclvmService.GetSchemaTypeMapping", args)
|
|
72
464
|
|
|
73
465
|
def validate_code(self, args: ValidateCode_Args) -> ValidateCode_Result:
|
|
466
|
+
"""Validate code using schema and JSON/YAML data strings.
|
|
467
|
+
|
|
468
|
+
## Example
|
|
469
|
+
Python Code
|
|
470
|
+
|
|
471
|
+
```python
|
|
472
|
+
import kcl_lib.api as api
|
|
473
|
+
|
|
474
|
+
code = \"\"\"
|
|
475
|
+
schema Person:
|
|
476
|
+
name: str
|
|
477
|
+
age: int
|
|
478
|
+
|
|
479
|
+
check:
|
|
480
|
+
0 < age < 120
|
|
481
|
+
\"\"\"
|
|
482
|
+
data = '{"name": "Alice", "age": 10}'
|
|
483
|
+
args = api.ValidateCode_Args(code=code, data=data, format="json")
|
|
484
|
+
api_instance = api.API()
|
|
485
|
+
result = api_instance.validate_code(args)
|
|
486
|
+
assert result.success == True
|
|
487
|
+
assert result.err_message == ""
|
|
488
|
+
```
|
|
489
|
+
"""
|
|
74
490
|
return self.call("KclvmService.ValidateCode", args)
|
|
75
491
|
|
|
76
492
|
def load_settings_files(
|
|
77
493
|
self,
|
|
78
494
|
args: LoadSettingsFiles_Args,
|
|
79
495
|
) -> LoadSettingsFiles_Result:
|
|
496
|
+
"""Load the setting file config defined in `kcl.yaml`
|
|
497
|
+
|
|
498
|
+
## Example
|
|
499
|
+
|
|
500
|
+
The content of `kcl.yaml` is
|
|
501
|
+
|
|
502
|
+
```yaml
|
|
503
|
+
kcl_cli_configs:
|
|
504
|
+
strict_range_check: true
|
|
505
|
+
kcl_options:
|
|
506
|
+
- key: key
|
|
507
|
+
value: value
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
Python Code
|
|
511
|
+
|
|
512
|
+
```python
|
|
513
|
+
import kcl_lib.api as api
|
|
514
|
+
|
|
515
|
+
args = api.LoadSettingsFiles_Args(
|
|
516
|
+
work_dir=".", files=["kcl.yaml"]
|
|
517
|
+
)
|
|
518
|
+
api_instance = api.API()
|
|
519
|
+
result = api_instance.load_settings_files(args)
|
|
520
|
+
assert result.kcl_cli_configs.files == []
|
|
521
|
+
assert result.kcl_cli_configs.strict_range_check == True
|
|
522
|
+
assert (
|
|
523
|
+
result.kcl_options[0].key == "key" and result.kcl_options[0].value == '"value"'
|
|
524
|
+
)
|
|
525
|
+
```
|
|
526
|
+
"""
|
|
80
527
|
return self.call("KclvmService.LoadSettingsFiles", args)
|
|
81
528
|
|
|
82
529
|
def rename(self, args: Rename_Args) -> Rename_Result:
|
|
530
|
+
"""Rename all the occurrences of the target symbol in the files. This API will rewrite files if they contain symbols to be renamed. Return the file paths that got changed.
|
|
531
|
+
|
|
532
|
+
## Example
|
|
533
|
+
|
|
534
|
+
The content of `main.k` is
|
|
535
|
+
|
|
536
|
+
```python
|
|
537
|
+
a = 1
|
|
538
|
+
b = a
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
Python Code
|
|
542
|
+
|
|
543
|
+
```python
|
|
544
|
+
import kcl_lib.api as api
|
|
545
|
+
|
|
546
|
+
args = api.Rename_Args(
|
|
547
|
+
package_root=".",
|
|
548
|
+
symbol_path="a",
|
|
549
|
+
file_paths=["main.k"],
|
|
550
|
+
new_name="a2",
|
|
551
|
+
)
|
|
552
|
+
api_instance = api.API()
|
|
553
|
+
result = api_instance.rename(args)
|
|
554
|
+
```
|
|
555
|
+
"""
|
|
83
556
|
return self.call("KclvmService.Rename", args)
|
|
84
557
|
|
|
85
558
|
def rename_code(self, args: RenameCode_Args) -> RenameCode_Result:
|
|
559
|
+
"""Rename all the occurrences of the target symbol and return the modified code if any code has been changed. This API won't rewrite files but return the changed code.
|
|
560
|
+
|
|
561
|
+
## Example
|
|
562
|
+
|
|
563
|
+
Python Code
|
|
564
|
+
|
|
565
|
+
```python
|
|
566
|
+
import kcl_lib.api as api
|
|
567
|
+
|
|
568
|
+
args = api.RenameCode_Args(
|
|
569
|
+
package_root="/mock/path",
|
|
570
|
+
symbol_path="a",
|
|
571
|
+
source_codes={"/mock/path/main.k": "a = 1"},
|
|
572
|
+
new_name="a2",
|
|
573
|
+
)
|
|
574
|
+
api_instance = api.API()
|
|
575
|
+
result = api_instance.rename_code(args)
|
|
576
|
+
assert result.changed_codes["/mock/path/main.k"] == "a2 = 1"
|
|
577
|
+
```
|
|
578
|
+
"""
|
|
86
579
|
return self.call("KclvmService.RenameCode", args)
|
|
87
580
|
|
|
88
581
|
def test(self, args: Test_Args) -> Test_Result:
|
|
582
|
+
"""Test KCL packages with test arguments.
|
|
583
|
+
|
|
584
|
+
## Example
|
|
585
|
+
|
|
586
|
+
Python Code
|
|
587
|
+
|
|
588
|
+
```python
|
|
589
|
+
import kcl_lib.api as api
|
|
590
|
+
args = api.Test_Args(
|
|
591
|
+
pkg_list=["path/to/testing/pkg/..."],
|
|
592
|
+
)
|
|
593
|
+
api_instance = api.API()
|
|
594
|
+
result = api_instance.test(args)
|
|
595
|
+
```
|
|
596
|
+
"""
|
|
89
597
|
return self.call("KclvmService.Test", args)
|
|
90
598
|
|
|
91
|
-
def
|
|
599
|
+
def update_dependencies(
|
|
600
|
+
self, args: UpdateDependencies_Args
|
|
601
|
+
) -> UpdateDependencies_Result:
|
|
602
|
+
"""Download and update dependencies defined in the `kcl.mod` file and return the external package name and location list.
|
|
603
|
+
|
|
604
|
+
## Examples
|
|
605
|
+
|
|
606
|
+
The content of `module/kcl.mod` is
|
|
607
|
+
|
|
608
|
+
```yaml
|
|
609
|
+
[package]
|
|
610
|
+
name = "mod_update"
|
|
611
|
+
edition = "0.0.1"
|
|
612
|
+
version = "0.0.1"
|
|
613
|
+
|
|
614
|
+
[dependencies]
|
|
615
|
+
helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.0" }
|
|
616
|
+
flask = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests", commit = "ade147b" }
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
Python Code
|
|
620
|
+
|
|
621
|
+
```python
|
|
622
|
+
import kcl_lib.api as api
|
|
623
|
+
|
|
624
|
+
args = api.UpdateDependencies_Args(
|
|
625
|
+
manifest_path="module"
|
|
626
|
+
)
|
|
627
|
+
api_instance = api.API()
|
|
628
|
+
result = api_instance.update_dependencies(args)
|
|
629
|
+
pkg_names = [pkg.pkg_name for pkg in result.external_pkgs]
|
|
630
|
+
assert len(pkg_names) == 2
|
|
631
|
+
assert "helloworld" in pkg_names
|
|
632
|
+
assert "flask" in pkg_names
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
Call `exec_program` with external dependencies
|
|
636
|
+
|
|
637
|
+
The content of `module/kcl.mod` is
|
|
638
|
+
|
|
639
|
+
```yaml
|
|
640
|
+
[package]
|
|
641
|
+
name = "mod_update"
|
|
642
|
+
edition = "0.0.1"
|
|
643
|
+
version = "0.0.1"
|
|
644
|
+
|
|
645
|
+
[dependencies]
|
|
646
|
+
helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.0" }
|
|
647
|
+
flask = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests", commit = "ade147b" }
|
|
648
|
+
```
|
|
649
|
+
|
|
650
|
+
The content of `module/main.k` is
|
|
651
|
+
|
|
652
|
+
```python
|
|
653
|
+
import helloworld
|
|
654
|
+
import flask
|
|
655
|
+
|
|
656
|
+
a = helloworld.The_first_kcl_program
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
Python Code
|
|
660
|
+
|
|
661
|
+
```python
|
|
662
|
+
import kcl_lib.api as api
|
|
663
|
+
|
|
664
|
+
args = api.UpdateDependencies_Args(
|
|
665
|
+
manifest_path="module"
|
|
666
|
+
)
|
|
667
|
+
api_instance = api.API()
|
|
668
|
+
result = api_instance.update_dependencies(args)
|
|
669
|
+
exec_args = api.ExecProgram_Args(
|
|
670
|
+
k_filename_list=["module/main.k"],
|
|
671
|
+
external_pkgs=result.external_pkgs,
|
|
672
|
+
)
|
|
673
|
+
result = api_instance.exec_program(exec_args)
|
|
674
|
+
assert result.yaml_result == "a: Hello World!"
|
|
675
|
+
```
|
|
676
|
+
"""
|
|
92
677
|
return self.call("KclvmService.UpdateDependencies", args)
|
|
93
678
|
|
|
679
|
+
def get_version(self) -> GetVersion_Result:
|
|
680
|
+
"""Return the KCL service version information.
|
|
681
|
+
|
|
682
|
+
## Example
|
|
683
|
+
|
|
684
|
+
Python Code
|
|
685
|
+
|
|
686
|
+
```python
|
|
687
|
+
import kcl_lib.api as api
|
|
688
|
+
|
|
689
|
+
api_instance = api.API()
|
|
690
|
+
result = api_instance.get_version()
|
|
691
|
+
print(result.version_info)
|
|
692
|
+
```
|
|
693
|
+
"""
|
|
694
|
+
return self.call("KclvmService.GetVersion", GetVersion_Args())
|
|
695
|
+
|
|
94
696
|
# Helper method to perform the call
|
|
95
697
|
def call(self, name: str, args):
|
|
698
|
+
"""Call KCL API with the API name and argument protobuf bytes."""
|
|
96
699
|
# Serialize arguments using pickle or json
|
|
97
700
|
args_serialized = args.SerializeToString()
|
|
98
701
|
|
|
99
702
|
# Call the service function and get the result
|
|
100
|
-
result = bytes(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
703
|
+
result = bytes(
|
|
704
|
+
kcl_lib.call_with_plugin_agent(
|
|
705
|
+
name.encode("utf-8"), args_serialized, self.plugin_agent
|
|
706
|
+
)
|
|
707
|
+
)
|
|
708
|
+
if result.startswith(b"ERROR:"):
|
|
709
|
+
raise Exception(result.decode(encoding="utf-8").removeprefix("ERROR:"))
|
|
105
710
|
msg = self.create_method_resp_message(name)
|
|
106
711
|
msg.ParseFromString(result)
|
|
107
712
|
return msg
|
|
@@ -109,89 +714,93 @@ class API:
|
|
|
109
714
|
def create_method_req_message(self, method: str) -> _message.Message:
|
|
110
715
|
if method in ["Ping", "KclvmService.Ping"]:
|
|
111
716
|
return Ping_Args()
|
|
112
|
-
|
|
717
|
+
elif method in ["ExecProgram", "KclvmService.ExecProgram"]:
|
|
113
718
|
return ExecProgram_Args()
|
|
114
|
-
|
|
719
|
+
elif method in ["BuildProgram", "KclvmService.BuildProgram"]:
|
|
115
720
|
return BuildProgram_Args()
|
|
116
|
-
|
|
721
|
+
elif method in ["ExecArtifact", "KclvmService.ExecArtifact"]:
|
|
117
722
|
return ExecArtifact_Args()
|
|
118
|
-
|
|
723
|
+
elif method in ["ParseFile", "KclvmService.ParseFile"]:
|
|
119
724
|
return ParseFile_Args()
|
|
120
|
-
|
|
725
|
+
elif method in ["ParseProgram", "KclvmService.ParseProgram"]:
|
|
121
726
|
return ParseProgram_Args()
|
|
122
|
-
|
|
727
|
+
elif method in ["LoadPackage", "KclvmService.LoadPackage"]:
|
|
123
728
|
return LoadPackage_Args()
|
|
124
|
-
|
|
729
|
+
elif method in ["ListOptions", "KclvmService.ListOptions"]:
|
|
125
730
|
return ParseProgram_Args()
|
|
126
|
-
|
|
731
|
+
elif method in ["ListVariables", "KclvmService.ListVariables"]:
|
|
127
732
|
return ListVariables_Args()
|
|
128
|
-
|
|
733
|
+
elif method in ["FormatCode", "KclvmService.FormatCode"]:
|
|
129
734
|
return FormatCode_Args()
|
|
130
|
-
|
|
735
|
+
elif method in ["FormatPath", "KclvmService.FormatPath"]:
|
|
131
736
|
return FormatPath_Args()
|
|
132
|
-
|
|
737
|
+
elif method in ["LintPath", "KclvmService.LintPath"]:
|
|
133
738
|
return LintPath_Args()
|
|
134
|
-
|
|
739
|
+
elif method in ["OverrideFile", "KclvmService.OverrideFile"]:
|
|
135
740
|
return OverrideFile_Args()
|
|
136
|
-
|
|
741
|
+
elif method in ["GetSchemaTypeMapping", "KclvmService.GetSchemaTypeMapping"]:
|
|
137
742
|
return GetSchemaTypeMapping_Args()
|
|
138
|
-
|
|
743
|
+
elif method in ["ValidateCode", "KclvmService.ValidateCode"]:
|
|
139
744
|
return ValidateCode_Args()
|
|
140
|
-
|
|
745
|
+
elif method in ["ListDepFiles", "KclvmService.ListDepFiles"]:
|
|
141
746
|
return ListDepFiles_Args()
|
|
142
|
-
|
|
747
|
+
elif method in ["LoadSettingsFiles", "KclvmService.LoadSettingsFiles"]:
|
|
143
748
|
return LoadSettingsFiles_Args()
|
|
144
|
-
|
|
749
|
+
elif method in ["Rename", "KclvmService.Rename"]:
|
|
145
750
|
return Rename_Args()
|
|
146
|
-
|
|
751
|
+
elif method in ["RenameCode", "KclvmService.RenameCode"]:
|
|
147
752
|
return RenameCode_Args()
|
|
148
|
-
|
|
753
|
+
elif method in ["Test", "KclvmService.Test"]:
|
|
149
754
|
return Test_Args()
|
|
150
|
-
|
|
755
|
+
elif method in ["UpdateDependencies", "KclvmService.UpdateDependencies"]:
|
|
151
756
|
return UpdateDependencies_Args()
|
|
757
|
+
elif method in ["GetVersion", "KclvmService.GetVersion"]:
|
|
758
|
+
return GetVersion_Args()
|
|
152
759
|
raise Exception(f"unknown method: {method}")
|
|
153
760
|
|
|
154
761
|
def create_method_resp_message(self, method: str) -> _message.Message:
|
|
155
762
|
if method in ["Ping", "KclvmService.Ping"]:
|
|
156
763
|
return Ping_Result()
|
|
157
|
-
|
|
764
|
+
elif method in ["ExecProgram", "KclvmService.ExecProgram"]:
|
|
158
765
|
return ExecProgram_Result()
|
|
159
|
-
|
|
766
|
+
elif method in ["BuildProgram", "KclvmService.BuildProgram"]:
|
|
160
767
|
return BuildProgram_Result()
|
|
161
|
-
|
|
768
|
+
elif method in ["ExecArtifact", "KclvmService.ExecArtifact"]:
|
|
162
769
|
return ExecProgram_Result()
|
|
163
|
-
|
|
770
|
+
elif method in ["ParseFile", "KclvmService.ParseFile"]:
|
|
164
771
|
return ParseFile_Result()
|
|
165
|
-
|
|
772
|
+
elif method in ["ParseProgram", "KclvmService.ParseProgram"]:
|
|
166
773
|
return ParseProgram_Result()
|
|
167
|
-
|
|
774
|
+
elif method in ["LoadPackage", "KclvmService.LoadPackage"]:
|
|
168
775
|
return LoadPackage_Result()
|
|
169
|
-
|
|
776
|
+
elif method in ["ListOptions", "KclvmService.ListOptions"]:
|
|
170
777
|
return ListOptions_Result()
|
|
171
|
-
|
|
778
|
+
elif method in ["ListVariables", "KclvmService.ListVariables"]:
|
|
172
779
|
return ListVariables_Result()
|
|
173
|
-
|
|
780
|
+
elif method in ["FormatCode", "KclvmService.FormatCode"]:
|
|
174
781
|
return FormatCode_Result()
|
|
175
|
-
|
|
782
|
+
elif method in ["FormatPath", "KclvmService.FormatPath"]:
|
|
176
783
|
return FormatPath_Result()
|
|
177
|
-
|
|
784
|
+
elif method in ["LintPath", "KclvmService.LintPath"]:
|
|
178
785
|
return LintPath_Result()
|
|
179
|
-
|
|
786
|
+
elif method in ["OverrideFile", "KclvmService.OverrideFile"]:
|
|
180
787
|
return OverrideFile_Result()
|
|
181
|
-
|
|
788
|
+
elif method in ["GetSchemaTypeMapping", "KclvmService.GetSchemaTypeMapping"]:
|
|
182
789
|
return GetSchemaTypeMapping_Result()
|
|
183
|
-
|
|
790
|
+
elif method in ["ValidateCode", "KclvmService.ValidateCode"]:
|
|
184
791
|
return ValidateCode_Result()
|
|
185
|
-
|
|
792
|
+
elif method in ["ListDepFiles", "KclvmService.ListDepFiles"]:
|
|
186
793
|
return ListDepFiles_Result()
|
|
187
|
-
|
|
794
|
+
elif method in ["LoadSettingsFiles", "KclvmService.LoadSettingsFiles"]:
|
|
188
795
|
return LoadSettingsFiles_Result()
|
|
189
|
-
|
|
796
|
+
elif method in ["Rename", "KclvmService.Rename"]:
|
|
190
797
|
return Rename_Result()
|
|
191
|
-
|
|
798
|
+
elif method in ["RenameCode", "KclvmService.RenameCode"]:
|
|
192
799
|
return RenameCode_Result()
|
|
193
|
-
|
|
800
|
+
elif method in ["Test", "KclvmService.Test"]:
|
|
194
801
|
return Test_Result()
|
|
195
|
-
|
|
802
|
+
elif method in ["UpdateDependencies", "KclvmService.UpdateDependencies"]:
|
|
196
803
|
return UpdateDependencies_Result()
|
|
804
|
+
elif method in ["GetVersion", "KclvmService.GetVersion"]:
|
|
805
|
+
return GetVersion_Result()
|
|
197
806
|
raise Exception(f"unknown method: {method}")
|