Skip to content

Commit

Permalink
Create file with file-like object (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoNegri authored Nov 14, 2023
1 parent ecc6c5c commit b5499dc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
37 changes: 25 additions & 12 deletions ansys/rep/client/jms/api/project_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,24 @@ def _upload_files(project_api: ProjectApi, files):
fs_headers = {"content-type": "application/octet-stream"}

for f in files:
if getattr(f, "src", None) is not None:
with open(f.src, "rb") as file_content:
r = project_api.client.session.post(
f"{project_api.fs_bucket_url}/{f.storage_id}",
data=file_content,
headers=fs_headers,
)
f.hash = r.json()["checksum"]
f.size = os.path.getsize(f.src)
if getattr(f, "src", None) is None:
continue

is_file = isinstance(f.src, str) and os.path.exists(f.src)
content = f.src
if is_file:
content = open(f.src, "rb")

r = project_api.client.session.post(
f"{project_api.fs_bucket_url}/{f.storage_id}",
data=content,
headers=fs_headers,
)
f.hash = r.json()["checksum"]
f.size = r.request.headers.get("Content-Length", None)

if is_file:
content.close()


def create_files(project_api: ProjectApi, files, as_objects=True) -> List[File]:
Expand Down Expand Up @@ -647,7 +656,7 @@ def copy_jobs(project_api: ProjectApi, jobs: List[Job], as_objects=True, **query

def sync_jobs(project_api: ProjectApi, jobs: List[Job]):

url = f"{project_api.url}/jobs:sync"
url = f"{project_api.url}/jobs:sync" # noqa: E231
json_data = json.dumps({"job_ids": [obj.id for obj in jobs]})
r = project_api.client.session.put(f"{url}", data=json_data)

Expand All @@ -661,6 +670,10 @@ def _fs_copy_file(
destination_name: str,
) -> str:

json_data = json.dumps({"destination": f"ansfs://{destination_bucket}/{destination_name}"})
r = session.post(url=f"{fs_url}/{source_bucket}/{source_name}:copy", data=json_data)
json_data = json.dumps(
{"destination": f"ansfs://{destination_bucket}/{destination_name}"} # noqa: E231
)
r = session.post(
url=f"{fs_url}/{source_bucket}/{source_name}:copy", data=json_data # noqa: E231
)
return r.json()["checksum"]
11 changes: 8 additions & 3 deletions ansys/rep/client/jms/resource/file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from marshmallow.utils import missing
from typing import Union
import io

from ..schema.file import FileSchema
from ansys.rep.client.common import Object
Expand All @@ -9,8 +11,10 @@ class File(Object):
Parameters
----------
src : str, optional
Client-only field to specify the path of an input file.
src : Union[str, io.IOBase], optional
Client-only field to specify either the path of an input file
or a file-like object. In the latter case, `requests` recommends that
you open files in binary mode.
id : str, optional
Unique ID to access the resource, generated internally by the server on creation.
name : str
Expand Down Expand Up @@ -49,7 +53,8 @@ class Meta:
schema = FileSchema
rest_name = "files"

def __init__(self, src=None,
def __init__(self,
src=None,
id=missing,
name=missing,
type=missing,
Expand Down
15 changes: 15 additions & 0 deletions tests/jms/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# Author(s): O.Koenig
# ----------------------------------------------------------
import io
import logging
import os
import tempfile
Expand Down Expand Up @@ -50,6 +51,18 @@ def test_files(self):
)
files.append(File(name="img", evaluation_path="file000.jpg", type="image/jpeg", hash=None))
files.append(File(name="out", evaluation_path="file.out", type="text/plain", hash=None))

with open(mac_path, "rb") as f:
file_object_string = f.read()
files.append(
File(
name="file-object",
evaluation_path="my-file.txt",
type="text/plain",
src=io.BytesIO(file_object_string),
)
)

files_created = project_api.create_files(files)
for file in files_created:
self.assertTrue(file.created_by is not missing)
Expand All @@ -72,10 +85,12 @@ def test_files(self):
self.assertEqual(f.read(), files_queried[0].content)
with open(res_path, "rb") as f:
self.assertEqual(f.read(), files_queried[1].content)
self.assertEqual(file_object_string, files_queried[4].content)

# verify that file size was correctly set
self.assertEqual(os.path.getsize(mac_path), files_queried[0].size)
self.assertEqual(os.path.getsize(res_path), files_queried[1].size)
self.assertEqual(os.path.getsize(mac_path), files_queried[4].size)

with tempfile.TemporaryDirectory() as tpath:

Expand Down

0 comments on commit b5499dc

Please sign in to comment.