pkl-python 0.1.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.
- archive/cmd/pkl-gen-go/pkl-gen-python.py +102 -0
- archive/cmd/pkl-gen-python/pkl-gen-python.py +235 -0
- old/pkl-gen-python/generate.py +112 -0
- old/pkl-gen-python/main.py +94 -0
- out/ExtendModule_pkl.py +29 -0
- out/ExtendingOpenClass_pkl.py +57 -0
- out/Foo_pkl.py +61 -0
- out/Imports_pkl.py +20 -0
- out/MyModule_pkl.py +13 -0
- out/com_example_ExtendedSimple_pkl.py +33 -0
- out/com_example_Simple_pkl.py +65 -0
- out/lib3_pkl.py +24 -0
- out/union_pkl.py +37 -0
- pkl/VERSION +1 -0
- pkl/__init__.py +181 -0
- pkl/evaluator.py +257 -0
- pkl/handler.py +37 -0
- pkl/msgapi.py +274 -0
- pkl/parser.py +269 -0
- pkl/server.py +191 -0
- pkl_python-0.1.0.dist-info/LICENSE +21 -0
- pkl_python-0.1.0.dist-info/METADATA +138 -0
- pkl_python-0.1.0.dist-info/RECORD +31 -0
- pkl_python-0.1.0.dist-info/WHEEL +5 -0
- pkl_python-0.1.0.dist-info/top_level.txt +6 -0
- scripts/download_binary.py +42 -0
- scripts/eval.py +32 -0
- tests/test_evaluator.py +7 -0
- tests/test_parser.py +58 -0
- tests/test_responses.py +99 -0
- tests/test_server.py +7 -0
|
@@ -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()
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import logging
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from urllib.request import pathname2url
|
|
5
|
+
import pkll
|
|
6
|
+
|
|
7
|
+
# Mock-up classes and functions to match the TypeScript functionality
|
|
8
|
+
# These would need to be implemented or adapted for your specific use case
|
|
9
|
+
# from src import newEvaluator, PreconfiguredOptions
|
|
10
|
+
# from generated import GeneratorSettings, load as loadGeneratorSettings
|
|
11
|
+
# from generate import generateTypescript
|
|
12
|
+
|
|
13
|
+
from generate import generate_python, GeneratorSettings
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger()
|
|
16
|
+
logging.basicConfig(level=logging.INFO, format="%(message)s")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def setup_logger(verbose):
|
|
20
|
+
if verbose:
|
|
21
|
+
logger.setLevel(logging.DEBUG)
|
|
22
|
+
else:
|
|
23
|
+
logger.setLevel(logging.INFO)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def cli_handler(pkl_modules, settings_file_path, output_directory, dry_run, verbose):
|
|
27
|
+
setup_logger(verbose)
|
|
28
|
+
|
|
29
|
+
if not pkl_modules:
|
|
30
|
+
logger.error("You must provide at least one file to evaluate.")
|
|
31
|
+
return
|
|
32
|
+
|
|
33
|
+
settings_file = settings_file_path or (Path.cwd() / "generator-settings.pkl")
|
|
34
|
+
if not settings_file.exists():
|
|
35
|
+
settings_file = None
|
|
36
|
+
|
|
37
|
+
if settings_file:
|
|
38
|
+
logger.info(f"Using settings file at {settings_file}")
|
|
39
|
+
else:
|
|
40
|
+
logger.info("No settings file found, using default settings.")
|
|
41
|
+
|
|
42
|
+
if settings_file:
|
|
43
|
+
settings = pkll.load(pathname2url(str(settings_file)))
|
|
44
|
+
else:
|
|
45
|
+
settings = GeneratorSettings(dry_run=dry_run, output_directory=output_directory)
|
|
46
|
+
|
|
47
|
+
generate_python(pkl_modules, settings, verbose)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def main():
|
|
51
|
+
parser = argparse.ArgumentParser(
|
|
52
|
+
description="Generate TypeScript from PKL modules."
|
|
53
|
+
)
|
|
54
|
+
parser.add_argument(
|
|
55
|
+
"pklModules",
|
|
56
|
+
metavar="PklModule",
|
|
57
|
+
type=str,
|
|
58
|
+
nargs="+",
|
|
59
|
+
help="Pkl module to evaluate",
|
|
60
|
+
)
|
|
61
|
+
parser.add_argument(
|
|
62
|
+
"-s",
|
|
63
|
+
"--settings-file",
|
|
64
|
+
type=str,
|
|
65
|
+
help="Path to the generator-settings.pkl file",
|
|
66
|
+
)
|
|
67
|
+
parser.add_argument(
|
|
68
|
+
"-o",
|
|
69
|
+
"--output-directory",
|
|
70
|
+
type=str,
|
|
71
|
+
help="Directory to write generated files into",
|
|
72
|
+
)
|
|
73
|
+
parser.add_argument(
|
|
74
|
+
"--dry-run",
|
|
75
|
+
action="store_true",
|
|
76
|
+
help="Evaluate the Pkl modules but do not write them anywhere",
|
|
77
|
+
)
|
|
78
|
+
parser.add_argument(
|
|
79
|
+
"-v", "--verbose", action="store_true", help="Enable debug logging"
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
args = parser.parse_args()
|
|
83
|
+
|
|
84
|
+
cli_handler(
|
|
85
|
+
pkl_modules=args.pklModules,
|
|
86
|
+
settings_file_path=Path(args.settings_file) if args.settings_file else None,
|
|
87
|
+
output_directory=args.output_directory,
|
|
88
|
+
dry_run=args.dry_run,
|
|
89
|
+
verbose=args.verbose,
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
if __name__ == "__main__":
|
|
94
|
+
main()
|
out/ExtendModule_pkl.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Code generated from Pkl module `ExtendModule`. DO NOT EDIT.
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from typing import Dict, List, Literal, Optional, Union
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
import pkll
|
|
6
|
+
from . import MyModule_pkl
|
|
7
|
+
|
|
8
|
+
_NO_DEFAULT = object()
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class ExtendModule(MyModule_pkl.MyModule):
|
|
12
|
+
bar: str = _NO_DEFAULT
|
|
13
|
+
|
|
14
|
+
_registered_identifier: str = "ExtendModule"
|
|
15
|
+
|
|
16
|
+
def __post_init__(self):
|
|
17
|
+
no_default_list = [("bar", self.bar)]
|
|
18
|
+
for n, x in no_default_list:
|
|
19
|
+
if x is _NO_DEFAULT:
|
|
20
|
+
raise TypeError(f"__init__ missing 1 required argument: '{n}'")
|
|
21
|
+
|
|
22
|
+
@classmethod
|
|
23
|
+
def load_pkl(cls, source: str):
|
|
24
|
+
# Load the Pkl module at the given source and evaluate it into `ExtendModule.Module`.
|
|
25
|
+
# - Parameter source: The source of the Pkl module.
|
|
26
|
+
config = pkll.load(source)
|
|
27
|
+
return cls(**config._asdict())
|
|
28
|
+
|
|
29
|
+
breakpoint()
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Code generated from Pkl module `ExtendingOpenClass`. DO NOT EDIT.
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from typing import Dict, List, Literal, Optional, Union
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
import pkll
|
|
6
|
+
from . import lib3_pkl
|
|
7
|
+
|
|
8
|
+
_NO_DEFAULT = object()
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class MyClass(MyOpenClass):
|
|
12
|
+
myBoolean: bool = _NO_DEFAULT
|
|
13
|
+
|
|
14
|
+
myStr: str = "mystr"
|
|
15
|
+
|
|
16
|
+
_registered_identifier: str = "ExtendingOpenClass#MyClass"
|
|
17
|
+
|
|
18
|
+
def __post_init__(self):
|
|
19
|
+
no_default_list = [("myBoolean", self.myBoolean)]
|
|
20
|
+
for n, x in no_default_list:
|
|
21
|
+
if x is _NO_DEFAULT:
|
|
22
|
+
raise TypeError(f"__init__ missing 1 required argument: '{n}'")
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class MyOpenClass:
|
|
26
|
+
myStr: str
|
|
27
|
+
|
|
28
|
+
_registered_identifier: str = "ExtendingOpenClass#MyOpenClass"
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class MyClass2(lib3_pkl.GoGoGo):
|
|
32
|
+
myBoolean: bool = _NO_DEFAULT
|
|
33
|
+
|
|
34
|
+
duck: Literal["quack"] = "quack"
|
|
35
|
+
|
|
36
|
+
_registered_identifier: str = "ExtendingOpenClass#MyClass2"
|
|
37
|
+
|
|
38
|
+
def __post_init__(self):
|
|
39
|
+
no_default_list = [("myBoolean", self.myBoolean)]
|
|
40
|
+
for n, x in no_default_list:
|
|
41
|
+
if x is _NO_DEFAULT:
|
|
42
|
+
raise TypeError(f"__init__ missing 1 required argument: '{n}'")
|
|
43
|
+
|
|
44
|
+
@dataclass
|
|
45
|
+
class ExtendingOpenClass:
|
|
46
|
+
res1: MyClass
|
|
47
|
+
|
|
48
|
+
res2: MyClass2
|
|
49
|
+
|
|
50
|
+
_registered_identifier: str = "ExtendingOpenClass"
|
|
51
|
+
|
|
52
|
+
@classmethod
|
|
53
|
+
def load_pkl(cls, source: str):
|
|
54
|
+
# Load the Pkl module at the given source and evaluate it into `ExtendingOpenClass.Module`.
|
|
55
|
+
# - Parameter source: The source of the Pkl module.
|
|
56
|
+
config = pkll.load(source)
|
|
57
|
+
return cls(**config._asdict())
|
out/Foo_pkl.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Code generated from Pkl module `Foo`. DO NOT EDIT.
|
|
2
|
+
from typing import Dict, List, Literal, Optional, Union
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
import pkll
|
|
5
|
+
|
|
6
|
+
_NO_DEFAULT = object()
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class Animal(Being):
|
|
10
|
+
name: str = _NO_DEFAULT
|
|
11
|
+
|
|
12
|
+
_registered_identifier: str = "Foo#Animal"
|
|
13
|
+
|
|
14
|
+
def __post_init__(self):
|
|
15
|
+
no_default_list = [("name", self.name)]
|
|
16
|
+
for n, x in no_default_list:
|
|
17
|
+
if x is _NO_DEFAULT:
|
|
18
|
+
raise TypeError(f"__init__ missing 1 required argument: '{n}'")
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class Being:
|
|
22
|
+
exists: bool
|
|
23
|
+
|
|
24
|
+
_registered_identifier: str = "Foo#Being"
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class Bird(Animal):
|
|
28
|
+
flies: bool = _NO_DEFAULT
|
|
29
|
+
|
|
30
|
+
_registered_identifier: str = "Foo#Bird"
|
|
31
|
+
|
|
32
|
+
def __post_init__(self):
|
|
33
|
+
no_default_list = [("flies", self.flies)]
|
|
34
|
+
for n, x in no_default_list:
|
|
35
|
+
if x is _NO_DEFAULT:
|
|
36
|
+
raise TypeError(f"__init__ missing 1 required argument: '{n}'")
|
|
37
|
+
|
|
38
|
+
@dataclass
|
|
39
|
+
class Dog(Animal):
|
|
40
|
+
barks: bool = _NO_DEFAULT
|
|
41
|
+
|
|
42
|
+
_registered_identifier: str = "Foo#Dog"
|
|
43
|
+
|
|
44
|
+
def __post_init__(self):
|
|
45
|
+
no_default_list = [("barks", self.barks)]
|
|
46
|
+
for n, x in no_default_list:
|
|
47
|
+
if x is _NO_DEFAULT:
|
|
48
|
+
raise TypeError(f"__init__ missing 1 required argument: '{n}'")
|
|
49
|
+
|
|
50
|
+
@dataclass
|
|
51
|
+
class Foo:
|
|
52
|
+
animals: List[Animal]
|
|
53
|
+
|
|
54
|
+
_registered_identifier: str = "Foo"
|
|
55
|
+
|
|
56
|
+
@classmethod
|
|
57
|
+
def load_pkl(cls, source: str):
|
|
58
|
+
# Load the Pkl module at the given source and evaluate it into `Foo.Module`.
|
|
59
|
+
# - Parameter source: The source of the Pkl module.
|
|
60
|
+
config = pkll.load(source)
|
|
61
|
+
return cls(**config._asdict())
|