Skip to content

Commit

Permalink
Merge pull request #895 from girder/can-read-list
Browse files Browse the repository at this point in the history
Add canReadList function to show what sources could be used.
  • Loading branch information
manthey authored Jul 26, 2022
2 parents 6638ddb + 10a3ef7 commit b803b9c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features
- Add a tifffile tile source ([885](../../pull/885))
- Added a canReadList method to large_image to show which source can be used ([895](../../pull/895))

### Improvements
- Pass options to the annotationLayer mode ([881](../../pull/881))
Expand Down
2 changes: 1 addition & 1 deletion large_image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#############################################################################

from . import tilesource # noqa
from .tilesource import canRead, getTileSource, new, open # noqa
from .tilesource import canRead, canReadList, getTileSource, new, open # noqa

try:
from importlib.metadata import PackageNotFoundError
Expand Down
42 changes: 36 additions & 6 deletions large_image/tilesource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,16 @@ def loadTileSources(entryPointName='large_image.source', sourceDict=AvailableTil
'Failed to loaded tile source %s' % entryPoint.name)


def getSourceNameFromDict(availableSources, pathOrUri, *args, **kwargs):
def getSortedSourceList(availableSources, pathOrUri, *args, **kwargs):
"""
Get a tile source based on a ordered dictionary of known sources and a path
name or URI. Additional parameters are passed to the tile source and can
be used for properties such as encoding.
Get an ordered list of sources where earlier sources are more likely to
work for a specified path or uri.
:param availableSources: an ordered dictionary of sources to try.
:param pathOrUri: either a file path or a fixed source via
large_image://<source>.
:returns: the name of a tile source that can read the input, or None if
there is no such source.
:returns: a list of (clash, priority, sourcename) for sources where
sourcename is a key in availableSources.
"""
uriWithoutProtocol = str(pathOrUri).split('://', 1)[-1]
isLargeImageUri = str(pathOrUri).startswith('large_image://')
Expand All @@ -99,6 +98,22 @@ def getSourceNameFromDict(availableSources, pathOrUri, *args, **kwargs):
getattr(availableSources[sourceName], k, False) != v
for k, v in properties.items())
sourceList.append((propertiesClash, priority, sourceName))
return sourceList


def getSourceNameFromDict(availableSources, pathOrUri, *args, **kwargs):
"""
Get a tile source based on a ordered dictionary of known sources and a path
name or URI. Additional parameters are passed to the tile source and can
be used for properties such as encoding.
:param availableSources: an ordered dictionary of sources to try.
:param pathOrUri: either a file path or a fixed source via
large_image://<source>.
:returns: the name of a tile source that can read the input, or None if
there is no such source.
"""
sourceList = getSortedSourceList(availableSources, pathOrUri, *args, **kwargs)
for _clash, _priority, sourceName in sorted(sourceList):
if availableSources[sourceName].canRead(pathOrUri, *args, **kwargs):
return sourceName
Expand Down Expand Up @@ -161,6 +176,21 @@ def canRead(*args, **kwargs):
return False


def canReadList(*args, **kwargs):
"""
Check if large_image can read a path or uri via each source.
:returns: A list of tuples of (source name, canRead).
"""
if not len(AvailableTileSources):
loadTileSources()
sourceList = getSortedSourceList(AvailableTileSources, *args, **kwargs)
result = []
for _clash, _priority, sourceName in sorted(sourceList):
result.append((sourceName, AvailableTileSources[sourceName].canRead(*args, **kwargs)))
return result


def new(*args, **kwargs):
"""
Create a new image.
Expand Down
6 changes: 6 additions & 0 deletions test/test_source_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,9 @@ def testGetTileFramesQuadInfo(options, lensrc, lenquads, frame10, src0, srclast,
crop10 = results['quads'][10]['crop']
for key, value in quads10.items():
assert crop10[key] == value


def testCanReadList():
imagePath = datastore.fetch('sample_image.ptif')
assert len(large_image.canReadList(imagePath)) > 1
assert any(canRead for source, canRead in large_image.canReadList(imagePath))

0 comments on commit b803b9c

Please sign in to comment.