snazy commented on code in PR #3016:
URL: https://github.com/apache/polaris/pull/3016#discussion_r2513566784


##########
client/python/generate_clients.py:
##########
@@ -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())

Review Comment:
   Clever :)



##########
client/python/generate_clients.py:
##########
@@ -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}"

Review Comment:
   Do you think it makes sense to check for multiple classes to import from a 
single module (like `from x.y.z import A, B, C`)?



##########
client/python/generate_clients.py:
##########
@@ -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}"
+            )
+        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(imports))

Review Comment:
   ```suggestion
           f.write("\n".join(sorted(imports)))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to