Skip to content

Commit

Permalink
Feat/push enhanced images (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
margalva authored Oct 22, 2024
1 parent c68e4ae commit 38ba103
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 28 deletions.
4 changes: 2 additions & 2 deletions codegen/pyadritem.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class Item:
if value.capitalize().endswith('png'):
with open(value, "rb") as fb:
img = fb.read()
elif value.capitalize().endswith('jpg') or value.capitalize().endswith('jpeg'):
# If jpg, then convert to png buffer first
if value.capitalize().endswith(('jpg', 'jpeg', 'tiff', 'tif')):
# If jpg or tiff, then convert to png buffer first
tmp_img = PIL_image_to_data(value)
img = tmp_img['file_data']
else:
Expand Down
3 changes: 2 additions & 1 deletion src/ansys/dynamicreporting/core/adr_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def __init__(
host_directory=self._data_directory,
db_directory=self._db_directory,
port=self._port,
ansys_version=self._ansys_version,
)
except Exception as e: # pragma: no cover
self.logger.error(f"Error starting the Docker Container.\n{str(e)}\n")
Expand Down Expand Up @@ -537,7 +538,7 @@ def start(
"exec_basis": self._ansys_installation,
"ansys_version": self._ansys_version,
}
if self._ansys_version >= 231:
if int(self._ansys_version) >= 231:
launch_kwargs.update({"allow_iframe_embedding": True})

try:
Expand Down
38 changes: 31 additions & 7 deletions src/ansys/dynamicreporting/core/docker_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def pull(self) -> None:
except Exception:
raise RuntimeError(f"Can't pull Docker image: {self._image_name}")

def start(self, host_directory: str, db_directory: str, port: int) -> None:
def start(self, host_directory: str, db_directory: str, port: int, ansys_version: int) -> None:
"""
Start the Docker container for Ansys Dynamic Reporting using a local image.
Expand All @@ -117,6 +117,7 @@ def start(self, host_directory: str, db_directory: str, port: int) -> None:
db_directory : str
Directory for the Ansys Dynamic Reporting database.
port: TCP port number for Ansys Dynamic Reporting.
ansys_version: ansys version
Returns
-------
Expand Down Expand Up @@ -201,12 +202,19 @@ def start(self, host_directory: str, db_directory: str, port: int) -> None:
# CEI Home for our use here. And, from this, get the Ansys version
# number.

cmd = ["bash", "--login", "-c", "ls /Nexus/CEI/nexus*/bin/nexus_launcher"]
if ansys_version is None:
launcher = "nexus_launcher"
else:
if int(self._ansys_version) > 242:
launcher = "adr_launcher"
else:
launcher = "nexus_launcher"
cmd = ["bash", "--login", "-c", f"ls /Nexus/CEI/nexus*/bin/{launcher}"]
ret = self._container.exec_run(cmd)
if ret[0] != 0: # pragma: no cover
self.stop()
raise RuntimeError(
"Can't find /Nexus/CEI/nexus*/bin/nexus_launcher in the Docker container.\n"
f"Can't find /Nexus/CEI/nexus*/bin/{launcher} in the Docker container.\n"
+ str(ret[1].decode("utf-8"))
)
p = ret[1].decode("utf-8").strip()
Expand Down Expand Up @@ -381,7 +389,11 @@ def create_nexus_db(self) -> str:
------
RuntimeError
"""
nexus_cmd = self._cei_home + "/bin/nexus_launcher create --db_directory /db_directory/ "
if int(self._ansys_version) > 242:
launcher = "adr_launcher"
else:
launcher = "nexus_launcher"
nexus_cmd = self._cei_home + f"/bin/{launcher} create --db_directory /db_directory/ "
return self.run_in_container(nexus_cmd)

def save_config(self) -> str:
Expand All @@ -399,7 +411,11 @@ def save_config(self) -> str:
------
RuntimeError
"""
nexus_cmd = self._cei_home + "/bin/nexus_launcher"
if int(self._ansys_version) > 242:
launcher = "adr_launcher"
else:
launcher = "nexus_launcher"
nexus_cmd = self._cei_home + f"/bin/{launcher}"
nexus_cmd += " --db_directory /db_directory"
nexus_cmd += " save_config"
ret = self.run_in_container(nexus_cmd)
Expand Down Expand Up @@ -434,7 +450,11 @@ def launch_nexus_server(
------
RuntimeError
"""
nexus_cmd = self._cei_home + "/bin/nexus_launcher start"
if int(self._ansys_version) > 242:
launcher = "adr_launcher"
else:
launcher = "nexus_launcher"
nexus_cmd = self._cei_home + f"/bin/{launcher} start"
nexus_cmd += " --db_directory /db_directory"
nexus_cmd += " --server_port "
nexus_cmd += str(self._port)
Expand All @@ -453,8 +473,12 @@ def stop(self) -> None:
"""Release any additional resources allocated during launching."""
try:
if self._nexus_is_running:
if int(self._ansys_version) > 242:
launcher = "adr_launcher"
else:
launcher = "nexus_launcher"
self._nexus_is_running = False
stop_cmd = self._cei_home + "/bin/nexus_launcher stop "
stop_cmd = self._cei_home + f"/bin/{launcher} stop "
stop_cmd += " --db_directory /db_directory"
self.run_in_container(stop_cmd)
except Exception as e:
Expand Down
52 changes: 34 additions & 18 deletions src/ansys/dynamicreporting/core/utils/report_objects.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import json
import logging
import os
import os.path
import pickle
import shlex
import sys
import uuid
import weakref

from PIL import Image
import dateutil
import dateutil.parser
import pytz
Expand Down Expand Up @@ -1333,23 +1333,39 @@ def set_payload_image(self, img):
from . import png
except Exception:
import png
# we can only read png images as string content (not filename)
reader = png.Reader(io.BytesIO(img))
# parse the input file
pngobj = reader.read()
width = pngobj[3]["size"][0]
height = pngobj[3]["size"][1]
imgdata = list(pngobj[2])
# tag the data and write it back out...
writer = png.Writer(
width=width,
height=height,
bitdepth=pngobj[3].get("bitdepth", 8),
greyscale=pngobj[3].get("greyscale", False),
alpha=pngobj[3].get("alpha", False),
planes=pngobj[3].get("planes", None),
palette=pngobj[3].get("palette", None),
)
try:
# we can only read png images as string content (not filename)
reader = png.Reader(io.BytesIO(img))
# parse the input file
pngobj = reader.read()
width = pngobj[3]["size"][0]
height = pngobj[3]["size"][1]
imgdata = list(pngobj[2])
# tag the data and write it back out...
writer = png.Writer(
width=width,
height=height,
bitdepth=pngobj[3].get("bitdepth", 8),
greyscale=pngobj[3].get("greyscale", False),
alpha=pngobj[3].get("alpha", False),
planes=pngobj[3].get("planes", None),
palette=pngobj[3].get("palette", None),
)
except Exception:
# enhanced images will fall into this case
data = report_utils.PIL_image_to_data(img)
self.width = data["width"]
self.height = data["height"]
writer = png.Writer(
width=self.width,
height=self.height,
)
imgdata = data["file_data"]
self.type = ItemREST.type_img
self.image_data = data["file_data"]
self.fileobj = io.BytesIO(self.image_data)
self.fileurl = "image." + data["format"]
return
# TODO: current version does not support set_text()?
# writer.set_text(dict(CEI_NEXUS_GUID=str(self.guid)))
io_in = io.BytesIO()
Expand Down
Binary file added tests/test_data/displacement.tiff
Binary file not shown.
16 changes: 16 additions & 0 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ def test_create_img_jpg(adr_service_create, request) -> bool:
assert len(new_img_items) == (len(img_items) + 1)


@pytest.mark.ado_test
def test_create_img_tiff(adr_service_create, request) -> bool:
_ = adr_service_create.start(
create_db=True,
exit_on_close=True,
delete_db=True,
)
filter_str = "A|i_type|cont|image"
img_items = adr_service_create.query(query_type="Item", filter=filter_str)
my_img = adr_service_create.create_item()
my_img.item_image = join(join(request.fspath.dirname, "test_data"), "displacement.tiff")
new_img_items = adr_service_create.query(query_type="Item", filter=filter_str)
adr_service_create.stop()
assert len(new_img_items) == (len(img_items) + 1)


def test_create_scene(adr_service_create, request) -> bool:
_ = adr_service_create.start(
create_db=True,
Expand Down

0 comments on commit 38ba103

Please sign in to comment.