Skip to content

Commit 80535a4

Browse files
authored
Merge pull request #395 from Peefy/update-plugin-documents
docs: update kcl python and java documents
2 parents 4374960 + 4e2126d commit 80535a4

File tree

9 files changed

+211
-624
lines changed

9 files changed

+211
-624
lines changed

docs/reference/lang/codelab/schema.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ Suppose we have some schema logic, we can wrapper it into schema:
164164
```python
165165
schema Deployment[priority]:
166166
name: str
167-
cpu: int = _cpu
168-
memory: int = _cpu * 2
169167
image: str
170168
service: "my-service" = "my-service"
171169
replica: int = 1
@@ -181,6 +179,9 @@ schema Deployment[priority]:
181179
_cpu = 1024
182180
else:
183181
_cpu = 2048
182+
183+
cpu: int = _cpu
184+
memory: int = _cpu * 2
184185
```
185186

186187
Now, we can define a config by creating a schema instance and pass in priority as an argument to schema:
@@ -227,8 +228,6 @@ Now we want to define a detailed schema with service and volumes, we can do it a
227228
```python
228229
schema Deployment[priority]:
229230
name: str
230-
cpu: int = _cpu
231-
memory: int = _cpu * 2
232231
volumes?: [Volume]
233232
image: str
234233
service?: Service
@@ -245,6 +244,9 @@ schema Deployment[priority]:
245244
else:
246245
_cpu = 2048
247246

247+
cpu: int = _cpu
248+
memory: int = _cpu * 2
249+
248250
schema Port:
249251
name: str
250252
protocol: str
@@ -373,8 +375,6 @@ import regex
373375

374376
schema Deployment[priority]:
375377
name: str
376-
cpu: int = _cpu
377-
memory: int = _cpu * 2
378378
volumes?: [Volume]
379379
image: str
380380
service?: Service
@@ -391,6 +391,9 @@ schema Deployment[priority]:
391391
else:
392392
_cpu = 2048
393393

394+
cpu: int = _cpu
395+
memory: int = _cpu * 2
396+
394397
check:
395398
multiplyof(cpu, 256), "cpu must be a multiplier of 256"
396399
regex.match(image, "^[a-zA-Z]+:\d+\.\d+\.\d+$"), "image name should be like 'nginx:1.14.2'"

docs/reference/plugin/overview.md

Lines changed: 41 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -101,184 +101,76 @@ func TestPluginAdd(t *testing.T) {
101101

102102
### 0. Prerequisites
103103

104-
Using the KCL Python plugin requires the presence of `Python 3.7+` in your `PATH`, install the KCL python SDK and set the plugin path.
104+
Using the KCL Python plugin requires the presence of `Python 3.7+` in your `PATH` and install the KCL python SDK.
105105

106106
```shell
107-
python3 -m pip install kclvm
108-
alias kcl-plugin="python3 -m kclvm.tools.plugin"
109-
export KCL_PLUGINS_ROOT=~/.kcl/plugins
107+
python3 -m pip kcl_lib
110108
```
111109

112110
### 1. Hello Plugin
113111

114-
KCL plugins are installed in the `plugins` subdirectory of KCL (usually installed in the `$HOME/.kcl/plugins` directory), or set through the `$KCL_PLUGINS_ROOT` environment variable. Besides, the `plugins` directory could also be placed at the `pwd` path. KCL plugins are managed in the Git repository: [https://github.com/kcl-lang/kcl-plugin](https://github.com/kcl-lang/kcl-plugin), we can clone the repository for development.
115-
116-
Enter the `kcl-plugin info` command to view the plugin directory (replace `/Users/kcl_user` with the local `$HOME` path):
117-
118-
```shell
119-
$ kcl-plugin info
120-
# plugin_root: /Users/kcl_user/.kcl/plugins
121-
```
122-
123-
View the list of plugins with the `kcl-plugin list` subcommand:
124-
125-
```shell
126-
$ kcl-plugin list
127-
hello: hello doc - 0.0.1
128-
```
129-
130-
Where `hello` is an example builtin plugin (do not modify the plugin).
131-
132-
In KCL code, the `hello` plugin can be imported via `import kcl_plugin.hello`. `main.k` code is as follows:
112+
Write the following Python code and add the the plugin named `my_plugin`.
133113

134114
```python
135-
import kcl_plugin.hello
115+
import kcl_lib.plugin as plugin
116+
import kcl_lib.api as api
136117
137-
name = "kcl"
138-
three = hello.add(1,2)
139-
```
118+
plugin.register_plugin("my_plugin", {"add": lambda x, y: x + y})
140119
141-
The output result is
120+
def main():
121+
result = api.API().exec_program(
122+
api.ExecProgram_Args(k_filename_list=["test.k"])
123+
)
124+
assert result.yaml_result == "result: 2"
142125
143-
```shell
144-
$ python3 -m kclvm main.k
145-
name: kcl
146-
three: 3
126+
main()
147127
```
148128

149-
### 2. `kcl-plugin` Command
129+
The content of `test.k` are:
150130

151-
`kcl-plugin` is a plugin helper command line tool, the command line help is as follows:
131+
```python
132+
import kcl_plugin.my_plugin
152133
153-
```shell
154-
$ kcl-plugin
155-
usage: kcl-plugin [-h] {list,info,init,gendoc,test} ...
156-
positional arguments:
157-
{list,info,init,gendoc,test}
158-
kcl plugin sub commands
159-
list list all plugins
160-
info show plugin document
161-
init init a new plugin
162-
gendoc gen all plugins document
163-
test test plugin
164-
optional arguments:
165-
-h, --help show this help message and exit
134+
result = my_plugin.add(1, 1)
166135
```
167136

168-
- The `list` subcommand is used to view the list of plugins.
169-
- The `info` subcommand is used to view the plugin directory and information about each plugin.
170-
- The `init` subcommand is used to initialize new plugins.
171-
- The `gendoc` subcommand is used to update the API documentation of all plugins.
172-
- The `test` subcommand is used to test specified plugins.
137+
## Use Java to Write Plugins
173138

174-
### 3. Plugin Information and Documentation
139+
### 0. Prerequisites
175140

176-
Enter `kcl-plugin info hello` to view the `hello` plugin information:
141+
Using the KCL Java plugin requires the presence of `Java 8+` in your `PATH` and install the KCL Java SDK.
177142

178-
```shell
179-
$ kcl-plugin info hello
180-
{
181-
"name": "hello",
182-
"describe": "hello doc",
183-
"long_describe": "long describe",
184-
"version": "0.0.1",
185-
"method": {
186-
"add": "add two numbers, and return result",
187-
"foo": "no doc",
188-
"list_append": "no doc",
189-
"say_hello": "no doc",
190-
"tolower": "no doc",
191-
"update_dict": "no doc"
192-
}
193-
}
194-
```
143+
### 1. Hello Plugin
195144

196-
The information of the plugin mainly includes the name and version information of the plugin, and the function information provided by the plugin. This information is consistent with the automatically generated `api.md` file in the plugin directory (regenerate the `api.md` file for all plugins via `kcl-plugin gendoc` when the plugin API document changes).
145+
Write the following Java code and add the the plugin named `my_plugin`.
197146

198-
### 4. Plugin Directory Structure
147+
```java
148+
package com.kcl;
199149
200-
The directory structure of the plugin is as follows (replace `/Users/kcl_user` with the local `$HOME` path):
150+
import com.kcl.api.API;
151+
import com.kcl.api.Spec.ExecProgram_Args;
152+
import com.kcl.api.Spec.ExecProgram_Result;
201153
202-
```shell
203-
$ tree /Users/kcl_user/.kcl/plugins/
204-
/Users/kcl_user/.kcl/plugins/
205-
├── _examples
206-
├── _test
207-
└── hello
208-
├── api.md
209-
├── plugin.py
210-
└── plugin_test.py
211-
$
212-
```
154+
import java.util.Collections;
213155
214-
The `_examples` directory is the sample code of the plugin, the `_test` directory is the KCL test code of the plugin, and the other directories starting with letters are ordinary plugins. The content of the plugin is as follows:
156+
public class PluginTest {
157+
public static void main(String[] mainArgs) throws Exception {
158+
API.registerPlugin("my_plugin", Collections.singletonMap("add", (args, kwArgs) -> {
159+
return (int) args[0] + (int) args[1];
160+
}));
161+
API api = new API();
215162
216-
```shell
217-
$ cat ./hello/plugin.py
218-
# Copyright 2020 The KCL Authors. All rights reserved.
219-
INFO = {
220-
'name': 'hello',
221-
'describe': 'hello doc',
222-
'long_describe': 'long describe',
223-
'version': '0.0.1',
163+
ExecProgram_Result result = api
164+
.execProgram(ExecProgram_Args.newBuilder().addKFilenameList("test.k").build());
165+
System.out.println(result.getYamlResult());
166+
}
224167
}
225-
def add(a: int, b: int) -> int:
226-
"""add two numbers, and return result"""
227-
return a + b
228-
...
229-
```
230-
231-
Where `INFO` specifies the name of the plugin, a brief description, a detailed description and version information. And all the functions whose names start with letters are the functions provided by the plugin, so the `add` function can be called directly in KCL.
232-
233-
> Note: KCL plugins are implemented in an independent pure Python code file, and plugins cannot directly call each other.
234-
235-
### 5. Create Plugin
236-
237-
A plugin can be created with the `kcl-plugin init` command:
238-
239168
```
240-
$ kcl-plugin init hi
241-
$ kcl-plugin list
242-
hello: hello doc - 0.0.1
243-
hi: hi doc - 0.0.1
244-
```
245-
246-
The `kcl-plugin init` command will construct a new plugin from the built-in template, and then we can view the created plugin information with the `kcl-plugin list` command.
247-
248-
### 6. Remove Plugin
249169

250-
KCL plugins are located in the `plugins` subdirectory of KCL (usually installed in the `$HOME/.kcl/plugins` directory).
251-
We can query the plugin installation directory with the command `kcl-plugin info`.
252-
253-
```shell
254-
$ kcl-plugin info
255-
/Users/kcl_user/.kcl/plugins/
256-
$ tree /Users/kcl_user/.kcl/plugins/
257-
/Users/kcl_user/.kcl/plugins/
258-
├── _examples
259-
├── _test
260-
└── hello -- Delete this directory to delete the hello plugin
261-
├── api.md
262-
├── plugin.py
263-
└── plugin_test.py
264-
$
265-
```
170+
The content of `test.k` are:
266171

267-
### 7. Test Plugin
268-
269-
There is a `plugin_test.py` file in the plugin directory, which is the unit test file of the plugin (based on the `pytest` testing framework). Also placed in the `_test` directory are plugin integration tests for KCL files. The `plugin_test.py` unit test is required, and the KCL integration tests in the `_test` directory can be added as needed.
270-
271-
Unit tests for plugins can be executed via `kcl-plugin test`:
172+
```python
173+
import kcl_plugin.my_plugin
272174
273-
```shell
274-
$ kcl-plugin test hello
275-
============================= test session starts ==============================
276-
platform darwin -- Python 3.7.6+, pytest-5.3.5, py-1.9.0, pluggy-0.13.1
277-
rootdir: /Users/kcl_user
278-
collected 5 items
279-
.kcl/plugins/hello/plugin_test.py ..... [100%]
280-
============================== 5 passed in 0.03s ===============================
281-
$
175+
result = my_plugin.add(1, 1)
282176
```
283-
284-
Integration tests can be tested by executing the `python3 -m pytest` command in the `_test` directory.

examples/codelab/schema/my_config.k

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import regex
22

33
schema Deployment[priority]:
44
name: str
5-
cpu: int = _cpu
6-
memory: int = _cpu * 2
75
volumes?: [Volume]
86
image: str
97
service?: Service
@@ -20,6 +18,9 @@ schema Deployment[priority]:
2018
else:
2119
_cpu = 2048
2220

21+
cpu: int = _cpu
22+
memory: int = _cpu * 2
23+
2324
check:
2425
multiplyof(cpu, 256), "cpu must be a multiplier of 256"
2526
regex.match(image, "^[a-zA-Z]+:\d+\.\d+\.\d+$"), "image name should be like 'nginx:1.14.2'"

i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/lang/codelab/schema.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ nginx = Deployment {
164164
```python
165165
schema Deployment[priority]:
166166
name: str
167-
cpu: int = _cpu
168-
memory: int = _cpu * 2
169167
image: str
170168
service: "my-service" = "my-service"
171169
replica: int = 1
@@ -181,6 +179,9 @@ schema Deployment[priority]:
181179
_cpu = 1024
182180
else:
183181
_cpu = 2048
182+
183+
cpu: int = _cpu
184+
memory: int = _cpu * 2
184185
```
185186

186187
现在,我们可以通过创建 schema 实例来定义配置,并将优先级作为参数传递给模式:
@@ -227,8 +228,6 @@ nginx:
227228
```python
228229
schema Deployment[priority]:
229230
name: str
230-
cpu: int = _cpu
231-
memory: int = _cpu * 2
232231
volumes?: [Volume]
233232
image: str
234233
service?: Service
@@ -244,6 +243,9 @@ schema Deployment[priority]:
244243
_cpu = 1024
245244
else:
246245
_cpu = 2048
246+
247+
cpu: int = _cpu
248+
memory: int = _cpu * 2
247249

248250
schema Port:
249251
name: str
@@ -373,8 +375,6 @@ import regex
373375

374376
schema Deployment[priority]:
375377
name: str
376-
cpu: int = _cpu
377-
memory: int = _cpu * 2
378378
volumes?: [Volume]
379379
image: str
380380
service?: Service
@@ -391,6 +391,9 @@ schema Deployment[priority]:
391391
else:
392392
_cpu = 2048
393393

394+
cpu: int = _cpu
395+
memory: int = _cpu * 2
396+
394397
check:
395398
multiplyof(cpu, 256), "cpu must be a multiplier of 256"
396399
regex.match(image, "^[a-zA-Z]+:\d+\.\d+\.\d+$"), "image name should be like 'nginx:1.14.2'"

0 commit comments

Comments
 (0)