Skip to content
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

Fetch representation hierarchy #155

Merged
merged 5 commits into from
Jun 10, 2024
Merged
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
4 changes: 4 additions & 0 deletions ayon_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@
get_representations,
get_representation_by_id,
get_representation_by_name,
get_representations_hierarchy,
get_representation_hierarchy,
get_representations_parents,
get_representation_parents,
get_repre_ids_by_context_filters,
Expand Down Expand Up @@ -390,6 +392,8 @@
"get_representations",
"get_representation_by_id",
"get_representation_by_name",
"get_representations_hierarchy",
"get_representation_hierarchy",
"get_representations_parents",
"get_representation_parents",
"get_repre_ids_by_context_filters",
Expand Down
67 changes: 63 additions & 4 deletions ayon_api/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3232,14 +3232,69 @@ def get_representation_by_name(*args, **kwargs):
return con.get_representation_by_name(*args, **kwargs)


def get_representations_hierarchy(*args, **kwargs):
"""Find representation with parents by representation id.

Representation entity with parent entities up to project.

Default fields are used when any fields are set to `None`. But it is
possible to pass in empty iterable (list, set, tuple) to skip
entity.

Args:
project_name (str): Project where to look for entities.
representation_ids (Iterable[str]): Representation ids.
project_fields (Optional[Iterable[str]]): Project fields.
folder_fields (Optional[Iterable[str]]): Folder fields.
product_fields (Optional[Iterable[str]]): Product fields.
version_fields (Optional[Iterable[str]]): Version fields.
representation_fields (Optional[Iterable[str]]): Representation
fields.

Returns:
dict[str, RepresentationHierarchy]: Parent entities by
representation id.

"""
con = get_server_api_connection()
return con.get_representations_hierarchy(*args, **kwargs)


def get_representation_hierarchy(*args, **kwargs):
"""Find representation parents by representation id.

Representation parent entities up to project.

Args:
project_name (str): Project where to look for entities.
representation_id (str): Representation id.
project_fields (Optional[Iterable[str]]): Project fields.
folder_fields (Optional[Iterable[str]]): Folder fields.
product_fields (Optional[Iterable[str]]): Product fields.
version_fields (Optional[Iterable[str]]): Version fields.
representation_fields (Optional[Iterable[str]]): Representation
fields.

Returns:
RepresentationHierarchy: Representation hierarchy entities.

"""
con = get_server_api_connection()
return con.get_representation_hierarchy(*args, **kwargs)


def get_representations_parents(*args, **kwargs):
"""Find representations parents by representation id.

Representation parent entities up to project.

Args:
project_name (str): Project where to look for entities.
representation_ids (Iterable[str]): Representation ids.
project_name (str): Project where to look for entities.
representation_ids (Iterable[str]): Representation ids.
project_fields (Optional[Iterable[str]]): Project fields.
folder_fields (Optional[Iterable[str]]): Folder fields.
product_fields (Optional[Iterable[str]]): Product fields.
version_fields (Optional[Iterable[str]]): Version fields.

Returns:
dict[str, RepresentationParents]: Parent entities by
Expand All @@ -3256,8 +3311,12 @@ def get_representation_parents(*args, **kwargs):
Representation parent entities up to project.

Args:
project_name (str): Project where to look for entities.
representation_id (str): Representation id.
project_name (str): Project where to look for entities.
representation_id (str): Representation id.
project_fields (Optional[Iterable[str]]): Project fields.
folder_fields (Optional[Iterable[str]]): Folder fields.
product_fields (Optional[Iterable[str]]): Product fields.
version_fields (Optional[Iterable[str]]): Version fields.

Returns:
RepresentationParents: Representation parent entities.
Expand Down
4 changes: 2 additions & 2 deletions ayon_api/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@


def fields_to_dict(fields):
output = {}
if not fields:
return None
return output

output = {}
for field in fields:
hierarchy = field.split(".")
last = hierarchy.pop(-1)
Expand Down
76 changes: 53 additions & 23 deletions ayon_api/graphql_queries.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
import collections

from .constants import DEFAULT_LINK_FIELDS
from .graphql import FIELD_VALUE, GraphQlQuery


def fields_to_dict(fields):
if not fields:
return None

output = {}
for field in fields:
hierarchy = field.split(".")
last = hierarchy.pop(-1)
value = output
for part in hierarchy:
if value is FIELD_VALUE:
break

if part not in value:
value[part] = {}
value = value[part]

if value is not FIELD_VALUE:
value[last] = FIELD_VALUE
return output
from .graphql import FIELD_VALUE, GraphQlQuery, fields_to_dict


def add_links_fields(entity_field, nested_fields):
Expand Down Expand Up @@ -486,6 +464,58 @@ def representations_parents_qraphql_query(
return query


def representations_hierarchy_qraphql_query(
folder_fields,
product_fields,
version_fields,
representation_fields,
):
query = GraphQlQuery("RepresentationsParentsQuery")

project_name_var = query.add_variable("projectName", "String!")
repre_ids_var = query.add_variable("representationIds", "[String!]")

project_field = query.add_field("project")
project_field.set_filter("name", project_name_var)

fields_queue = collections.deque()

repres_field = project_field.add_field_with_edges("representations")
for key, value in fields_to_dict(representation_fields).items():
fields_queue.append((key, value, repres_field))

repres_field.set_filter("ids", repre_ids_var)
version_field = None
if folder_fields or product_fields or version_fields:
version_field = repres_field.add_field("version")
if version_fields:
for key, value in fields_to_dict(version_fields).items():
fields_queue.append((key, value, version_field))

product_field = None
if folder_fields or product_fields:
product_field = version_field.add_field("product")
for key, value in fields_to_dict(product_fields).items():
fields_queue.append((key, value, product_field))

if folder_fields:
folder_field = product_field.add_field("folder")
for key, value in fields_to_dict(folder_fields).items():
fields_queue.append((key, value, folder_field))

while fields_queue:
item = fields_queue.popleft()
key, value, parent = item
field = parent.add_field(key)
if value is FIELD_VALUE:
continue

for k, v in value.items():
fields_queue.append((k, v, field))

return query


def workfiles_info_graphql_query(fields):
query = GraphQlQuery("WorkfilesInfo")
project_name_var = query.add_variable("projectName", "String!")
Expand Down
Loading