pkl-python 0.0.0__py3-none-any.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.
- pkl/__init__.py +118 -0
- pkl/evaluator_manager.py +384 -0
- pkl/evaluator_options.py +169 -0
- pkl/msgapi.py +312 -0
- pkl/parser.py +229 -0
- pkl/reader.py +64 -0
- pkl/server.py +156 -0
- pkl/utils.py +33 -0
- pkl_python-0.0.0.dist-info/LICENSE +21 -0
- pkl_python-0.0.0.dist-info/METADATA +168 -0
- pkl_python-0.0.0.dist-info/RECORD +14 -0
- pkl_python-0.0.0.dist-info/WHEEL +5 -0
- pkl_python-0.0.0.dist-info/entry_points.txt +2 -0
- pkl_python-0.0.0.dist-info/top_level.txt +1 -0
pkl/server.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import atexit
|
|
2
|
+
import os
|
|
3
|
+
import platform
|
|
4
|
+
import signal
|
|
5
|
+
import stat
|
|
6
|
+
import subprocess
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import msgpack
|
|
10
|
+
import requests
|
|
11
|
+
|
|
12
|
+
BINARIES = {
|
|
13
|
+
("darwin", "64bit"): "pkl-macos-amd64",
|
|
14
|
+
("darwin", "aarch64"): "pkl-macos-aarch64",
|
|
15
|
+
("linux", "64bit"): "pkl-linux-amd64",
|
|
16
|
+
("linux", "aarch64"): "pkl-linux-aarch64",
|
|
17
|
+
("linux", "64bit", "alpine"): "pkl-alpine-linux-amd64",
|
|
18
|
+
}
|
|
19
|
+
PKL_VERSION = "0.25.3"
|
|
20
|
+
BASE_PATH = "https://github.com/apple/pkl/releases/download/"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def preexec_function():
|
|
24
|
+
# Cause the child process to be terminated when the parent exits
|
|
25
|
+
signal.signal(signal.SIGHUP, signal.SIG_IGN)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def detect_system():
|
|
29
|
+
os_name = platform.system().lower()
|
|
30
|
+
arch, _ = platform.architecture()
|
|
31
|
+
return os_name, arch
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def is_alpine_linux():
|
|
35
|
+
return os.path.isfile("/etc/alpine-release")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def execute_binary(binary_path):
|
|
39
|
+
subprocess.run([binary_path, "server"], check=True)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def download_binary(file, target_fp):
|
|
43
|
+
url = BASE_PATH + PKL_VERSION + "/" + file
|
|
44
|
+
response = requests.get(url)
|
|
45
|
+
with open(target_fp, "wb") as f:
|
|
46
|
+
f.write(response.content)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def get_binary_path():
|
|
50
|
+
os_name, arch = detect_system()
|
|
51
|
+
if os_name == "linux" and is_alpine_linux():
|
|
52
|
+
os_name = "alpine"
|
|
53
|
+
binary_key = (os_name, arch)
|
|
54
|
+
if binary_key == ("linux", "64bit") and is_alpine_linux():
|
|
55
|
+
binary_key += ("alpine",)
|
|
56
|
+
bin_file = BINARIES.get(binary_key)
|
|
57
|
+
|
|
58
|
+
if bin_file is None:
|
|
59
|
+
raise OSError("No compatible binary found for your system.")
|
|
60
|
+
|
|
61
|
+
bin_parent_path = (Path("~/.pkl/bin/") / PKL_VERSION).expanduser()
|
|
62
|
+
binary_path = bin_parent_path / bin_file
|
|
63
|
+
|
|
64
|
+
if not binary_path.exists():
|
|
65
|
+
binary_path.parent.mkdir(exist_ok=True, parents=True)
|
|
66
|
+
download_binary(bin_file, binary_path)
|
|
67
|
+
|
|
68
|
+
current_permissions = os.stat(binary_path).st_mode
|
|
69
|
+
new_permissions = current_permissions | stat.S_IXUSR
|
|
70
|
+
os.chmod(binary_path, new_permissions)
|
|
71
|
+
|
|
72
|
+
return binary_path
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
_PROCESSES = []
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def terminate_processes():
|
|
79
|
+
for process in _PROCESSES:
|
|
80
|
+
process.terminate()
|
|
81
|
+
process.wait()
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
atexit.register(terminate_processes)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class PKLServer:
|
|
88
|
+
def __init__(self, cmd=None, debug=False):
|
|
89
|
+
self.cmd = cmd or [get_binary_path(), "server"]
|
|
90
|
+
self.next_request_id = 1
|
|
91
|
+
self.unpacker = msgpack.Unpacker()
|
|
92
|
+
|
|
93
|
+
env = {"PKL_DEBUG": "1"} if debug else {}
|
|
94
|
+
|
|
95
|
+
self.process = subprocess.Popen(
|
|
96
|
+
self.cmd,
|
|
97
|
+
stdin=subprocess.PIPE,
|
|
98
|
+
stdout=subprocess.PIPE,
|
|
99
|
+
stderr=subprocess.PIPE,
|
|
100
|
+
text=False,
|
|
101
|
+
bufsize=0,
|
|
102
|
+
preexec_fn=preexec_function,
|
|
103
|
+
env=env,
|
|
104
|
+
)
|
|
105
|
+
self.stdout = self.process.stdout
|
|
106
|
+
self.stdin = self.process.stdin
|
|
107
|
+
self.stderr = self.process.stderr
|
|
108
|
+
self.closed = False
|
|
109
|
+
|
|
110
|
+
os.set_blocking(self.stdout.fileno(), False)
|
|
111
|
+
os.set_blocking(self.stderr.fileno(), False)
|
|
112
|
+
_PROCESSES.append(self.process)
|
|
113
|
+
|
|
114
|
+
def get_request_id(self):
|
|
115
|
+
ret = self.next_request_id
|
|
116
|
+
self.next_request_id += 1
|
|
117
|
+
return ret
|
|
118
|
+
|
|
119
|
+
def send(self, msg):
|
|
120
|
+
if self.closed:
|
|
121
|
+
raise ValueError("Server closed")
|
|
122
|
+
self.stdin.write(msg)
|
|
123
|
+
self.stdin.flush()
|
|
124
|
+
|
|
125
|
+
def _read(self, stream):
|
|
126
|
+
msg = None
|
|
127
|
+
while msg is None:
|
|
128
|
+
msg = stream.read()
|
|
129
|
+
return msg
|
|
130
|
+
|
|
131
|
+
def _receive(self, stream):
|
|
132
|
+
while True:
|
|
133
|
+
for unpacked in self.unpacker:
|
|
134
|
+
return unpacked
|
|
135
|
+
msg = self._read(stream)
|
|
136
|
+
self.unpacker.feed(msg)
|
|
137
|
+
|
|
138
|
+
def receive(self):
|
|
139
|
+
if self.closed:
|
|
140
|
+
raise ValueError("Server closed")
|
|
141
|
+
return self._receive(self.stdout)
|
|
142
|
+
|
|
143
|
+
def receive_err(self):
|
|
144
|
+
if self.closed:
|
|
145
|
+
raise ValueError("Server closed")
|
|
146
|
+
msg = self.stderr.read()
|
|
147
|
+
if msg is not None:
|
|
148
|
+
print(msg.decode(), end="")
|
|
149
|
+
|
|
150
|
+
def terminate(self):
|
|
151
|
+
self.process.terminate()
|
|
152
|
+
self.process.stdout.close()
|
|
153
|
+
self.process.stderr.close()
|
|
154
|
+
self.process.stdin.close()
|
|
155
|
+
self.process.wait()
|
|
156
|
+
self.closed = True
|
pkl/utils.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Optional, Union
|
|
4
|
+
from urllib.parse import urlparse
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class PklBugError(Exception):
|
|
8
|
+
pass
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class PklError(Exception):
|
|
12
|
+
pass
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class ModuleSource:
|
|
17
|
+
uri: str
|
|
18
|
+
text: Optional[str] = None
|
|
19
|
+
|
|
20
|
+
@classmethod
|
|
21
|
+
def from_path(cls, path: Union[str, Path]):
|
|
22
|
+
uri = Path(path).absolute().as_uri()
|
|
23
|
+
return cls(uri=uri)
|
|
24
|
+
|
|
25
|
+
@classmethod
|
|
26
|
+
def from_text(cls, text: str):
|
|
27
|
+
return cls(uri="repl:text", text=text)
|
|
28
|
+
|
|
29
|
+
@classmethod
|
|
30
|
+
def from_uri(cls, uri):
|
|
31
|
+
res = urlparse(uri)
|
|
32
|
+
parsed = res.geturl()
|
|
33
|
+
return cls(uri=parsed)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jungwoo Yang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pkl-python
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: Python library for Apple's PKL.
|
|
5
|
+
Author-email: Jungwoo Yang <jwyang0213@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2024 Jungwoo Yang
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/jw-y/pkl
|
|
29
|
+
Project-URL: Bug Reports, https://github.com/jw-y/pkl/issues
|
|
30
|
+
Project-URL: Source, https://github.com/jw-y/pkl
|
|
31
|
+
Classifier: Programming Language :: Python :: 3
|
|
32
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
33
|
+
Classifier: Operating System :: OS Independent
|
|
34
|
+
Requires-Python: >=3.7
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
License-File: LICENSE
|
|
37
|
+
Requires-Dist: msgpack >=1.0.8
|
|
38
|
+
Provides-Extra: dev
|
|
39
|
+
Requires-Dist: pre-commit ; extra == 'dev'
|
|
40
|
+
Requires-Dist: black ; extra == 'dev'
|
|
41
|
+
Requires-Dist: isort ; extra == 'dev'
|
|
42
|
+
Requires-Dist: mypy ; extra == 'dev'
|
|
43
|
+
Requires-Dist: pylint ; extra == 'dev'
|
|
44
|
+
Requires-Dist: pytest ; extra == 'dev'
|
|
45
|
+
Requires-Dist: pytest-cov ; extra == 'dev'
|
|
46
|
+
Requires-Dist: tox ; extra == 'dev'
|
|
47
|
+
|
|
48
|
+
> [!CAUTION]
|
|
49
|
+
>
|
|
50
|
+
> # THIS LIBRARY IS CURRENTLY PRE-RELEASE
|
|
51
|
+
>
|
|
52
|
+
> `pkl-python` is currently major version `v0`, and **breaking changes will happen** between versions.
|
|
53
|
+
|
|
54
|
+
# pkl-python - Pkl Bindings for Python
|
|
55
|
+
Python binding for [Apple's Pkl language](https://pkl-lang.org/index.html).
|
|
56
|
+
|
|
57
|
+
## Getting Started
|
|
58
|
+
### Installation
|
|
59
|
+
|
|
60
|
+
``` bash
|
|
61
|
+
pip install pkl-python
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Basic Usage
|
|
65
|
+
Here's how you can start using `pkl-python` to load a PKL module:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
import pkl
|
|
69
|
+
|
|
70
|
+
config = pkl.load("path/to/pkl/example_module.pkl")
|
|
71
|
+
print(config)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Status
|
|
75
|
+
* Evaluator API: fully functional
|
|
76
|
+
* Code Generation: in development
|
|
77
|
+
|
|
78
|
+
### TODO
|
|
79
|
+
* [x] (codgen) fix class order
|
|
80
|
+
* [ ] (codgen) pip binary installation
|
|
81
|
+
* [ ] (codgen) clean up code
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
## Usage
|
|
85
|
+
|
|
86
|
+
## Advanced Features
|
|
87
|
+
For details on the parameters, refer [Message Passing API](https://pkl-lang.org/main/current/bindings-specification/message-passing-api.html).
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
import pkl
|
|
91
|
+
|
|
92
|
+
config = pkl.load("./tests/types.pkl")
|
|
93
|
+
config = pkl.load("./tests/types.pkl", expr="datasize")
|
|
94
|
+
config = pkl.load(None, module_text="a: Int = 1 + 1")
|
|
95
|
+
config = pkl.load("./tests/types.pkl", debug=True)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Custom Readers
|
|
99
|
+
It is possible to add module or resource or module readers:
|
|
100
|
+
```python
|
|
101
|
+
from typing import List
|
|
102
|
+
from dataclasses import dataclass
|
|
103
|
+
|
|
104
|
+
import pkl
|
|
105
|
+
from pkl import (
|
|
106
|
+
ModuleReader, ResourceReader, PathElement,
|
|
107
|
+
ModuleSource, PreconfiguredOptions, PklError,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
class TestModuleReader(ModuleReader):
|
|
111
|
+
def read(self, url) -> str:
|
|
112
|
+
return "foo = 1"
|
|
113
|
+
|
|
114
|
+
def list_elements(self, url: str) -> List[PathElement]:
|
|
115
|
+
return [PathElement("foo.pkl", False)]
|
|
116
|
+
|
|
117
|
+
opts = PreconfiguredOptions(
|
|
118
|
+
moduleReaders=[TestModuleReader("customfs", True, True, True)]
|
|
119
|
+
)
|
|
120
|
+
opts.allowedModules.append("customfs:")
|
|
121
|
+
config = pkl.load("./tests/myModule.pkl", evaluator_options=opts)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Appendix
|
|
125
|
+
|
|
126
|
+
### Type Mappings
|
|
127
|
+
|
|
128
|
+
While in pre-release they are subject to change.
|
|
129
|
+
|
|
130
|
+
| Pkl type | TypeScript type |
|
|
131
|
+
| ---------------- | -------------------------- |
|
|
132
|
+
| Null | `None` |
|
|
133
|
+
| Boolean | `bool` |
|
|
134
|
+
| String | `str` |
|
|
135
|
+
| Int | `int` |
|
|
136
|
+
| Int8 | `int` |
|
|
137
|
+
| Int16 | `int` |
|
|
138
|
+
| Int32 | `int` |
|
|
139
|
+
| UInt | `int` |
|
|
140
|
+
| UInt8 | `int` |
|
|
141
|
+
| UInt16 | `int` |
|
|
142
|
+
| UInt32 | `int` |
|
|
143
|
+
| Float | `float` |
|
|
144
|
+
| Number | `float` |
|
|
145
|
+
| List | `list` |
|
|
146
|
+
| Listing | `list` |
|
|
147
|
+
| Map | `dict` |
|
|
148
|
+
| Mapping | `dict` |
|
|
149
|
+
| Set | `set` |
|
|
150
|
+
| Pair | `pkl.Pair` |
|
|
151
|
+
| Dynamic | `dataclasses.dataclass` |
|
|
152
|
+
| DataSize | `pkl.DataSize` |
|
|
153
|
+
| Duration | `pkl.Duration` |
|
|
154
|
+
| IntSeq | `pkl.IntSeq` |
|
|
155
|
+
| Class | `dataclasses.dataclass` |
|
|
156
|
+
| TypeAlias | `typing` |
|
|
157
|
+
| Any | `typing.Any` |
|
|
158
|
+
| Unions (A\|B\|C) | `typing.Union[A\|B\|C]` |
|
|
159
|
+
| Regex | `pkl.Regex` |
|
|
160
|
+
|
|
161
|
+
## Contributing
|
|
162
|
+
Contributions are welcome! If you'd like to contribute, please fork the repository and submit a pull request. For major changes, please open an issue first to discuss what you would like to change.
|
|
163
|
+
|
|
164
|
+
## License
|
|
165
|
+
PKL is released under the MIT License. See the LICENSE file for more details.
|
|
166
|
+
|
|
167
|
+
## Contact
|
|
168
|
+
For support or to contribute, please contact jwyang0213@gmail.com or visit our GitHub repository to report issues or submit pull requests.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
pkl/__init__.py,sha256=FebsX_7t0B6Up7ZuSV8eRNBcRYmy317gdnZ-l4QjzfU,3970
|
|
2
|
+
pkl/evaluator_manager.py,sha256=uPvOQMGG6xhpdIAdAqGUqlB-P4TVxeR1nZ76b1DkGIQ,12637
|
|
3
|
+
pkl/evaluator_options.py,sha256=KLe37agqCidUJvWfUIGuCbIDW9LzJSsGv10yR9W_eWk,4841
|
|
4
|
+
pkl/msgapi.py,sha256=wlX1t0eqB4Vj0d2KYgosy3LsA92eX5tE6GwugJqQRqU,8111
|
|
5
|
+
pkl/parser.py,sha256=ygrY68Bo-hFTas07iHwL2ZDTS9uPeCgjFCmOWsjmN4g,6260
|
|
6
|
+
pkl/reader.py,sha256=z3fHHt5nZuu-Q7RqXB9wWeEdA8-jFyf0yizuT4y6Ix0,1780
|
|
7
|
+
pkl/server.py,sha256=Rof4heSCvrFSoc6ggt69soKmLRcuur3UaK6y0LMykRU,4114
|
|
8
|
+
pkl/utils.py,sha256=1SFKaIdFgDo8Z8fVBtq_5LZeKb2qNF2fuTDLbG58WVg,658
|
|
9
|
+
pkl_python-0.0.0.dist-info/LICENSE,sha256=xakNlyP5yr5S03TW66xSAFmKjgc3eC4Dxz0ggbmFzFM,1069
|
|
10
|
+
pkl_python-0.0.0.dist-info/METADATA,sha256=YhjQbrkFWI2vKLfxTV4Fv0cYAmkLr8pkB_NYa55qB8o,5998
|
|
11
|
+
pkl_python-0.0.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
12
|
+
pkl_python-0.0.0.dist-info/entry_points.txt,sha256=awJKbVyaDuuDJuBfTUUOkJ0tdfpPFCh5AjmW9uC8FZI,55
|
|
13
|
+
pkl_python-0.0.0.dist-info/top_level.txt,sha256=bOuzzlEm20X6PyirtLenvxqt_ukmxWuxP4odSZyuD80,4
|
|
14
|
+
pkl_python-0.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pkl
|