kcl-lib 0.12.2__cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.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.
- kcl_lib/__init__.py +1 -0
- kcl_lib/_kcl_lib.cpython-38-aarch64-linux-gnu.so +0 -0
- kcl_lib/api/__init__.py +137 -0
- kcl_lib/api/service.py +764 -0
- kcl_lib/api/spec_pb2.py +247 -0
- kcl_lib/api/spec_pb2.pyi +1169 -0
- kcl_lib/plugin/__init__.py +3 -0
- kcl_lib/plugin/plugin.py +82 -0
- kcl_lib-0.12.2.dist-info/METADATA +19 -0
- kcl_lib-0.12.2.dist-info/RECORD +11 -0
- kcl_lib-0.12.2.dist-info/WHEEL +5 -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
|
+
PingArgs,
|
|
5
|
+
PingResult,
|
|
6
|
+
GetVersionArgs,
|
|
7
|
+
GetVersionResult,
|
|
8
|
+
ParseFileArgs,
|
|
9
|
+
ParseFileResult,
|
|
10
|
+
ParseProgramArgs,
|
|
11
|
+
ParseProgramResult,
|
|
12
|
+
LoadPackageArgs,
|
|
13
|
+
LoadPackageResult,
|
|
14
|
+
ListOptionsResult,
|
|
15
|
+
ExecProgramArgs,
|
|
16
|
+
ExecProgramResult,
|
|
17
|
+
BuildProgramArgs,
|
|
18
|
+
BuildProgramResult,
|
|
19
|
+
ExecArtifactArgs,
|
|
20
|
+
FormatCodeArgs,
|
|
21
|
+
FormatCodeResult,
|
|
22
|
+
FormatPathArgs,
|
|
23
|
+
FormatPathResult,
|
|
24
|
+
LintPathArgs,
|
|
25
|
+
LintPathResult,
|
|
26
|
+
OverrideFileArgs,
|
|
27
|
+
OverrideFileResult,
|
|
28
|
+
ListVariablesArgs,
|
|
29
|
+
ListVariablesResult,
|
|
30
|
+
GetSchemaTypeMappingArgs,
|
|
31
|
+
GetSchemaTypeMappingResult,
|
|
32
|
+
ValidateCodeArgs,
|
|
33
|
+
ValidateCodeResult,
|
|
34
|
+
ListDepFilesArgs,
|
|
35
|
+
ListDepFilesResult,
|
|
36
|
+
LoadSettingsFilesArgs,
|
|
37
|
+
LoadSettingsFilesResult,
|
|
38
|
+
RenameArgs,
|
|
39
|
+
RenameResult,
|
|
40
|
+
RenameCodeArgs,
|
|
41
|
+
RenameCodeResult,
|
|
42
|
+
TestArgs,
|
|
43
|
+
TestResult,
|
|
44
|
+
UpdateDependenciesArgs,
|
|
45
|
+
UpdateDependenciesResult,
|
|
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.ExecProgramArgs(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: PingArgs) -> PingResult:
|
|
71
|
+
return self.call("KclService.Ping", args)
|
|
72
|
+
|
|
73
|
+
def parse_program(self, args: ParseProgramArgs) -> ParseProgramResult:
|
|
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.ParseProgramArgs(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("KclService.ParseProgram", args)
|
|
101
|
+
|
|
102
|
+
def exec_program(self, args: ExecProgramArgs) -> ExecProgramResult:
|
|
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.ExecProgramArgs(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.ExecProgramArgs(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("KclService.ExecProgram", args)
|
|
141
|
+
|
|
142
|
+
def parse_file(self, args: ParseFileArgs) -> ParseFileResult:
|
|
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.ParseProgramArgs(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("KclService.ParseFile", args)
|
|
170
|
+
|
|
171
|
+
def parse_program(self, args: ParseProgramArgs) -> ParseProgramResult:
|
|
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.ParseProgramArgs(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("KclService.ParseProgram", args)
|
|
199
|
+
|
|
200
|
+
def load_package(self, args: LoadPackageArgs) -> LoadPackageResult:
|
|
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.LoadPackageArgs(
|
|
220
|
+
parse_args=api.ParseProgramArgs(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("KclService.LoadPackage", args)
|
|
228
|
+
|
|
229
|
+
def list_options(self, args: ParseProgramArgs) -> ListOptionsResult:
|
|
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.ParseProgramArgs(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("KclService.ListOptions", args)
|
|
259
|
+
|
|
260
|
+
def list_variables(self, args: ListVariablesArgs) -> ListVariablesResult:
|
|
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.ListVariablesArgs(files=[TEST_FILE])
|
|
281
|
+
api = api.API()
|
|
282
|
+
result = api.list_variables(args)
|
|
283
|
+
```
|
|
284
|
+
"""
|
|
285
|
+
return self.call("KclService.ListVariables", args)
|
|
286
|
+
|
|
287
|
+
def format_code(self, args: FormatCodeArgs) -> FormatCodeResult:
|
|
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.FormatCodeArgs(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("KclService.FormatCode", args)
|
|
305
|
+
|
|
306
|
+
def format_path(self, args: FormatPathArgs) -> FormatPathResult:
|
|
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.FormatPathArgs(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("KclService.FormatPath", args)
|
|
333
|
+
|
|
334
|
+
def lint_path(self, args: LintPathArgs) -> LintPathResult:
|
|
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.LintPathArgs(paths=["lint_path.k"])
|
|
352
|
+
api_instance = api.API()
|
|
353
|
+
result = api_instance.lint_path(args)
|
|
354
|
+
```
|
|
355
|
+
"""
|
|
356
|
+
return self.call("KclService.LintPath", args)
|
|
357
|
+
|
|
358
|
+
def override_file(self, args: OverrideFileArgs) -> OverrideFileResult:
|
|
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.OverrideFileArgs(
|
|
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("KclService.OverrideFile", args)
|
|
397
|
+
|
|
398
|
+
def get_schema_type_mapping(
|
|
399
|
+
self,
|
|
400
|
+
args: GetSchemaTypeMappingArgs,
|
|
401
|
+
) -> GetSchemaTypeMappingResult:
|
|
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.ExecProgramArgs(k_filename_list=["schema.k"])
|
|
422
|
+
args = api.GetSchemaTypeMappingArgs(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("KclService.GetSchemaTypeMapping", args)
|
|
429
|
+
|
|
430
|
+
def validate_code(self, args: ValidateCodeArgs) -> ValidateCodeResult:
|
|
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.ValidateCodeArgs(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("KclService.ValidateCode", args)
|
|
455
|
+
|
|
456
|
+
def load_settings_files(
|
|
457
|
+
self,
|
|
458
|
+
args: LoadSettingsFilesArgs,
|
|
459
|
+
) -> LoadSettingsFilesResult:
|
|
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.LoadSettingsFilesArgs(
|
|
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("KclService.LoadSettingsFiles", args)
|
|
491
|
+
|
|
492
|
+
def rename(self, args: RenameArgs) -> RenameResult:
|
|
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.RenameArgs(
|
|
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("KclService.Rename", args)
|
|
520
|
+
|
|
521
|
+
def rename_code(self, args: RenameCodeArgs) -> RenameCodeResult:
|
|
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.RenameCodeArgs(
|
|
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("KclService.RenameCode", args)
|
|
542
|
+
|
|
543
|
+
def test(self, args: TestArgs) -> TestResult:
|
|
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.TestArgs(
|
|
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("KclService.Test", args)
|
|
560
|
+
|
|
561
|
+
def update_dependencies(
|
|
562
|
+
self, args: UpdateDependenciesArgs
|
|
563
|
+
) -> UpdateDependenciesResult:
|
|
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.UpdateDependenciesArgs(
|
|
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.UpdateDependenciesArgs(
|
|
624
|
+
manifest_path="module"
|
|
625
|
+
)
|
|
626
|
+
api_instance = api.API()
|
|
627
|
+
result = api_instance.update_dependencies(args)
|
|
628
|
+
exec_args = api.ExecProgramArgs(
|
|
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("KclService.UpdateDependencies", args)
|
|
637
|
+
|
|
638
|
+
def get_version(self) -> GetVersionResult:
|
|
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("KclService.GetVersion", GetVersionArgs())
|
|
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", "KclService.Ping"]:
|
|
674
|
+
return PingArgs()
|
|
675
|
+
elif method in ["ExecProgram", "KclService.ExecProgram"]:
|
|
676
|
+
return ExecProgramArgs()
|
|
677
|
+
elif method in ["BuildProgram", "KclService.BuildProgram"]:
|
|
678
|
+
return BuildProgramArgs()
|
|
679
|
+
elif method in ["ExecArtifact", "KclService.ExecArtifact"]:
|
|
680
|
+
return ExecArtifactArgs()
|
|
681
|
+
elif method in ["ParseFile", "KclService.ParseFile"]:
|
|
682
|
+
return ParseFileArgs()
|
|
683
|
+
elif method in ["ParseProgram", "KclService.ParseProgram"]:
|
|
684
|
+
return ParseProgramArgs()
|
|
685
|
+
elif method in ["LoadPackage", "KclService.LoadPackage"]:
|
|
686
|
+
return LoadPackageArgs()
|
|
687
|
+
elif method in ["ListOptions", "KclService.ListOptions"]:
|
|
688
|
+
return ParseProgramArgs()
|
|
689
|
+
elif method in ["ListVariables", "KclService.ListVariables"]:
|
|
690
|
+
return ListVariablesArgs()
|
|
691
|
+
elif method in ["FormatCode", "KclService.FormatCode"]:
|
|
692
|
+
return FormatCodeArgs()
|
|
693
|
+
elif method in ["FormatPath", "KclService.FormatPath"]:
|
|
694
|
+
return FormatPathArgs()
|
|
695
|
+
elif method in ["LintPath", "KclService.LintPath"]:
|
|
696
|
+
return LintPathArgs()
|
|
697
|
+
elif method in ["OverrideFile", "KclService.OverrideFile"]:
|
|
698
|
+
return OverrideFileArgs()
|
|
699
|
+
elif method in ["GetSchemaTypeMapping", "KclService.GetSchemaTypeMapping"]:
|
|
700
|
+
return GetSchemaTypeMappingArgs()
|
|
701
|
+
elif method in ["ValidateCode", "KclService.ValidateCode"]:
|
|
702
|
+
return ValidateCodeArgs()
|
|
703
|
+
elif method in ["ListDepFiles", "KclService.ListDepFiles"]:
|
|
704
|
+
return ListDepFilesArgs()
|
|
705
|
+
elif method in ["LoadSettingsFiles", "KclService.LoadSettingsFiles"]:
|
|
706
|
+
return LoadSettingsFilesArgs()
|
|
707
|
+
elif method in ["Rename", "KclService.Rename"]:
|
|
708
|
+
return RenameArgs()
|
|
709
|
+
elif method in ["RenameCode", "KclService.RenameCode"]:
|
|
710
|
+
return RenameCodeArgs()
|
|
711
|
+
elif method in ["Test", "KclService.Test"]:
|
|
712
|
+
return TestArgs()
|
|
713
|
+
elif method in ["UpdateDependencies", "KclService.UpdateDependencies"]:
|
|
714
|
+
return UpdateDependenciesArgs()
|
|
715
|
+
elif method in ["GetVersion", "KclService.GetVersion"]:
|
|
716
|
+
return GetVersionArgs()
|
|
717
|
+
raise Exception(f"unknown method: {method}")
|
|
718
|
+
|
|
719
|
+
def create_method_resp_message(self, method: str) -> _message.Message:
|
|
720
|
+
if method in ["Ping", "KclService.Ping"]:
|
|
721
|
+
return PingResult()
|
|
722
|
+
elif method in ["ExecProgram", "KclService.ExecProgram"]:
|
|
723
|
+
return ExecProgramResult()
|
|
724
|
+
elif method in ["BuildProgram", "KclService.BuildProgram"]:
|
|
725
|
+
return BuildProgramResult()
|
|
726
|
+
elif method in ["ExecArtifact", "KclService.ExecArtifact"]:
|
|
727
|
+
return ExecProgramResult()
|
|
728
|
+
elif method in ["ParseFile", "KclService.ParseFile"]:
|
|
729
|
+
return ParseFileResult()
|
|
730
|
+
elif method in ["ParseProgram", "KclService.ParseProgram"]:
|
|
731
|
+
return ParseProgramResult()
|
|
732
|
+
elif method in ["LoadPackage", "KclService.LoadPackage"]:
|
|
733
|
+
return LoadPackageResult()
|
|
734
|
+
elif method in ["ListOptions", "KclService.ListOptions"]:
|
|
735
|
+
return ListOptionsResult()
|
|
736
|
+
elif method in ["ListVariables", "KclService.ListVariables"]:
|
|
737
|
+
return ListVariablesResult()
|
|
738
|
+
elif method in ["FormatCode", "KclService.FormatCode"]:
|
|
739
|
+
return FormatCodeResult()
|
|
740
|
+
elif method in ["FormatPath", "KclService.FormatPath"]:
|
|
741
|
+
return FormatPathResult()
|
|
742
|
+
elif method in ["LintPath", "KclService.LintPath"]:
|
|
743
|
+
return LintPathResult()
|
|
744
|
+
elif method in ["OverrideFile", "KclService.OverrideFile"]:
|
|
745
|
+
return OverrideFileResult()
|
|
746
|
+
elif method in ["GetSchemaTypeMapping", "KclService.GetSchemaTypeMapping"]:
|
|
747
|
+
return GetSchemaTypeMappingResult()
|
|
748
|
+
elif method in ["ValidateCode", "KclService.ValidateCode"]:
|
|
749
|
+
return ValidateCodeResult()
|
|
750
|
+
elif method in ["ListDepFiles", "KclService.ListDepFiles"]:
|
|
751
|
+
return ListDepFilesResult()
|
|
752
|
+
elif method in ["LoadSettingsFiles", "KclService.LoadSettingsFiles"]:
|
|
753
|
+
return LoadSettingsFilesResult()
|
|
754
|
+
elif method in ["Rename", "KclService.Rename"]:
|
|
755
|
+
return RenameResult()
|
|
756
|
+
elif method in ["RenameCode", "KclService.RenameCode"]:
|
|
757
|
+
return RenameCodeResult()
|
|
758
|
+
elif method in ["Test", "KclService.Test"]:
|
|
759
|
+
return TestResult()
|
|
760
|
+
elif method in ["UpdateDependencies", "KclService.UpdateDependencies"]:
|
|
761
|
+
return UpdateDependenciesResult()
|
|
762
|
+
elif method in ["GetVersion", "KclService.GetVersion"]:
|
|
763
|
+
return GetVersionResult()
|
|
764
|
+
raise Exception(f"unknown method: {method}")
|