-
Notifications
You must be signed in to change notification settings - Fork 66
feat: add get and create methods and **kwarg support for create #4866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
df68d26
61d4c66
f274df3
4e5c6be
269c59e
c40e9ed
91882b2
9430310
c38d797
9d6dacc
73cafbe
d42a24a
779bb60
51664a4
c3c5aee
20ce7f9
ae11446
fedf799
fb48b06
a81582c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add get and create methods and **kwarg support for create | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,6 @@ | |
| from ansys.fluent.core.utils.fluent_version import FluentVersion, all_versions | ||
|
|
||
| _PY_FILE = config.codegen_outdir / "solver" / "settings_builtin.py" | ||
| _PYI_FILE = config.codegen_outdir / "solver" / "settings_builtin.pyi" | ||
|
|
||
| _CLASS_NAME_OVERRIDES = { | ||
| "ReadCaseData": "ReadCaseAndData", | ||
|
|
@@ -83,6 +82,29 @@ def _get_named_objects_in_path(root, path, kind): | |
| return named_objects, final_type | ||
|
|
||
|
|
||
| def _has_create_method(root, path): | ||
| """Check if a setting object has a create method.""" | ||
| try: | ||
| cls = root | ||
| comps = path.split(".") | ||
| for comp in comps: | ||
| cls = cls._child_classes[comp] | ||
| # Check if the class has 'create' in its child classes or command names | ||
| return "create" in getattr(cls, "_child_classes", {}) or "create" in getattr( | ||
| cls, "command_names", [] | ||
| ) | ||
| except (KeyError, AttributeError): | ||
| return False | ||
|
|
||
|
|
||
| def _get_reciprocal_name(name: str) -> str | None: | ||
| """Get the reciprocal name (singular/plural counterpart) from DATA.""" | ||
| try: | ||
| return DATA[name][2] | ||
| except KeyError: | ||
| return None | ||
|
|
||
|
|
||
| def generate(version: str): | ||
| """Generate builtin setting classes.""" | ||
| print("Generating builtin settings...") | ||
|
|
@@ -132,14 +154,14 @@ def _write_deprecated_alias_class( | |
| "import warnings\n\n\n" | ||
| ) | ||
| f.write("__all__ = [\n") | ||
| for legacy_name, (kind, _) in DATA.items(): | ||
| for legacy_name, (kind, _, _) in DATA.items(): | ||
| name = _get_public_class_name(legacy_name) | ||
| _write_symbol_to_all(name, kind) | ||
| if name != legacy_name: | ||
| _write_symbol_to_all(legacy_name, kind) | ||
| f.write("]\n\n") | ||
| for legacy_name, v in DATA.items(): | ||
| kind, path = v | ||
| kind, path, _ = v | ||
| name = _get_public_class_name(legacy_name) | ||
| if isinstance(path, dict): | ||
| version_supported = False | ||
|
|
@@ -210,6 +232,7 @@ def _write_deprecated_alias_class( | |
| " return super().__new__(cls, settings_source=settings_source, **kwargs)\n\n" | ||
| ) | ||
|
|
||
|
|
||
| with open(_PYI_FILE, "w") as f: | ||
| for version in FluentVersion: | ||
| f.write( | ||
|
|
@@ -229,10 +252,92 @@ def _write_deprecated_alias_class( | |
| f.write(f" type(settings_root_{v.number}.{p}),\n") | ||
| f.write("): ...\n\n") | ||
|
|
||
| # Generate version-specific .pyi files | ||
| pyi_file = ( | ||
| config.codegen_outdir / "solver" / f"settings_builtin_{version.number}.pyi" | ||
| ) | ||
| with open(pyi_file, "w") as f: | ||
| # Import base classes and deprecated decorator | ||
| f.write( | ||
| "from typing_extensions import deprecated\n" | ||
| "from ansys.fluent.core.solver.settings_builtin_bases import _SingletonSetting, _CreatableNamedObjectSetting, _NonCreatableNamedObjectSetting, _CommandSetting\n" | ||
| ) | ||
| # Import version-specific root for type hints | ||
| f.write( | ||
| f"from ansys.fluent.core.generated.solver.settings_{version.number} import root as settings_root_{version.number}\n" | ||
| ) | ||
| f.write("\n\n") | ||
| for legacy_name, v in DATA.items(): | ||
| kind, path, recip = v | ||
| name = _get_public_class_name(legacy_name) | ||
| if isinstance(path, dict): | ||
| version_supported = False | ||
| for version_set, p in path.items(): | ||
| if version in version_set: | ||
| path = p | ||
| version_supported = True | ||
| break | ||
| if not version_supported: | ||
| continue | ||
| named_objects, final_type = _get_named_objects_in_path(root, path, kind) | ||
| if kind == "NamedObject": | ||
| kind = f"{final_type}NamedObject" | ||
| path_with_child = f"{path}.child_object_type" | ||
| f.write(f"class {name}(\n") | ||
| f.write(f" _{kind}Setting,\n") | ||
| f.write( | ||
| f" type(settings_root_{version.number}.{path_with_child}),\n" | ||
| ) | ||
| f.write("):\n") | ||
| if final_type == "Creatable": | ||
| f.write( | ||
| f" create = settings_root_{version.number}.{path}.create\n" | ||
| ) | ||
| else: | ||
| f.write(" ...\n") | ||
| f.write("\n") | ||
| else: | ||
| # For Singleton and Command types | ||
| # Check if this is a plural class by looking at its reciprocal | ||
| if kind == "Singleton" and recip: | ||
| # Add deprecated decorator for plural container classes | ||
| f.write(f'@deprecated("Use {recip}.all() instead")\n') | ||
|
|
||
| f.write(f"class {name}(\n") | ||
| f.write(f" _{kind}Setting,\n") | ||
| f.write(f" type(settings_root_{version.number}.{path}),\n") | ||
| f.write("):\n") | ||
| # Check if singleton has create method | ||
| if kind == "Singleton" and _has_create_method(root, path): | ||
| f.write( | ||
| f" create = settings_root_{version.number}.{path}.create\n" | ||
| ) | ||
| else: | ||
| f.write(" ...\n") | ||
| f.write("\n") | ||
|
|
||
| if name != legacy_name: | ||
| f.write(f"class {legacy_name}({name}): ...\n\n") | ||
|
|
||
|
|
||
| def generate_main_pyi(version_str: str): | ||
| """Generate main settings_builtin.pyi that imports from a specific version.""" | ||
| _MAIN_PYI_FILE = config.codegen_outdir / "solver" / "settings_builtin.pyi" | ||
| version_obj = FluentVersion(version_str) | ||
| with open(_MAIN_PYI_FILE, "w") as f: | ||
| f.write(f"# Re-export from version {version_str}\n") | ||
| f.write( | ||
| f"from ansys.fluent.core.generated.solver.settings_builtin_{version_obj.number} import *\n" | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| version = "261" # for development | ||
| generate(version) | ||
| # Generate for all available versions | ||
| versions = sorted([v.number for v in all_versions()]) | ||
|
Comment on lines
+335
to
+336
|
||
| for version in versions: | ||
| try: | ||
| generate(str(version)) | ||
| except Exception as e: | ||
| print(f"Failed to generate for version {version}: {e}") | ||
| # Generate main .pyi that imports from the latest version | ||
| generate_main_pyi(str(versions[-1])) | ||
Uh oh!
There was an error while loading. Please reload this page.