Skip to content

fix: change iterdir() for rglob('*') #2101

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/2101.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change ``iterdir()`` for ``rglob('*')``
30 changes: 22 additions & 8 deletions src/ansys/geometry/core/modeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def exit(self, close_design: bool = True) -> None:
def _upload_file(
self,
file_path: str,
project_dir: Path,
open_file: bool = False,
import_options: ImportOptions = ImportOptions(),
) -> str:
Expand All @@ -263,6 +264,9 @@ def _upload_file(
----------
file_path : str
Path of the file to upload. The extension of the file must be included.
project_dir : Path
Root directory of the folder being uploaded. This is used to
determine the relative path of the file on the server.
open_file : bool, default: False
Whether to open the file in the Geometry service.
import_options : ImportOptions
Expand All @@ -287,7 +291,7 @@ def _upload_file(
if fp_path.is_dir():
raise ValueError("File path must lead to a file, not a directory.")

file_name = fp_path.name
file_name = fp_path.relative_to(project_dir).as_posix()

with fp_path.open(mode="rb") as file:
data = file.read()
Expand All @@ -308,6 +312,7 @@ def _upload_file(
def _upload_file_stream(
self,
file_path: str,
project_dir: Path,
open_file: bool = False,
import_options: ImportOptions = ImportOptions(),
) -> str:
Expand All @@ -317,6 +322,9 @@ def _upload_file_stream(
----------
file_path : str
Path of the file to upload. The extension of the file must be included.
project_dir : Path
Root directory of the folder being uploaded. This is used to
determine the relative path of the file on the server.
open_file : bool, default: False
Whether to open the file in the Geometry service.
import_options : ImportOptions
Expand Down Expand Up @@ -344,19 +352,22 @@ def _upload_file_stream(
c_stub = CommandsStub(self.client.channel)

response = c_stub.StreamFileUpload(
self._generate_file_chunks(fp_path, open_file, import_options)
self._generate_file_chunks(fp_path, project_dir, open_file, import_options)
)
return response.file_path

def _generate_file_chunks(
self, file_path: Path, open_file: bool, import_options: ImportOptions
self, file_path: Path, project_dir: Path, open_file: bool, import_options: ImportOptions
) -> Generator[UploadFileRequest, None, None]:
"""Generate appropriate chunk sizes for uploading files.

Parameters
----------
file_path : Path
Path of the file to upload. The extension of the file must be included.
project_dir : Path
Root directory of the folder being uploaded. This is used to
determine the relative path of the file on the server.
open_file : bool
Whether to open the file in the Geometry service.
import_options : ImportOptions
Expand All @@ -372,11 +383,12 @@ def _generate_file_chunks(
raise ValueError("MAX_MESSAGE_LENGTH is too small for file upload.")

chunk_size = pygeom_defaults.MAX_MESSAGE_LENGTH - msg_buffer
filename = file_path.relative_to(project_dir).as_posix()
with Path.open(file_path, "rb") as file:
while chunk := file.read(chunk_size):
yield UploadFileRequest(
data=chunk,
file_name=file_path.name,
file_name=filename,
open=open_file,
import_options=import_options.to_dict(),
)
Expand Down Expand Up @@ -441,14 +453,16 @@ def open_file(
if any(
ext in str(file_path) for ext in [".CATProduct", ".asm", ".solution", ".sldasm"]
):
dir = fp_path.parent
for file in dir.iterdir():
project_dir = fp_path.parent
for file in project_dir.rglob("*"):
full_path = file.resolve()
if not full_path.is_file():
continue
if full_path != fp_path:
if full_path.stat().st_size < pygeom_defaults.MAX_MESSAGE_LENGTH:
self._upload_file(full_path)
self._upload_file(full_path, project_dir)
elif self.client.backend_version >= (25, 2, 0):
self._upload_file_stream(full_path)
self._upload_file_stream(full_path, project_dir)
else: # pragma: no cover
raise RuntimeError(
"File is too large to upload."
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/integration/test_design_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,10 @@ def test_design_insert_id_bug(modeler: Modeler):

assert len(design1.components[0].bodies) == 1
assert len(design1.components[1].bodies) == 1


def test_nested_folder_import_with_open_file(modeler: Modeler):
"""Test importing a file from a nested folder structure."""
# Open the design *just verify it can be opened without errors)
file_path = Path(IMPORT_FILES_DIR, "nested_cat_project/car.CATProduct")
modeler.open_file(file_path)
Loading