diff --git a/doc/release/release_notes.rst b/doc/release/release_notes.rst index 298a3cb6..85837848 100644 --- a/doc/release/release_notes.rst +++ b/doc/release/release_notes.rst @@ -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 diff --git a/source/ftrack_api/session.py b/source/ftrack_api/session.py index 91a2fe24..22af1d9a 100644 --- a/source/ftrack_api/session.py +++ b/source/ftrack_api/session.py @@ -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) @@ -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( @@ -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( @@ -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( diff --git a/test/unit/structure/test_get_file_from_string_event.py b/test/unit/structure/test_get_file_from_string_event.py index d211023b..76ffd59f 100644 --- a/test/unit/structure/test_get_file_from_string_event.py +++ b/test/unit/structure/test_get_file_from_string_event.py @@ -7,6 +7,7 @@ import ftrack_api import ftrack_api.structure.standard +import ftrack_api.structure.id @pytest.fixture(scope='session') @@ -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.'''