pkl-python 0.1.10__tar.gz → 0.1.12__tar.gz

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.
Files changed (37) hide show
  1. {pkl-python-0.1.10 → pkl-python-0.1.12}/PKG-INFO +86 -56
  2. pkl-python-0.1.12/README.md +121 -0
  3. pkl-python-0.1.12/pkl/VERSION +1 -0
  4. pkl-python-0.1.12/pkl/__init__.py +118 -0
  5. pkl-python-0.1.12/pkl/evaluator_manager.py +384 -0
  6. pkl-python-0.1.12/pkl/evaluator_options.py +169 -0
  7. pkl-python-0.1.12/pkl/msgapi.py +312 -0
  8. {pkl-python-0.1.10 → pkl-python-0.1.12}/pkl/parser.py +33 -73
  9. pkl-python-0.1.12/pkl/reader.py +64 -0
  10. pkl-python-0.1.12/pkl/server.py +156 -0
  11. pkl-python-0.1.12/pkl/utils.py +33 -0
  12. {pkl-python-0.1.10 → pkl-python-0.1.12}/pkl_python.egg-info/PKG-INFO +86 -56
  13. {pkl-python-0.1.10 → pkl-python-0.1.12}/pkl_python.egg-info/SOURCES.txt +7 -4
  14. pkl-python-0.1.12/pkl_python.egg-info/top_level.txt +1 -0
  15. {pkl-python-0.1.10 → pkl-python-0.1.12}/pyproject.toml +2 -2
  16. {pkl-python-0.1.10 → pkl-python-0.1.12}/setup.py +1 -1
  17. pkl-python-0.1.12/tests/test_evaluator_manager.py +14 -0
  18. pkl-python-0.1.12/tests/test_load.py +19 -0
  19. pkl-python-0.1.12/tests/test_readers.py +63 -0
  20. {pkl-python-0.1.10 → pkl-python-0.1.12}/tests/test_server.py +0 -1
  21. pkl-python-0.1.10/README.md +0 -91
  22. pkl-python-0.1.10/pkl/VERSION +0 -1
  23. pkl-python-0.1.10/pkl/__init__.py +0 -181
  24. pkl-python-0.1.10/pkl/evaluator.py +0 -257
  25. pkl-python-0.1.10/pkl/handler.py +0 -37
  26. pkl-python-0.1.10/pkl/msgapi.py +0 -274
  27. pkl-python-0.1.10/pkl/server.py +0 -191
  28. pkl-python-0.1.10/pkl_python.egg-info/top_level.txt +0 -5
  29. pkl-python-0.1.10/tests/test_evaluator.py +0 -7
  30. pkl-python-0.1.10/tests/test_responses.py +0 -99
  31. {pkl-python-0.1.10 → pkl-python-0.1.12}/LICENSE +0 -0
  32. {pkl-python-0.1.10 → pkl-python-0.1.12}/pkl_python.egg-info/dependency_links.txt +0 -0
  33. {pkl-python-0.1.10 → pkl-python-0.1.12}/pkl_python.egg-info/requires.txt +0 -0
  34. {pkl-python-0.1.10 → pkl-python-0.1.12}/scripts/download_binary.py +0 -0
  35. {pkl-python-0.1.10 → pkl-python-0.1.12}/scripts/eval.py +0 -0
  36. {pkl-python-0.1.10 → pkl-python-0.1.12}/setup.cfg +0 -0
  37. {pkl-python-0.1.10 → pkl-python-0.1.12}/tests/test_parser.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pkl-python
3
- Version: 0.1.10
3
+ Version: 0.1.12
4
4
  Summary: Python library for Apple's PKL.
5
5
  Author-email: Jungwoo Yang <jwyang0213@gmail.com>
6
6
  License: MIT License
@@ -31,7 +31,7 @@ Project-URL: Source, https://github.com/jw-y/pkl
31
31
  Classifier: Programming Language :: Python :: 3
32
32
  Classifier: License :: OSI Approved :: MIT License
33
33
  Classifier: Operating System :: OS Independent
34
- Requires-Python: >=3.6
34
+ Requires-Python: >=3.7
35
35
  Description-Content-Type: text/markdown
36
36
  License-File: LICENSE
37
37
  Requires-Dist: msgpack>=1.0.8
@@ -45,27 +45,24 @@ Requires-Dist: pytest; extra == "dev"
45
45
  Requires-Dist: pytest-cov; extra == "dev"
46
46
  Requires-Dist: tox; extra == "dev"
47
47
 
48
- # pkl-python - PKL Language Python Binding
49
- Python binding for [Apple's Pkl language](https://pkl-lang.org/index.html).
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.
50
53
 
51
- ### Status
52
- * Evaluator API: fully functional
53
- * Code Generation: in development
54
-
55
- ### TODO
56
- * [ ] (codgen) binary installation feature
57
- * [ ] (codgen) fix class order
58
- * [ ] (codgen) clean up code
54
+ # pkl-python - Pkl Bindings for Python
55
+ Python binding for [Apple's Pkl language](https://pkl-lang.org/index.html).
59
56
 
60
- ## Installation
57
+ ## Getting Started
58
+ ### Installation
61
59
 
62
60
  ``` bash
63
61
  pip install pkl-python
64
62
  ```
65
63
 
66
- ## Usage
67
64
  ### Basic Usage
68
- Here's how you can start using PKLL to load a PKL module:
65
+ Here's how you can start using `pkl-python` to load a PKL module:
69
66
 
70
67
  ```python
71
68
  import pkl
@@ -74,60 +71,93 @@ config = pkl.load("path/to/pkl/example_module.pkl")
74
71
  print(config)
75
72
  ```
76
73
 
74
+ ### Status
75
+ * Evaluator API: fully functional
76
+ * Code Generation: in development
77
+
78
+ ### TODO
79
+ * [ ] (codgen) pip binary installation
80
+ * [ ] (codgen) fix class order
81
+ * [ ] (codgen) clean up code
82
+
83
+
84
+ ## Usage
85
+
77
86
  ## Advanced Features
78
87
  For details on the parameters, refer [Message Passing API](https://pkl-lang.org/main/current/bindings-specification/message-passing-api.html).
79
88
 
80
89
  ```python
81
- from pkl import load
90
+ import pkl
82
91
 
83
- # Advanced loading with custom environment and properties
84
- result = load(
85
- "path/to/pkl/example_module.pkl"
86
- env={"CUSTOM_ENV": "value"},
87
- properties={"custom.property": "value"}
88
- )
89
- print(result)
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)
90
96
  ```
91
97
 
92
- ### Custom Handler
93
- It is possible to add custom resources or module handler:
98
+ ### Custom Readers
99
+ It is possible to add module or resource or module readers:
94
100
  ```python
101
+ from typing import List
102
+ from dataclasses import dataclass
103
+
95
104
  import pkl
96
- from pkl.handler import (
97
- ListResponse,
98
- ReadModuleResponse,
99
- ReadResourceResponse,
100
- ResourcesHandler,
105
+ from pkl import (
106
+ ModuleReader, ResourceReader, PathElement,
107
+ ModuleSource, PreconfiguredOptions, PklError,
101
108
  )
102
- from pkl.msgapi import ClientResourceReader
103
-
104
- class CustomModuleHandler(ResourcesHandler):
105
- def list_response(self, uri: str) -> ListResponse:
106
- return ListResponse(
107
- pathElements=[{"name": "foo.pkl", "isDirectory": False}]
108
- )
109
-
110
- def read_response(self, uri: str) -> ReadResourceResponse:
111
- return ReadModuleResponse(
112
- contents="foo = 1",
113
- )
114
-
115
- config = pkl.load(
116
- "./tests/myModule.pkl",
117
- allowedModules=["pkl:", "repl:", "file:", "customfs:"],
118
- clientModuleReaders=[
119
- {
120
- "scheme": "customfs",
121
- "hasHierarchicalUris": True,
122
- "isGlobbable": True,
123
- "isLocal": True,
124
- }
125
- ],
126
- debug=True,
127
- module_handler=CustomModuleHandler(),
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)]
128
119
  )
120
+ opts.allowedModules.append("customfs:")
121
+ config = pkl.load("./tests/myModule.pkl", evaluator_options=opts)
129
122
  ```
130
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
+
131
161
  ## Contributing
132
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.
133
163
 
@@ -0,0 +1,121 @@
1
+ > [!CAUTION]
2
+ >
3
+ > # THIS LIBRARY IS CURRENTLY PRE-RELEASE
4
+ >
5
+ > `pkl-python` is currently major version `v0`, and **breaking changes will happen** between versions.
6
+
7
+ # pkl-python - Pkl Bindings for Python
8
+ Python binding for [Apple's Pkl language](https://pkl-lang.org/index.html).
9
+
10
+ ## Getting Started
11
+ ### Installation
12
+
13
+ ``` bash
14
+ pip install pkl-python
15
+ ```
16
+
17
+ ### Basic Usage
18
+ Here's how you can start using `pkl-python` to load a PKL module:
19
+
20
+ ```python
21
+ import pkl
22
+
23
+ config = pkl.load("path/to/pkl/example_module.pkl")
24
+ print(config)
25
+ ```
26
+
27
+ ### Status
28
+ * Evaluator API: fully functional
29
+ * Code Generation: in development
30
+
31
+ ### TODO
32
+ * [ ] (codgen) pip binary installation
33
+ * [ ] (codgen) fix class order
34
+ * [ ] (codgen) clean up code
35
+
36
+
37
+ ## Usage
38
+
39
+ ## Advanced Features
40
+ For details on the parameters, refer [Message Passing API](https://pkl-lang.org/main/current/bindings-specification/message-passing-api.html).
41
+
42
+ ```python
43
+ import pkl
44
+
45
+ config = pkl.load("./tests/types.pkl")
46
+ config = pkl.load("./tests/types.pkl", expr="datasize")
47
+ config = pkl.load(None, module_text="a: Int = 1 + 1")
48
+ config = pkl.load("./tests/types.pkl", debug=True)
49
+ ```
50
+
51
+ ### Custom Readers
52
+ It is possible to add module or resource or module readers:
53
+ ```python
54
+ from typing import List
55
+ from dataclasses import dataclass
56
+
57
+ import pkl
58
+ from pkl import (
59
+ ModuleReader, ResourceReader, PathElement,
60
+ ModuleSource, PreconfiguredOptions, PklError,
61
+ )
62
+
63
+ class TestModuleReader(ModuleReader):
64
+ def read(self, url) -> str:
65
+ return "foo = 1"
66
+
67
+ def list_elements(self, url: str) -> List[PathElement]:
68
+ return [PathElement("foo.pkl", False)]
69
+
70
+ opts = PreconfiguredOptions(
71
+ moduleReaders=[TestModuleReader("customfs", True, True, True)]
72
+ )
73
+ opts.allowedModules.append("customfs:")
74
+ config = pkl.load("./tests/myModule.pkl", evaluator_options=opts)
75
+ ```
76
+
77
+ ## Appendix
78
+
79
+ ### Type Mappings
80
+
81
+ While in pre-release they are subject to change.
82
+
83
+ | Pkl type | TypeScript type |
84
+ | ---------------- | -------------------------- |
85
+ | Null | `None` |
86
+ | Boolean | `bool` |
87
+ | String | `str` |
88
+ | Int | `int` |
89
+ | Int8 | `int` |
90
+ | Int16 | `int` |
91
+ | Int32 | `int` |
92
+ | UInt | `int` |
93
+ | UInt8 | `int` |
94
+ | UInt16 | `int` |
95
+ | UInt32 | `int` |
96
+ | Float | `float` |
97
+ | Number | `float` |
98
+ | List | `list` |
99
+ | Listing | `list` |
100
+ | Map | `dict` |
101
+ | Mapping | `dict` |
102
+ | Set | `set` |
103
+ | Pair | `pkl.Pair` |
104
+ | Dynamic | `dataclasses.dataclass` |
105
+ | DataSize | `pkl.DataSize` |
106
+ | Duration | `pkl.Duration` |
107
+ | IntSeq | `pkl.IntSeq` |
108
+ | Class | `dataclasses.dataclass` |
109
+ | TypeAlias | `typing` |
110
+ | Any | `typing.Any` |
111
+ | Unions (A\|B\|C) | `typing.Union[A\|B\|C]` |
112
+ | Regex | `pkl.Regex` |
113
+
114
+ ## Contributing
115
+ 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.
116
+
117
+ ## License
118
+ PKL is released under the MIT License. See the LICENSE file for more details.
119
+
120
+ ## Contact
121
+ 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 @@
1
+ 0.1.12
@@ -0,0 +1,118 @@
1
+ import os
2
+ from pathlib import Path
3
+ from typing import Optional, Union
4
+ from urllib.parse import ParseResult, urlparse
5
+
6
+ from pkl.evaluator_manager import Evaluator, EvaluatorManager
7
+ from pkl.evaluator_options import EvaluatorOptions, PreconfiguredOptions
8
+ from pkl.parser import DataSize, Duration, IntSeq, Pair, Parser, Regex
9
+ from pkl.reader import ModuleReader, PathElement, ResourceReader
10
+ from pkl.utils import ModuleSource, PklBugError, PklError
11
+
12
+ # get version
13
+ with open(os.path.join(os.path.dirname(__file__), "VERSION"), "r") as _f:
14
+ __version__ = _f.read().strip()
15
+
16
+
17
+ class PklDefaultType:
18
+ def __repr__(self):
19
+ return "<PklDefault>"
20
+
21
+
22
+ PKL_DEFAULT = PklDefaultType()
23
+
24
+
25
+ def _search_project_dir(module_path: str) -> str:
26
+ cur_path = Path(module_path).parent.absolute()
27
+ while not (cur_path / "PklProject").exists():
28
+ cur_path = cur_path.parent
29
+ if str(cur_path) == "/":
30
+ break
31
+
32
+ if str(cur_path) == "/":
33
+ cur_path = Path(module_path).parent
34
+ return str(cur_path.absolute())
35
+
36
+
37
+ def load(
38
+ module_uri: Union[str, Path],
39
+ *,
40
+ module_text: Optional[str] = None,
41
+ expr: Optional[str] = None,
42
+ project_dir: str = PKL_DEFAULT,
43
+ evaluator_options: EvaluatorOptions = PreconfiguredOptions(),
44
+ parser=None,
45
+ debug=False,
46
+ **kwargs,
47
+ ):
48
+ """
49
+ Loads and evaluates a Pkl module or expression with specified parameters and customization options.
50
+
51
+ Args:
52
+ module_uri (str): The absolute URI of the module to be loaded.
53
+ module_text (Optional[str], None): Optionally, the content of the module to be loaded.
54
+ If None, the module is loaded from the specified URI.
55
+ expr (Optional[str], None): Optionally, a Pkl expression to be evaluated
56
+ within the loaded module. If None, the entire module is evaluated.
57
+ project_dir (str, PKL_DEFAULT): The project directory to use for this command.
58
+ By default, searches up from the working directory for a PklProject file.
59
+ evaluator_options (EvaluatorOptions, PreconfiguredOptions()):
60
+ extra options for evaluator
61
+ parser: A specific parser to be used for parsing the module.
62
+ If None, a default parser is used.
63
+ debug (bool, False): Enable debugging mode for additional output and diagnostics.
64
+ **kwargs: Additional keyword arguments for extensibility and future use.
65
+
66
+ Returns:
67
+ The result of the module or expression evaluation, depending on the inputs and configuration.
68
+
69
+ This function provides a flexible interface for loading and evaluating Pkl modules
70
+ with a variety of customization options, including custom module and resource readers,
71
+ environmental configurations, and support for complex project dependencies.
72
+ """
73
+
74
+ parsed = urlparse(str(module_uri))
75
+
76
+ def is_uri(_uri: ParseResult):
77
+ return bool(_uri.scheme) and (bool(_uri.netloc) or bool(_uri.path))
78
+
79
+ if module_text:
80
+ source = ModuleSource.from_text(module_text)
81
+ elif is_uri(parsed):
82
+ source = ModuleSource.from_uri(module_uri)
83
+ else:
84
+ source = ModuleSource.from_path(module_uri)
85
+
86
+ if project_dir is PKL_DEFAULT:
87
+ project_dir = _search_project_dir(str(module_uri))
88
+
89
+ with EvaluatorManager(debug=debug) as manager:
90
+ if (Path(project_dir) / "PklProject").exists():
91
+ evaluator = manager.new_project_evaluator(
92
+ project_dir, evaluator_options, parser=parser
93
+ )
94
+ else:
95
+ evaluator = manager.new_evaluator(evaluator_options, parser=parser)
96
+ config = evaluator.evaluate_expression(source, expr)
97
+ return config
98
+
99
+
100
+ __all__ = [
101
+ "load",
102
+ "Evaluator",
103
+ "EvaluatorManager",
104
+ "EvaluatorOptions",
105
+ "PreconfiguredOptions",
106
+ "ModuleReader",
107
+ "ResourceReader",
108
+ "PathElement",
109
+ "Parser",
110
+ "ModuleSource",
111
+ "PklError",
112
+ "PklBugError",
113
+ "Duration",
114
+ "DataSize",
115
+ "Pair",
116
+ "IntSeq",
117
+ "Regex",
118
+ ]