Skip to content

Commit

Permalink
Merged in backlog/incorrect-handeling-of-sequence-file-type (pull req…
Browse files Browse the repository at this point in the history
…uest #274)

incorrect-handeling-of-sequence-file-type

Approved-by: Henrik Norin
Approved-by: Lorenzo Angeli
  • Loading branch information
torsdag committed Apr 8, 2022
2 parents 615621e + 475e0bc commit b4147b3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
8 changes: 8 additions & 0 deletions doc/release/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ Release Notes

.. currentmodule:: ftrack_api.session

.. release:: Upcoming

.. change:: fixed
:tags: session, event,

:ref:`event_list/ftrack.api.session.get-file-type-from-string` not handled
correctly when dealing with sequences.

.. release:: 2.3.2
:date: 2022-02-08

Expand Down
46 changes: 31 additions & 15 deletions source/ftrack_api/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,28 @@ def create_component(
else:
location = self.pick_location()

def retrieve_file_type(_path):
'''try to retrive the file type from any registered plugins. If
none are available fall back to os.path.splitext'''
response = self.event_hub.publish(
ftrack_api.event.base.Event(
topic='ftrack.api.session.get-file-type-from-string',
data=dict(
file_path=_path
)
),
synchronous=True
)

_file_type = next(
(result for result in response if result), None
)

if not _file_type:
return os.path.splitext(_path)[-1]

return _file_type

try:
collection = clique.parse(path)

Expand All @@ -1960,21 +1982,10 @@ def create_component(
if 'size' not in data:
data['size'] = self._get_filesystem_size(path)

file_type = self.event_hub.publish(
ftrack_api.event.base.Event(
topic='ftrack.api.session.get-file-type-from-string',
data=dict(
file_path=path
)
),
synchronous=True
file_type = retrieve_file_type(
path
)

# Pick the first valid result or None
file_type = next((result for result in file_type if result), None)
if not file_type:
file_type = os.path.splitext(path)[-1]

data.setdefault('file_type', file_type)

return self._create_component(
Expand All @@ -2001,9 +2012,14 @@ def create_component(
container_size += member_sizes[item]

# Create sequence component

container_path = collection.format('{head}{padding}{tail}')
file_type = retrieve_file_type(
container_path
)

data.setdefault('padding', collection.padding)
data.setdefault('file_type', os.path.splitext(container_path)[-1])
data.setdefault('file_type', file_type)
data.setdefault('size', container_size)

container = self._create_component(
Expand All @@ -2016,7 +2032,7 @@ def create_component(
'name': collection.match(member_path).group('index'),
'container': container,
'size': member_sizes[member_path],
'file_type': os.path.splitext(member_path)[-1]
'file_type': file_type
}

component = self._create_component(
Expand Down
18 changes: 17 additions & 1 deletion test/unit/structure/test_get_file_from_string_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import ftrack_api
import ftrack_api.structure.standard
import ftrack_api.structure.id


@pytest.fixture(scope='session')
Expand Down Expand Up @@ -80,7 +81,22 @@ def file_compound_extension_component_event(component_file=None):
file_compound_extension_no_component_event('mytest'), {},
'',
id='no-file-compound-extension-no-component-event'
)
),
pytest.param(
file_compound_extension_component_event('%04d.bgeo.sc [1-10]'), {},
'.bgeo.sc',
id='file-sequence-compound-extension-component-event-valid-clique'
),
pytest.param(
file_compound_extension_component_event('argh.%04d.bgeo.sc [1-10]'), {},
'.bgeo.sc',
id='file-sequence-compound-extension-component-event-valid-clique-with-prefix'
),
pytest.param(
file_compound_extension_component_event('foobar.%04d.jpg [1-10]'), {},
'.jpg',
id='file-sequence-compound-extension-component-event-valid-clique-single-extension'
),
])
def test_get_resource_identifier(structure, entity, context, expected):
'''Get resource identifier.'''
Expand Down

0 comments on commit b4147b3

Please sign in to comment.