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