MCPcopy Create free account
hub / github.com/Slicer/Slicer / downloadFromSource

Method downloadFromSource

Modules/Scripted/SampleData/SampleData.py:850–981  ·  view source on GitHub ↗

Given an instance of SampleDataSource, downloads the associated data and load them into Slicer if it applies. The function always returns a list. Based on the fileType(s), nodeName(s) and loadFile(s) associated with the source, different values may be appended to th

(self, source, maximumAttemptsCount=3, forceDownload=False)

Source from the content-addressed store, hash-verified

848 return filePaths
849
850 def downloadFromSource(self, source, maximumAttemptsCount=3, forceDownload=False):
851 """Given an instance of SampleDataSource, downloads the associated data and
852 load them into Slicer if it applies.
853
854 The function always returns a list.
855
856 Based on the fileType(s), nodeName(s) and loadFile(s) associated with
857 the source, different values may be appended to the returned list:
858
859 - if nodeName is specified, appends loaded nodes but if ``loadFile`` is False appends downloaded filepath
860 - if fileType is ``SceneFile``, appends downloaded filepath
861 - if fileType is ``ZipFile``, appends directory of extracted archive but if ``loadFile`` is False appends downloaded filepath
862
863 If no ``nodeNames`` and no ``fileTypes`` are specified or if ``loadFiles`` are all False,
864 returns the list of all downloaded filepaths.
865
866 If ``forceDownload`` is True then existing files are downloaded again (if no hash is provided).
867 """
868
869 # Input may contain urls without associated node names, which correspond to additional data files
870 # (e.g., .raw file for a .nhdr header file). Therefore we collect nodes and file paths separately
871 # and we only return file paths if no node names have been provided.
872 resultNodes = []
873 resultFilePaths = []
874
875 # If some node names are defined and some are left empty then we assume that it is intentional
876 # (e.g., you only want to load the image.nhdr file and not the image.raw file).
877 # In this case default node names are not generated.
878 generateDefaultNodeNames = all(n is None for n in source.nodeNames)
879
880 for uri, fileName, nodeName, checksum, loadFile, loadFileType in zip(
881 source.uris, source.fileNames, source.nodeNames, source.checksums, source.loadFiles, source.loadFileTypes, strict=False):
882
883 if nodeName is None or fileName is None:
884 # Determine file basename and extension from URL or path
885 import urllib
886 import uuid
887 if fileName is not None:
888 basename, ext = os.path.splitext(os.path.basename(fileName))
889 else:
890 p = urllib.parse.urlparse(uri)
891 basename, ext = os.path.splitext(os.path.basename(p.path))
892
893 # Generate default node name (we only need this if we want to load the file into the scene and no node name is provided)
894 if (nodeName is None) and (loadFile is not False) and generateDefaultNodeNames:
895 nodeName = basename
896
897 # Generate default file name (we always need this, even for just downloading)
898 if fileName is None:
899 # Generate a unique filename to avoid overwriting existing file with the same name
900 fileName = f"{nodeName if nodeName else basename}-{uuid.uuid4().hex}{ext}"
901
902 current_source = SampleDataSource(
903 uris=uri,
904 fileNames=fileName,
905 nodeNames=nodeName,
906 checksums=checksum,
907 loadFiles=loadFile,

Calls 10

downloadFileIntoCacheMethod · 0.95
logMessageMethod · 0.95
loadSceneMethod · 0.95
loadNodeMethod · 0.95
SampleDataSourceClass · 0.85
appendMethod · 0.80
extractArchiveMethod · 0.80
formatMethod · 0.45
removeMethod · 0.45