pkl-python 0.1.0__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.0/LICENSE +21 -0
  2. pkl-python-0.1.0/PKG-INFO +138 -0
  3. pkl-python-0.1.0/README.md +91 -0
  4. pkl-python-0.1.0/archive/cmd/pkl-gen-go/pkl-gen-python.py +102 -0
  5. pkl-python-0.1.0/archive/cmd/pkl-gen-python/pkl-gen-python.py +235 -0
  6. pkl-python-0.1.0/old/pkl-gen-python/generate.py +112 -0
  7. pkl-python-0.1.0/old/pkl-gen-python/main.py +94 -0
  8. pkl-python-0.1.0/out/ExtendModule_pkl.py +29 -0
  9. pkl-python-0.1.0/out/ExtendingOpenClass_pkl.py +57 -0
  10. pkl-python-0.1.0/out/Foo_pkl.py +61 -0
  11. pkl-python-0.1.0/out/Imports_pkl.py +20 -0
  12. pkl-python-0.1.0/out/MyModule_pkl.py +13 -0
  13. pkl-python-0.1.0/out/com_example_ExtendedSimple_pkl.py +33 -0
  14. pkl-python-0.1.0/out/com_example_Simple_pkl.py +65 -0
  15. pkl-python-0.1.0/out/lib3_pkl.py +24 -0
  16. pkl-python-0.1.0/out/union_pkl.py +37 -0
  17. pkl-python-0.1.0/pkl/VERSION +1 -0
  18. pkl-python-0.1.0/pkl/__init__.py +181 -0
  19. pkl-python-0.1.0/pkl/evaluator.py +257 -0
  20. pkl-python-0.1.0/pkl/handler.py +37 -0
  21. pkl-python-0.1.0/pkl/msgapi.py +274 -0
  22. pkl-python-0.1.0/pkl/parser.py +269 -0
  23. pkl-python-0.1.0/pkl/server.py +191 -0
  24. pkl-python-0.1.0/pkl_python.egg-info/PKG-INFO +138 -0
  25. pkl-python-0.1.0/pkl_python.egg-info/SOURCES.txt +35 -0
  26. pkl-python-0.1.0/pkl_python.egg-info/dependency_links.txt +1 -0
  27. pkl-python-0.1.0/pkl_python.egg-info/requires.txt +11 -0
  28. pkl-python-0.1.0/pkl_python.egg-info/top_level.txt +11 -0
  29. pkl-python-0.1.0/pyproject.toml +58 -0
  30. pkl-python-0.1.0/scripts/download_binary.py +42 -0
  31. pkl-python-0.1.0/scripts/eval.py +32 -0
  32. pkl-python-0.1.0/setup.cfg +4 -0
  33. pkl-python-0.1.0/setup.py +74 -0
  34. pkl-python-0.1.0/tests/test_evaluator.py +7 -0
  35. pkl-python-0.1.0/tests/test_parser.py +58 -0
  36. pkl-python-0.1.0/tests/test_responses.py +99 -0
  37. pkl-python-0.1.0/tests/test_server.py +7 -0
@@ -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,138 @@
1
+ Metadata-Version: 2.1
2
+ Name: pkl-python
3
+ Version: 0.1.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.6
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
+ # PKLL - PKL Language Python Binding
49
+ Python binding for [Apple's Pkl language](https://pkl-lang.org/index.html).
50
+
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
59
+
60
+ ## Installation
61
+
62
+ ``` bash
63
+ pip install pkll
64
+ ```
65
+
66
+ ## Usage
67
+ ### Basic Usage
68
+ Here's how you can start using PKLL to load a PKL module:
69
+
70
+ ```python
71
+ import pkll
72
+
73
+ config = pkll.load("path/to/pkl/example_module.pkl")
74
+ print(config)
75
+ ```
76
+
77
+ ## Advanced Features
78
+ For details on the parameters, refer [Message Passing API](https://pkl-lang.org/main/current/bindings-specification/message-passing-api.html).
79
+
80
+ ```python
81
+ from pkll import load
82
+
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)
90
+ ```
91
+
92
+ ### Custom Handler
93
+ It is possible to add custom resources or module handler:
94
+ ```python
95
+ import pkll
96
+ from pkll.handler import (
97
+ ListResponse,
98
+ ReadModuleResponse,
99
+ ReadResourceResponse,
100
+ ResourcesHandler,
101
+ )
102
+ from pkll.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 = pkll.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(),
128
+ )
129
+ ```
130
+
131
+ ## Contributing
132
+ 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
+
134
+ ## License
135
+ PKLL is released under the MIT License. See the LICENSE file for more details.
136
+
137
+ ## Contact
138
+ 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,91 @@
1
+ # PKLL - PKL Language Python Binding
2
+ Python binding for [Apple's Pkl language](https://pkl-lang.org/index.html).
3
+
4
+ ### Status
5
+ * Evaluator API: fully functional
6
+ * Code Generation: in development
7
+
8
+ ### TODO
9
+ * [ ] (codgen) binary installation feature
10
+ * [ ] (codgen) fix class order
11
+ * [ ] (codgen) clean up code
12
+
13
+ ## Installation
14
+
15
+ ``` bash
16
+ pip install pkll
17
+ ```
18
+
19
+ ## Usage
20
+ ### Basic Usage
21
+ Here's how you can start using PKLL to load a PKL module:
22
+
23
+ ```python
24
+ import pkll
25
+
26
+ config = pkll.load("path/to/pkl/example_module.pkl")
27
+ print(config)
28
+ ```
29
+
30
+ ## Advanced Features
31
+ For details on the parameters, refer [Message Passing API](https://pkl-lang.org/main/current/bindings-specification/message-passing-api.html).
32
+
33
+ ```python
34
+ from pkll import load
35
+
36
+ # Advanced loading with custom environment and properties
37
+ result = load(
38
+ "path/to/pkl/example_module.pkl"
39
+ env={"CUSTOM_ENV": "value"},
40
+ properties={"custom.property": "value"}
41
+ )
42
+ print(result)
43
+ ```
44
+
45
+ ### Custom Handler
46
+ It is possible to add custom resources or module handler:
47
+ ```python
48
+ import pkll
49
+ from pkll.handler import (
50
+ ListResponse,
51
+ ReadModuleResponse,
52
+ ReadResourceResponse,
53
+ ResourcesHandler,
54
+ )
55
+ from pkll.msgapi import ClientResourceReader
56
+
57
+ class CustomModuleHandler(ResourcesHandler):
58
+ def list_response(self, uri: str) -> ListResponse:
59
+ return ListResponse(
60
+ pathElements=[{"name": "foo.pkl", "isDirectory": False}]
61
+ )
62
+
63
+ def read_response(self, uri: str) -> ReadResourceResponse:
64
+ return ReadModuleResponse(
65
+ contents="foo = 1",
66
+ )
67
+
68
+ config = pkll.load(
69
+ "./tests/myModule.pkl",
70
+ allowedModules=["pkl:", "repl:", "file:", "customfs:"],
71
+ clientModuleReaders=[
72
+ {
73
+ "scheme": "customfs",
74
+ "hasHierarchicalUris": True,
75
+ "isGlobbable": True,
76
+ "isLocal": True,
77
+ }
78
+ ],
79
+ debug=True,
80
+ module_handler=CustomModuleHandler(),
81
+ )
82
+ ```
83
+
84
+ ## Contributing
85
+ 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.
86
+
87
+ ## License
88
+ PKLL is released under the MIT License. See the LICENSE file for more details.
89
+
90
+ ## Contact
91
+ 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,102 @@
1
+ from typing import Optional
2
+ import argparse
3
+ import sys
4
+ import os
5
+ from pathlib import Path
6
+
7
+ VERSION = "development"
8
+
9
+ def parse_args():
10
+ parser = argparse.ArgumentParser(description="Generates Python bindings for a Pkl module")
11
+
12
+ # Positional argument for module
13
+ parser.add_argument('module', help="The module for which to generate bindings", nargs="?")
14
+
15
+ # Optional arguments
16
+ parser.add_argument('--generator-settings', help="The path to a generator settings file", default="")
17
+ parser.add_argument('--output-path', help="The output directory to write generated sources into", default="")
18
+ parser.add_argument('--base-path', help="The base path used to determine relative output", default="")
19
+ parser.add_argument('--mapping', action='append', help="The mapping of a Pkl module name to a Python package name", default=[])
20
+ parser.add_argument('--suppress-format-warning', action='store_true', help="Suppress warnings around formatting issues")
21
+ parser.add_argument('--allowed-modules', action='append', help="URI patterns that determine which modules can be loaded and evaluated", default=[])
22
+ parser.add_argument('--allowed-resources', action='append', help="URI patterns that determine which resources can be loaded and evaluated", default=[])
23
+ parser.add_argument('--project-dir', help="The project directory to load dependency and evaluator settings from", default="")
24
+ parser.add_argument('--dry-run', action='store_true', help="Print out the names of the files that will be generated, but don't write any files")
25
+ # parser.add_argument('--version', action='store_true', help="Print the version and exit")
26
+
27
+ return parser.parse_args()
28
+
29
+ def find_project_dir(start_path: str) -> Optional[str]:
30
+ current_path = Path(start_path)
31
+ while current_path != current_path.parent:
32
+ if (current_path / "PklProject").exists():
33
+ return str(current_path)
34
+ current_path = current_path.parent
35
+ return None
36
+
37
+
38
+ // Loads the settings for controlling codegen.
39
+ // Uses a Pkl evaluator that is separate from what's used for actually running codegen.
40
+ func loadGeneratorSettings(generatorSettingsPath string, projectDirFlag string) (*generatorsettings.GeneratorSettings, error) {
41
+ projectDir := findProjectDir(projectDirFlag)
42
+ var evaluator pkl.Evaluator
43
+ var err error
44
+ if projectDir != "" {
45
+ evaluator, err = pkl.NewProjectEvaluator(context.Background(), projectDir, pkl.PreconfiguredOptions)
46
+ } else {
47
+ evaluator, err = pkl.NewEvaluator(context.Background(), pkl.PreconfiguredOptions)
48
+ }
49
+ if err != nil {
50
+ panic(err)
51
+ }
52
+ var source *pkl.ModuleSource
53
+ if generatorSettingsPath != "" {
54
+ source = pkl.FileSource(generatorSettingsPath)
55
+ } else if fileExists("generator-settings.pkl") {
56
+ source = pkl.FileSource("generator-settings.pkl")
57
+ } else {
58
+ source = generatorSettingsSource()
59
+ }
60
+ return generatorsettings.Load(context.Background(), evaluator, source)
61
+ }
62
+
63
+ def load_generator_settings(generator_settings_path, project_dir_flag):
64
+ project_dir = find_project_dir(project_dir_flag)
65
+ evaluator = None # Placeholder for evaluator initialization
66
+ # Initialize your evaluator here based on project_dir, if applicable
67
+
68
+ settings_path = generator_settings_path if generator_settings_path else "generator-settings.pkl"
69
+ if not os.path.exists(settings_path):
70
+ # Fallback to a default source or handle error
71
+ print("Settings file not found.")
72
+ return None
73
+
74
+ with open(settings_path, 'r') as settings_file:
75
+ settings = json.load(settings_file)
76
+ # Assuming the settings file is in JSON format
77
+ # For YAML, use `settings = yaml.safe_load(settings_file)`
78
+
79
+ # Placeholder: Load and return the settings using the evaluator
80
+ # This part of the logic will depend on how you intend to use the settings and the evaluator
81
+ return settings
82
+
83
+
84
+ def main():
85
+ args = parse_args()
86
+
87
+ if not args.module:
88
+ print("Error: Module name is required.")
89
+ sys.exit(1)
90
+
91
+ project_dir = find_project_dir(os.getcwd())
92
+ evaluator = PklEvaluator(project_dir)
93
+ settings = {}
94
+ if args.generator_settings:
95
+ settings = load_generator_settings(args.generator_settings, project_dir)
96
+
97
+ # Placeholder for the main logic to generate Python bindings.
98
+ # You would need to implement this based on how the Python bindings are generated from Pkl files.
99
+ print(f"Generating bindings for module: {args.module} with settings: {settings} and output path: {args.output_path}")
100
+
101
+ if __name__ == "__main__":
102
+ main()
@@ -0,0 +1,235 @@
1
+ import argparse
2
+ import os
3
+ import sys
4
+ import tempfile
5
+ from pathlib import Path
6
+ from typing import Dict
7
+ from textwrap import dedent
8
+
9
+ import pkll
10
+
11
+
12
+ def file_exists(filepath):
13
+ return Path(filepath).exists()
14
+
15
+
16
+ def do_find_project_dir(directory: Path) -> Path:
17
+ if (directory / "PklProject").exists():
18
+ return directory
19
+ parent = directory.parent
20
+ if parent == directory:
21
+ return Path()
22
+ return do_find_project_dir(parent)
23
+
24
+
25
+ def find_project_dir(project_dir_flag: str) -> Path:
26
+ if project_dir_flag:
27
+ return Path(project_dir_flag).resolve()
28
+ cwd = Path.cwd()
29
+ return do_find_project_dir(cwd)
30
+
31
+
32
+ def load_generator_settings(generator_settings_path: str, project_dir_flag: str):
33
+ project_dir = find_project_dir(project_dir_flag)
34
+ if project_dir:
35
+ print(f"Project directory found: {project_dir}")
36
+ else:
37
+ print("No specific project directory found, using current working directory.")
38
+
39
+ if generator_settings_path:
40
+ settings_path = Path(generator_settings_path)
41
+ else:
42
+ settings_path = project_dir / "generator-settings.pkl"
43
+ if not settings_path.exists():
44
+ print("Generator settings file not found. Using default settings...")
45
+ raise NotImplementedError
46
+
47
+ print(f"Loading settings from: {settings_path}")
48
+
49
+ config = pkll.load(settings_path.absolute().as_uri(), allowedResources=["file:"])
50
+ return config
51
+
52
+
53
+ def generate_dry_run(tmp_file_path: str, output_path: str, settings) -> None:
54
+ #filenames = evaluate_pkl("output.files.toMap().keys.toList()", tmp_file_path)
55
+ filenames = pkll.load(Path(tmp_file_path).as_uri(), expr="output.files.toMap().keys.toList()")
56
+
57
+ print("Dry run; printing filenames but not writing files to disk")
58
+ for filename in filenames:
59
+ if settings.basePath and filename.startswith(settings.basePath):
60
+ #filename = filename[len(settings.basePath) :]
61
+ filename = Path(filename).relative_to(settings.basePath)
62
+ else:
63
+ continue
64
+ print(Path(output_path) / filename)
65
+
66
+
67
+ def generate_python(
68
+ pkl_module_path: str, settings, output_path: str, dry_run: bool
69
+ ) -> None:
70
+ module_to_evaluate = dedent(
71
+ f"""\
72
+ amends "{Path(settings.generatorScriptPath).absolute()}"
73
+ import "{Path(pkl_module_path).absolute()}" as theModule
74
+
75
+ moduleToGenerate = theModule
76
+ """
77
+ )
78
+ module_to_evaluate = dedent(
79
+ """\
80
+ import "{}" as Generator
81
+ import "{}" as theModule
82
+
83
+ local gen = new Generator {{
84
+ codegenSettings {{
85
+ packageMappings {{
86
+ {}
87
+ }}
88
+ {}
89
+ structTags {{
90
+ {}
91
+ }}
92
+ }}
93
+ moduleToGenerate = theModule
94
+ }}
95
+ output = gen.output\
96
+ """.format(
97
+ Path(settings.generatorScriptPath).absolute(),
98
+ Path(pkl_module_path).absolute(),
99
+ "\\n".join(f'["{k}"] = "{v}"' for k, v in settings.packageMappings),
100
+ f'basePath = "{settings.basePath}"' if settings.basePath else "",
101
+ "\\n".join(f'["{k}"] = "{v}"' for k, v in settings.structTags),
102
+ )
103
+ )
104
+ module_to_evaluate = dedent(
105
+ """\
106
+ amends "{}"
107
+ import "{}" as theModule
108
+
109
+ codegenSettings = new {{
110
+ packageMappings {{
111
+ {}
112
+ }}
113
+ {}
114
+ structTags {{
115
+ {}
116
+ }}
117
+ }}
118
+ moduleToGenerate = theModule
119
+ """.format(
120
+ Path(settings.generatorScriptPath).absolute(),
121
+ Path(pkl_module_path).absolute(),
122
+ "\\n".join(f'["{k}"] = "{v}"' for k, v in settings.packageMappings),
123
+ f'basePath = "{settings.basePath}"' if settings.basePath else "",
124
+ "\\n".join(f'["{k}"] = "{v}"' for k, v in settings.structTags),
125
+ )
126
+ )
127
+
128
+ with tempfile.NamedTemporaryFile(suffix=".pkl") as tmp_file:
129
+ tmp_file.write(module_to_evaluate.encode())
130
+ tmp_file.flush()
131
+ print("module tmp file:", tmp_file.name)
132
+
133
+ if dry_run or settings.dryRun:
134
+ generate_dry_run(tmp_file.name, output_path, settings)
135
+ return
136
+
137
+ config = pkll.load(
138
+ Path(tmp_file.name).as_uri(),
139
+ expr="output",
140
+ allowedResources=["env:", "prop:", "package:", "projectpackage:", "file:"],
141
+ debug=True,
142
+ )
143
+ print(config)
144
+ breakpoint()
145
+
146
+
147
+ def main():
148
+ # Create the parser
149
+ parser = argparse.ArgumentParser(
150
+ description="Generates Python bindings for a Pkl module."
151
+ )
152
+
153
+ # Positional arguments
154
+ parser.add_argument("module", help="The Pkl module to process.")
155
+
156
+ # Optional arguments
157
+ parser.add_argument(
158
+ "--generator-settings",
159
+ default="",
160
+ help="The path to a generator settings file.",
161
+ )
162
+ parser.add_argument(
163
+ "--output-path",
164
+ default="",
165
+ help="The output directory to write generated sources into.",
166
+ )
167
+ parser.add_argument(
168
+ "--base-path",
169
+ default="",
170
+ help="The base path used to determine relative output paths.",
171
+ )
172
+ parser.add_argument(
173
+ "--generate-script",
174
+ default="",
175
+ help="The Generate.pkl script to use for code generation.",
176
+ )
177
+ parser.add_argument(
178
+ "--project-dir",
179
+ default="",
180
+ help="The project directory to load dependency and evaluator settings from.",
181
+ )
182
+ parser.add_argument(
183
+ "--dry-run",
184
+ action="store_true",
185
+ help="Print out the names of the files that will be generated, but don't write any files.",
186
+ )
187
+
188
+ # Flags
189
+ parser.add_argument(
190
+ "--suppress-format-warning",
191
+ action="store_true",
192
+ help="Suppress warnings around formatting issues.",
193
+ )
194
+ parser.add_argument(
195
+ "--version", action="store_true", help="Print the version of the tool and exit."
196
+ )
197
+
198
+ # Repeatable arguments
199
+ parser.add_argument(
200
+ "--mapping",
201
+ action="append",
202
+ nargs=2,
203
+ metavar=("PKL_MODULE_NAME", "PYTHON_PACKAGE_NAME"),
204
+ help="Mapping of a Pkl module name to a Python package name. This option can be repeated.",
205
+ )
206
+ parser.add_argument(
207
+ "--allowed-modules",
208
+ action="append",
209
+ help="URI patterns that determine which modules can be loaded and evaluated. Can be repeated.",
210
+ )
211
+ parser.add_argument(
212
+ "--allowed-resources",
213
+ action="append",
214
+ help="URI patterns that determine which resources can be read and evaluated. Can be repeated.",
215
+ )
216
+
217
+ # Parse the arguments
218
+ args = parser.parse_args()
219
+
220
+ if args.version:
221
+ print("Version placeholder") # Replace with actual version logic
222
+ sys.exit()
223
+
224
+ settings = load_generator_settings(args.generator_settings, args.project_dir)
225
+
226
+ if not args.output_path:
227
+ output_path = os.getcwd()
228
+ else:
229
+ output_path = args.output_path
230
+
231
+ generate_python(args.module, settings, output_path, args.dry_run)
232
+
233
+
234
+ if __name__ == "__main__":
235
+ main()
@@ -0,0 +1,112 @@
1
+ import os
2
+ from pathlib import Path
3
+ from tempfile import TemporaryDirectory
4
+ import sys
5
+ import pkll
6
+
7
+ # Assuming 'Evaluator' and 'GeneratorSettings' are classes you will define or adapt in Python
8
+ # from src.evaluator.evaluator import Evaluator
9
+ # from generated import GeneratorSettings
10
+ import logging
11
+
12
+ logger = logging.getLogger()
13
+ logging.basicConfig(level=logging.INFO, format="%(message)s")
14
+ chalk = logging # Simplified representation; in Python, you might use colorama or termcolor for colored output
15
+
16
+ class GeneratorSettings:
17
+ """
18
+ GeneratorSettings holds configuration for generating TypeScript files from PKL modules.
19
+ Attributes:
20
+ output_directory (str|None): The output path to write generated files into.
21
+ dry_run (bool|None): If true, evaluates the PKL modules but does not write any files.
22
+ generator_script_path (str|None): The Generator.pkl script to use for code generation.
23
+ """
24
+ def __init__(self, output_directory=None, dry_run=None, generator_script_path=None):
25
+ self.output_directory = output_directory
26
+ self.dry_run = dry_run
27
+ self.generator_script_path = generator_script_path
28
+
29
+
30
+ def to_absolute_path(path: str) -> Path:
31
+ return Path(path).resolve()
32
+
33
+
34
+ def generate_python(
35
+ pkl_module_paths: list, settings: GeneratorSettings, verbose: bool,
36
+ ):
37
+ logger.info(
38
+ f"Generating TypeScript sources for modules {', '.join(pkl_module_paths)}"
39
+ )
40
+
41
+ pkl_module_paths = [to_absolute_path(path) for path in pkl_module_paths]
42
+
43
+ if settings.generator_script_path:
44
+ if ":" in settings.generator_script_path:
45
+ settings.generator_script_path = Path(settings.generator_script_path)
46
+ else:
47
+ settings.generator_script_path = to_absolute_path(
48
+ settings.generator_script_path
49
+ )
50
+ logger.warning(
51
+ f"Using custom generator script: {settings.generator_script_path}"
52
+ )
53
+ else:
54
+ # Adjust the path as necessary for your project structure
55
+ settings.generator_script_path = (
56
+ Path(__file__).parent / "../codegen/src/Generator.pkl"
57
+ )
58
+
59
+ with TemporaryDirectory() as tmp_dir:
60
+ output_dir = to_absolute_path(
61
+ settings.output_directory if settings.output_directory else ".out"
62
+ )
63
+ output_dir.mkdir(parents=True, exist_ok=True)
64
+
65
+ for index, pkl_input_module in enumerate(pkl_module_paths):
66
+ module_to_evaluate = f"""
67
+ amends "{settings.generator_script_path}"
68
+
69
+ import "{pkl_input_module}" as theModule
70
+
71
+ moduleToGenerate = theModule
72
+ """
73
+
74
+ if logger.getEffectiveLevel() <= logging.DEBUG:
75
+ logger.info(
76
+ f"""
77
+ Evaluating temp Pkl module:
78
+ ---
79
+ {module_to_evaluate}
80
+ """
81
+ )
82
+
83
+ tmp_file_path = Path(tmp_dir) / f"pkl-gen-python-{index}.pkl"
84
+ with open(tmp_file_path, "w", encoding="utf-8") as tmp_file:
85
+ tmp_file.write(module_to_evaluate)
86
+
87
+ files = pkll.load(tmp_file_path.as_uri(), debug=verbose)
88
+
89
+ for filename, contents in files.items():
90
+ path = output_dir / filename
91
+ if not settings.dry_run:
92
+ with open(path, "w", encoding="utf-8") as file:
93
+ file.write(contents)
94
+ logger.info(f"Generated: {path}")
95
+
96
+
97
+ # Mockup classes to complete the example
98
+ class Evaluator:
99
+ async def evaluate_output_files(self, uri):
100
+ return {"example.ts": "const example = true;"}
101
+
102
+
103
+ # Example usage
104
+ def main():
105
+ settings = GeneratorSettings(output_directory="path/to/output", dry_run=False)
106
+ generate_python(
107
+ ["path/to/module1", "path/to/module2"], settings
108
+ )
109
+
110
+
111
+ if __name__ == "__main__":
112
+ main()