How to use the synapseclient.cache.add_local_file_to_cache function in synapseclient

To help you get started, we’ve selected a few synapseclient examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Sage-Bionetworks / synapsePythonClient / synapseclient / client.py View on Github external
if limitSearch is not None:
            #Go through and find the path of every entity found
            paths = [self.restGET('/entity/%s/path' %ent['id']) for ent in results]
            #Filter out all entities whose path does not contain limitSearch
            results = [ent for ent, path in zip(results, paths) if
                       utils.is_in_path(limitSearch, path)]
        if len(results)==0: #None found 
            raise SynapseError('File %s not found in Synapse' % (filepath,))
        elif len(results)>1:
            sys.stderr.write('\nWARNING: The file %s is associated with many entities in Synapse. '
                          'You can limit to a specific project or folder by setting the '
                          'limitSearch to a synapse Id.  Will use the first one returned: '
                          '%s version %i\n' %(filepath,  results[0]['id'], results[0]['versionNumber']))
        entity = results[0]
        bundle = self._getEntityBundle(entity)
        cache.add_local_file_to_cache(path = filepath, **bundle['entity'])
        return bundle
github Sage-Bionetworks / synapsePythonClient / synapseclient / client.py View on Github external
properties.update(self._uploadFileAsLocation(properties, entity['path']))
                
                    # A file has been uploaded, so version should not be incremented if possible
                    forceVersion = False
                    
                else:
                    fileHandle = self._uploadToFileHandleService(entity['path'], \
                                            synapseStore=entity.get('synapseStore', True),
                                            mimetype=local_state.get('contentType', None))
                    properties['dataFileHandleId'] = fileHandle['id']
                
                    # A file has been uploaded, so version must be updated
                    forceVersion = True
                    
                # The cache expects a path, but FileEntities and Locationables do not have the path in their properties
                cache.add_local_file_to_cache(path=entity['path'], **properties)
                    
            elif 'dataFileHandleId' not in properties and not isLocationable:
                # Handle the case where the Entity lacks an ID 
                # But becomes an update() due to conflict
                properties['dataFileHandleId'] = bundle['entity']['dataFileHandleId']

        # Create or update Entity in Synapse
        if 'id' in properties:
            properties = self._updateEntity(properties, forceVersion, versionLabel)
        else:
            try:
                properties = self._createEntity(properties)
            except SynapseHTTPError as ex:
                if createOrUpdate and ex.response.status_code == 409:
                    # Get the existing Entity's ID via the name and parent
                    existing_entity_id = self._findEntityIdByNameAndParent(properties['name'], properties.get('parentId', ROOT_ENTITY))
github Sage-Bionetworks / synapsePythonClient / synapseclient / client.py View on Github external
Stores or updates the given Wiki.
        
        :param wiki: A Wiki object
        
        :returns: An updated Wiki object
        """
        
        # Make sure the file handle field is a list
        if 'attachmentFileHandleIds' not in wiki:
            wiki['attachmentFileHandleIds'] = []

        # Convert all attachments into file handles
        if 'attachments' in wiki:
            for attachment in wiki['attachments']:
                fileHandle = self._uploadToFileHandleService(attachment)
                cache.add_local_file_to_cache(path=attachment, dataFileHandleId=fileHandle['id'])
                wiki['attachmentFileHandleIds'].append(fileHandle['id'])
            del wiki['attachments']
            
        # Perform an update if the Wiki has an ID
        if 'id' in wiki:
            wiki.update(self.restPUT(wiki.putURI(), wiki.json()))
        
        # Perform a create if the Wiki has no ID
        else:
            try:
                wiki.update(self.restPOST(wiki.postURI(), wiki.json()))
            except SynapseHTTPError as err:
                # If already present we get an unhelpful SQL error
                # TODO: implement createOrUpdate for Wikis, see SYNR-631
                if err.response.status_code == 400 and "DuplicateKeyException" in err.message:
                    raise SynapseHTTPError("Can't re-create a wiki that already exists. CreateOrUpdate not yet supported for wikis.", response=err.response)
github Sage-Bionetworks / synapsePythonClient / synapseclient / client.py View on Github external
else:
                # The local state of the Entity is normally updated by the _downloadFileEntity method
                # If the file exists locally, make sure the entity points to it
                localFileInfo = cache.retrieve_local_file_info(entityBundle, downloadPath)
                if 'path' in localFileInfo and localFileInfo['path'] is not None and os.path.isfile(localFileInfo['path']):
                    entity.update(localFileInfo)
                
                # If the file was not downloaded and does not exist, set the synapseStore flag appropriately
                if not isLocationable \
                        and 'path' in entity \
                        and (entity['path'] is None or not os.path.exists(entity['path'])):
                    entity['synapseStore'] = False
                    
            # Send the Entity's dictionary to the update the file cache
            if 'path' in entity.keys():
                cache.add_local_file_to_cache(**entity)
            elif 'path' in entity:
                cache.add_local_file_to_cache(path=entity['path'], **entity)
            elif downloadPath is not None:
                cache.add_local_file_to_cache(path=downloadPath, **entity)

        return entity
github Sage-Bionetworks / synapsePythonClient / synapseclient / client.py View on Github external
# If the file exists locally, make sure the entity points to it
                localFileInfo = cache.retrieve_local_file_info(entityBundle, downloadPath)
                if 'path' in localFileInfo and localFileInfo['path'] is not None and os.path.isfile(localFileInfo['path']):
                    entity.update(localFileInfo)
                
                # If the file was not downloaded and does not exist, set the synapseStore flag appropriately
                if not isLocationable \
                        and 'path' in entity \
                        and (entity['path'] is None or not os.path.exists(entity['path'])):
                    entity['synapseStore'] = False
                    
            # Send the Entity's dictionary to the update the file cache
            if 'path' in entity.keys():
                cache.add_local_file_to_cache(**entity)
            elif 'path' in entity:
                cache.add_local_file_to_cache(path=entity['path'], **entity)
            elif downloadPath is not None:
                cache.add_local_file_to_cache(path=downloadPath, **entity)

        return entity
github Sage-Bionetworks / synapsePythonClient / synapseclient / client.py View on Github external
if 'path' in localFileInfo and localFileInfo['path'] is not None and os.path.isfile(localFileInfo['path']):
                    entity.update(localFileInfo)
                
                # If the file was not downloaded and does not exist, set the synapseStore flag appropriately
                if not isLocationable \
                        and 'path' in entity \
                        and (entity['path'] is None or not os.path.exists(entity['path'])):
                    entity['synapseStore'] = False
                    
            # Send the Entity's dictionary to the update the file cache
            if 'path' in entity.keys():
                cache.add_local_file_to_cache(**entity)
            elif 'path' in entity:
                cache.add_local_file_to_cache(path=entity['path'], **entity)
            elif downloadPath is not None:
                cache.add_local_file_to_cache(path=downloadPath, **entity)

        return entity