From ea6cf625d9321abb0a6f1c243f5815d393529642 Mon Sep 17 00:00:00 2001 From: "robin@ynput.io" Date: Wed, 13 Nov 2024 11:53:22 -0500 Subject: [PATCH] Implement project metadata feature through WireTapCom. --- client/ayon_flame/api/scripts/wiretap_com.py | 71 ++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/client/ayon_flame/api/scripts/wiretap_com.py b/client/ayon_flame/api/scripts/wiretap_com.py index 42b9257..402eb79 100644 --- a/client/ayon_flame/api/scripts/wiretap_com.py +++ b/client/ayon_flame/api/scripts/wiretap_com.py @@ -6,6 +6,7 @@ import sys import subprocess import json +import tempfile import xml.dom.minidom as minidom from copy import deepcopy import datetime @@ -459,6 +460,76 @@ def _set_project_colorspace(self, project_name, color_policy): color_policy, project_name )) + @staticmethod + def get_project_metadata(project_name): + """Retrieve project metadata as XML. + + Args: + project_name (str): name of the project. + + Returns: + str. The metadata formatted as XML. + + Raises: + RuntimeError. When metadata could not be fetched. + """ + wiretap_tools_dir = os.getenv("AYON_WIRETAP_TOOLS") + get_metadata_cmd = os.path.join( + wiretap_tools_dir, + "wiretap_get_metadata", + ) + + cmd = [ + get_metadata_cmd, + "-n", f"/projects/{project_name}", + "-s", "XML" + ] + + try: + return subprocess.check_output(cmd) + + except subprocess.CalledProcessError as error: + raise RuntimeError( + f"Cannot retrieve metadata for {project_name}" + ) from error + + @staticmethod + def set_project_metadata(project_name, xml_metadata): + """Retrieve project metadata as XML. + + Args: + project_name (str): name of the project. + xml_metadata (str): new metadata formatted as XML. + + Returns: + bool. Has the metadata been edited. + """ + wiretap_tools_dir = os.getenv("AYON_WIRETAP_TOOLS") + set_metadata_cmd = os.path.join( + wiretap_tools_dir, + "wiretap_set_metadata", + ) + + with tempfile.NamedTemporaryFile(delete=False) as tmp_file: + tmp_file.write(xml_metadata.encode()) + + cmd = [ + set_metadata_cmd, + "-n", f"/projects/{project_name}", + "-s", "XML", + "-f", tmp_file.name + ] + + try: + subprocess.check_output(cmd) + return True + + except subprocess.CalledProcessError: + return False + + finally: + os.remove(tmp_file.name) + def _subprocess_preexec_fn(): """ Helper function