Merge pull request #211 from ynput/bugfix/monkey-patch-ayon-api-last-version

Chore: Fix last versions getters for ayon api
This commit is contained in:
Jakub Trllo 2024-03-21 14:19:33 +01:00 committed by GitHub
commit 47cb00b433
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -10,6 +10,87 @@ class _Cache:
initialized = False
def _new_get_last_versions(
self,
project_name,
product_ids,
active=True,
fields=None,
own_attributes=False
):
"""Query last version entities by product ids.
Args:
project_name (str): Project where to look for representation.
product_ids (Iterable[str]): Product ids.
active (Optional[bool]): Receive active/inactive entities.
Both are returned when 'None' is passed.
fields (Optional[Iterable[str]]): fields to be queried
for representations.
own_attributes (Optional[bool]): Attribute values that are
not explicitly set on entity will have 'None' value.
Returns:
dict[str, dict[str, Any]]: Last versions by product id.
"""
if fields:
fields = set(fields)
fields.add("productId")
versions = self.get_versions(
project_name,
product_ids=product_ids,
latest=True,
hero=False,
active=active,
fields=fields,
own_attributes=own_attributes
)
return {
version["productId"]: version
for version in versions
}
def _new_get_last_version_by_product_id(
self,
project_name,
product_id,
active=True,
fields=None,
own_attributes=False
):
"""Query last version entity by product id.
Args:
project_name (str): Project where to look for representation.
product_id (str): Product id.
active (Optional[bool]): Receive active/inactive entities.
Both are returned when 'None' is passed.
fields (Optional[Iterable[str]]): fields to be queried
for representations.
own_attributes (Optional[bool]): Attribute values that are
not explicitly set on entity will have 'None' value.
Returns:
Union[dict[str, Any], None]: Queried version entity or None.
"""
versions = self.get_versions(
project_name,
product_ids=[product_id],
latest=True,
hero=False,
active=active,
fields=fields,
own_attributes=own_attributes
)
for version in versions:
return version
return None
def _new_get_last_version_by_product_name(
self,
project_name,
@ -73,9 +154,15 @@ def initialize_ayon_connection(force=False):
semver.VersionInfo.parse(ayon_api.__version__).to_tuple()
)
# TODO remove mokey patching after when AYON api is safely updated
fix_last_version_by_product_name = ayon_api_version < (1, 0, 2)
fix_before_1_0_2 = ayon_api_version < (1, 0, 2)
# Monkey patching to fix 'get_last_version_by_product_name'
if fix_last_version_by_product_name:
if fix_before_1_0_2:
ayon_api.ServerAPI.get_last_versions = (
_new_get_last_versions
)
ayon_api.ServerAPI.get_last_version_by_product_id = (
_new_get_last_version_by_product_id
)
ayon_api.ServerAPI.get_last_version_by_product_name = (
_new_get_last_version_by_product_name
)
@ -85,12 +172,22 @@ def initialize_ayon_connection(force=False):
if ayon_api.is_connection_created():
con = ayon_api.get_server_api_connection()
# Monkey patching to fix 'get_last_version_by_product_name'
if fix_last_version_by_product_name:
def _con_wrapper(*args, **kwargs):
if fix_before_1_0_2:
def _lvs_wrapper(*args, **kwargs):
return _new_get_last_versions(
con, *args, **kwargs
)
def _lv_by_pi_wrapper(*args, **kwargs):
return _new_get_last_version_by_product_id(
con, *args, **kwargs
)
def _lv_by_pn_wrapper(*args, **kwargs):
return _new_get_last_version_by_product_name(
con, *args, **kwargs
)
con.get_last_version_by_product_name = _con_wrapper
con.get_last_versions = _lvs_wrapper
con.get_last_version_by_product_id = _lv_by_pi_wrapper
con.get_last_version_by_product_name = _lv_by_pn_wrapper
con.set_site_id(site_id)
con.set_client_version(version)
else: