-
Notifications
You must be signed in to change notification settings - Fork 380
Fix monkey patching #3016
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
Fix monkey patching #3016
Changes from all commits
abffaa1
d1e393a
9dc7378
71750ac
c29531e
7ca5511
17b75de
ce42f91
6753055
ff48a7d
a5ce393
f857e29
16e4649
e9ba76d
0deb31b
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 |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| import logging | ||
| import argparse | ||
| import shutil | ||
| import ast | ||
|
|
||
| # Paths | ||
| CLIENT_DIR = Path(__file__).parent | ||
|
|
@@ -306,9 +307,53 @@ def build() -> None: | |
| generate_polaris_management_client() | ||
| generate_polaris_catalog_client() | ||
| generate_iceberg_catalog_client() | ||
| fix_catalog_models_init() | ||
| prepend_licenses() | ||
|
|
||
|
|
||
| def fix_catalog_models_init() -> None: | ||
| """ | ||
| Regenerate the `apache_polaris.sdk.catalog.models.__init__.py` file by consolidating | ||
| imports for all model classes found under `apache_polaris/sdk/catalog/models`. | ||
|
|
||
| This ensures that rerunning the OpenAPI Generator (which overwrites `__init__.py`) | ||
| does not cause missing imports for earlier generated model files. | ||
| """ | ||
| logger.info("Fixing catalog models __init__.py...") | ||
| models_dir = CLIENT_DIR / "apache_polaris" / "sdk" / "catalog" / "models" | ||
| init_py = models_dir / "__init__.py" | ||
|
|
||
| # Get all python files in the models directory except __init__.py | ||
| model_files = [ | ||
| f for f in models_dir.glob("*.py") if f.is_file() and f.name != "__init__.py" | ||
| ] | ||
|
|
||
| # Generate import statements | ||
| imports = [] | ||
| for model_file in sorted(model_files): | ||
| module_name = model_file.stem | ||
| with open(model_file, "r") as f: | ||
| tree = ast.parse(f.read()) | ||
| class_name = None | ||
| for node in ast.walk(tree): | ||
| if isinstance(node, ast.ClassDef): | ||
| # Find the first class that doesn't start with an underscore | ||
| if not node.name.startswith("_"): | ||
| class_name = node.name | ||
| break | ||
| if class_name: | ||
| imports.append( | ||
| f"from apache_polaris.sdk.catalog.models.{module_name} import {class_name}" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it makes sense to check for multiple classes to import from a single module (like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is possible to do. But I thought OpenAPI generator always produces model files with only one public class (I may be wrong here). If there is needed, maybe we can add it as an enhancement PR instead?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nah, was just a comment. Nothing to do here :) |
||
| ) | ||
| else: | ||
| logger.warning(f"Could not find a suitable class in {model_file}") | ||
|
|
||
| # Write the new __init__.py | ||
| with open(init_py, "w") as f: | ||
| f.write("\n".join(sorted(imports))) | ||
| logger.info("Catalog models __init__.py fixed.") | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description="Generate Polaris Python clients.") | ||
| parser.add_argument( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clever :)